<?php
	session_start();
	if($_SESSION['unohs'] == null){
		header("location:index.php?msg=unauthorized");
	}
?>
<?php
	include ("conn.php");
	include (__DIR__ . "/../telegram_notify.php");
	include (__DIR__ . "/../sms_notify.php");

	// site_settings Table (না থাকলে নিজে থেকেই তৈরি হবে)
	$conn->query("
		CREATE TABLE IF NOT EXISTS site_settings (
			setting_key VARCHAR(50) PRIMARY KEY,
			setting_value VARCHAR(255)
		)
	");

	$message = '';

	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save_sms') {
		$mobile = trim($_POST['admin_alert_mobile'] ?? '');
		$stmt = $conn->prepare("
			INSERT INTO site_settings (setting_key, setting_value) VALUES ('admin_alert_mobile', ?)
			ON DUPLICATE KEY UPDATE setting_value = ?
		");
		$stmt->bind_param("ss", $mobile, $mobile);
		$stmt->execute();
		$stmt->close();
		$message = '✅ SMS Alert Mobile Number সংরক্ষণ হয়েছে।';
	}

	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'send_sms_test') {
		$ok = sendSmsAlert($conn, "Test: Aapka SMS Deposit Alert System kaam kar raha hai!");
		$message = $ok ? '✅ Test SMS পাঠানো হয়েছে — Phone Check করুন।' : '❌ Test SMS পাঠানো যায়নি। Mobile Number আবার Check করুন।';
	}

	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save_telegram') {
		$botToken = trim($_POST['telegram_bot_token'] ?? '');
		$chatId   = trim($_POST['telegram_chat_id'] ?? '');

		$stmt = $conn->prepare("
			INSERT INTO site_settings (setting_key, setting_value) VALUES ('telegram_bot_token', ?)
			ON DUPLICATE KEY UPDATE setting_value = ?
		");
		$stmt->bind_param("ss", $botToken, $botToken);
		$stmt->execute();
		$stmt->close();

		$stmt2 = $conn->prepare("
			INSERT INTO site_settings (setting_key, setting_value) VALUES ('telegram_chat_id', ?)
			ON DUPLICATE KEY UPDATE setting_value = ?
		");
		$stmt2->bind_param("ss", $chatId, $chatId);
		$stmt2->execute();
		$stmt2->close();

		$message = '✅ Telegram Setting সংরক্ষণ হয়েছে।';
	}

	if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'send_test') {
		$ok = sendTelegramAlert($conn, "🔔 <b>Test Notification</b>\nআপনার Telegram Alert System ঠিকভাবে কাজ করছে!");
		$message = $ok ? '✅ Test Message পাঠানো হয়েছে — Telegram Check করুন।' : '❌ Test Message পাঠানো যায়নি। Bot Token/Chat ID আবার Check করুন।';
	}

	$current = ['telegram_bot_token' => '', 'telegram_chat_id' => '', 'admin_alert_mobile' => ''];
	$result = $conn->query("SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('telegram_bot_token','telegram_chat_id','admin_alert_mobile')");
	while ($row = $result->fetch_assoc()) { $current[$row['setting_key']] = $row['setting_value']; }
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Dashboard</title>
  <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
  <link rel="stylesheet" href="vendors/feather/feather.css">
  <link rel="stylesheet" href="vendors/base/vendor.bundle.base.css">
  <link rel="stylesheet" href="vendors/flag-icon-css/css/flag-icon.min.css"/>
  <link rel="stylesheet" href="vendors/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="css/style.css">
  <link rel="shortcut icon" href="images/favicon.png" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Sora:wght@600;700;800&family=Inter:wght@400;500;600&display=swap">
  <style>
	:root{ --tg-bg:#100B22; --tg-panel:#1B1633; --tg-border:rgba(245,197,66,0.22); --tg-gold:#F5C542; --tg-text:#EFEBFB; --tg-muted:#9691B8; --tg-emerald:#22C58B; --tg-coral:#F75676; }
	.tg-wrap{ font-family:'Inter',sans-serif; max-width:520px; margin:24px auto 40px; }
	.tg-card{ background:linear-gradient(165deg, var(--tg-panel) 0%, var(--tg-bg) 100%); border:1px solid var(--tg-border); border-radius:18px; padding:26px; box-shadow:0 20px 50px -20px rgba(0,0,0,0.6); margin-bottom:16px; }
	.tg-title{ font-family:'Sora',sans-serif; font-weight:700; font-size:18px; color:var(--tg-text); margin:0 0 6px; }
	.tg-sub{ color:var(--tg-muted); font-size:12.5px; margin:0 0 20px; line-height:1.6; }
	.tg-label{ display:block; color:var(--tg-muted); font-size:11px; font-weight:700; text-transform:uppercase; letter-spacing:.08em; margin:16px 0 7px; }
	.tg-label:first-of-type{ margin-top:0; }
	.tg-input{ width:100%; padding:12px 14px; border-radius:10px; border:1px solid #2A2250; background:#0E0B1A; color:var(--tg-text); font-size:14px; font-family:'Inter',sans-serif; }
	.tg-input:focus{ outline:none; border-color:var(--tg-gold); }
	.tg-btn{ width:100%; padding:13px; border-radius:11px; border:none; font-weight:700; font-size:14px; margin-top:18px; cursor:pointer; font-family:'Inter',sans-serif; }
	.tg-btn-gold{ background:linear-gradient(180deg,#F0D264,var(--tg-gold)); color:#1A1400; }
	.tg-btn-test{ background:rgba(34,197,139,0.12); border:1px solid var(--tg-emerald); color:var(--tg-emerald); }
	.tg-msg{ padding:12px 16px; border-radius:10px; font-size:13px; margin-bottom:16px; }
	.tg-msg-ok{ background:rgba(34,197,139,.12); border:1px solid var(--tg-emerald); color:var(--tg-emerald); }
	.tg-msg-err{ background:rgba(247,86,118,.12); border:1px solid var(--tg-coral); color:var(--tg-coral); }
	.tg-steps{ font-size:12.5px; color:var(--tg-muted); line-height:1.9; }
	.tg-steps b{ color:var(--tg-text); }
  </style>
</head>
<body>
  <div class="container-scroller">
    <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
      <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center">
        <a class="navbar-brand brand-logo" href="dashboard.php"><img src="images/logo.png" alt="logo"/></a>
        <a class="navbar-brand brand-logo-mini" href="dashboard.php"><img src="images/logo-mini.png" alt="logo"/></a>
      </div>
    </nav>
    <div class="container-fluid page-body-wrapper">
      <nav class="sidebar sidebar-offcanvas" id="sidebar">
        <div class="user-profile">
          <div class="user-image"><img src="images/faces/face28.png"></div>
          <div class="user-name">bigdelhi</div>
          <div class="user-designation">Admin</div>
        </div>
        <?php include 'compass.php';?>
      </nav>
      <div class="main-panel">
        <div class="content-wrapper">
          <div class="row">
            <div class="col-sm-12 mb-4">
              <h4 class="font-weight-bold text-dark">Deposit Alert Settings</h4>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-12">
              <div class="tg-wrap">

                <?php if ($message): ?>
                  <div class="tg-msg <?= strpos($message,'✅')!==false ? 'tg-msg-ok' : 'tg-msg-err' ?>"><?= htmlspecialchars($message) ?></div>
                <?php endif; ?>

                <div class="tg-card">
                  <p class="tg-title">📱 SMS Alert (সবচেয়ে সহজ, কোনো Setup লাগবে না)</p>
                  <p class="tg-sub">একাধিক Admin-এর Mobile Number দিতে চাইলে, Comma ( , ) দিয়ে আলাদা করে লিখুন — যেমন: 8801XXXXXXXXX,8801YYYYYYYYY,8801ZZZZZZZZZ। Project-এ ইতিমধ্যে থাকা SMS Gateway ব্যবহার করে নতুন Deposit/Withdraw এলেই সবার কাছে একসাথে SMS যাবে।</p>
                  <form method="POST">
                    <input type="hidden" name="action" value="save_sms">
                    <label class="tg-label">Admin-দের Mobile Number (কমা দিয়ে আলাদা করা)</label>
                    <input class="tg-input" type="text" name="admin_alert_mobile" placeholder="8801XXXXXXXXX,8801YYYYYYYYY,8801ZZZZZZZZZ" value="<?= htmlspecialchars($current['admin_alert_mobile']) ?>">
                    <button type="submit" class="tg-btn tg-btn-gold">Save করো</button>
                  </form>
                </div>

                <form method="POST" style="margin-bottom:24px;">
                  <input type="hidden" name="action" value="send_sms_test">
                  <button type="submit" class="tg-btn tg-btn-test">📨 Test SMS পাঠাও</button>
                </form>

                <div class="tg-card">
                  <p class="tg-title">🔔 নতুন Deposit/Withdraw এলে Telegram-এ Alert</p>
                  <p class="tg-sub">Deposit/Withdraw Request জমা হলেই আপনার Telegram-এ সাথে সাথে একটা Message আসবে — Phone Notification-সহ, Alarm-এর মতো। ৪ জন Admin থাকলে প্রতিটা Admin-এর নিজের Chat ID এই একই Bot-কে <b>/start</b> করে জোগাড় করতে হবে, তারপর নিচে সবগুলো Chat ID Comma ( , ) দিয়ে একসাথে বসান।</p>
                  <div class="tg-steps">
                    <b>ধাপ ১:</b> Telegram-এ গিয়ে <b>@BotFather</b>-কে খুঁজুন, <b>/newbot</b> লিখে একটা নতুন Bot বানান — একটা Token পাবেন (শুধু একবার, সব Admin এই একই Token ব্যবহার করবে)।<br>
                    <b>ধাপ ২:</b> প্রতিটা Admin আলাদাভাবে <b>@userinfobot</b>-কে Message করে নিজের নিজের <b>Chat ID</b> বের করুন।<br>
                    <b>ধাপ ৩:</b> প্রতিটা Admin নিজে থেকে একবার Bot-টাকে <b>/start</b> করুন (নাহলে Bot তাকে Message পাঠাতে পারবে না)।<br>
                    <b>ধাপ ৪:</b> নিচে Token একবার বসান, আর সব Admin-এর Chat ID একসাথে Comma দিয়ে বসিয়ে Save করুন।
                  </div>
                  <form method="POST">
                    <input type="hidden" name="action" value="save_telegram">
                    <label class="tg-label">Bot Token</label>
                    <input class="tg-input" type="text" name="telegram_bot_token" placeholder="123456789:AAExxxxxxxxxxxxxxxxxxxxxxxxxxxxx" value="<?= htmlspecialchars($current['telegram_bot_token']) ?>">
                    <label class="tg-label">Chat ID (একাধিক হলে কমা দিয়ে আলাদা করুন)</label>
                    <input class="tg-input" type="text" name="telegram_chat_id" placeholder="যেমন: 987654321,123456789,555555555,111222333" value="<?= htmlspecialchars($current['telegram_chat_id']) ?>">
                    <button type="submit" class="tg-btn tg-btn-gold">Save করো</button>
                  </form>
                </div>

                <form method="POST">
                  <input type="hidden" name="action" value="send_test">
                  <button type="submit" class="tg-btn tg-btn-test">📨 Test Message পাঠাও</button>
                </form>

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
