<?php
require_once("auth.php");

$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_mobile'])) {
    $mobile_raw = trim($_POST['new_mobile'] ?? '');
    $digits     = preg_replace('/^(\+?880|\+?91|0)/', '', $mobile_raw);
    $new_mobile = ($digits !== '') ? '880' . $digits : '';
    $password_confirm = $_POST['password_confirm'] ?? '';

    if (!preg_match('/^880[0-9]{7,12}$/', $new_mobile)) {
        $message = 'সঠিক format-এ Mobile Number দাও।';
        $message_type = 'danger';
    } else {
        $pwChk = mysqli_prepare($conn, "SELECT password FROM shonu_subjects WHERE id = ?");
        mysqli_stmt_bind_param($pwChk, "i", $agent_uid);
        mysqli_stmt_execute($pwChk);
        $pwRow = mysqli_stmt_get_result($pwChk)->fetch_assoc();
        mysqli_stmt_close($pwChk);

        if (!$pwRow || $pwRow['password'] !== md5($password_confirm)) {
            $message = 'ভুল Password — নিরাপত্তার জন্য নিজের বর্তমান password দিতে হবে।';
            $message_type = 'danger';
        } else {
            $mchk = mysqli_prepare($conn, "SELECT id FROM shonu_subjects WHERE mobile = ? AND id != ?");
            mysqli_stmt_bind_param($mchk, "si", $new_mobile, $agent_uid);
            mysqli_stmt_execute($mchk);
            mysqli_stmt_store_result($mchk);

            if (mysqli_stmt_num_rows($mchk) > 0) {
                $message = "এই Mobile Number ($new_mobile) আগে থেকেই অন্য কারো account-এ আছে।";
                $message_type = 'danger';
            } else {
                mysqli_stmt_close($mchk);

                $u1 = mysqli_prepare($conn, "UPDATE shonu_subjects SET mobile = ? WHERE id = ?");
                mysqli_stmt_bind_param($u1, "si", $new_mobile, $agent_uid);
                mysqli_stmt_execute($u1);
                mysqli_stmt_close($u1);

                $u2 = mysqli_prepare($conn, "UPDATE tb_agent SET mobile = ? WHERE userid = ?");
                mysqli_stmt_bind_param($u2, "si", $new_mobile, $agent_uid);
                mysqli_stmt_execute($u2);
                mysqli_stmt_close($u2);

                $_SESSION['agent_mobile'] = $new_mobile;
                $agent_mobile = $new_mobile;
                $message = "✅ Mobile Number সফলভাবে বদলে গেছে: $new_mobile — এখন থেকে এই number দিয়েই login করবে।";
                $message_type = 'success';
            }
        }
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['set_contact'])) {
    $contact_raw = trim($_POST['contact_mobile'] ?? '');
    $c_digits    = preg_replace('/^(\+?880|\+?91|0)/', '', $contact_raw);
    $contact_mobile = ($c_digits !== '') ? '880' . $c_digits : '';

    if (!preg_match('/^880[0-9]{7,12}$/', $contact_mobile)) {
        $message = 'সঠিক Contact Number দাও।';
        $message_type = 'danger';
    } else {
        $cu = mysqli_prepare($conn, "UPDATE tb_agent SET contact_mobile = ? WHERE userid = ?");
        mysqli_stmt_bind_param($cu, "si", $contact_mobile, $agent_uid);
        mysqli_stmt_execute($cu);
        mysqli_stmt_close($cu);

        $message = "✅ Contact Number সেট হয়ে গেছে: $contact_mobile — User-রা এখন এই number-ই দেখবে।";
        $message_type = 'success';
    }
}

$cur = mysqli_prepare($conn, "SELECT mobile FROM shonu_subjects WHERE id = ?");
mysqli_stmt_bind_param($cur, "i", $agent_uid);
mysqli_stmt_execute($cur);
$curRow = mysqli_stmt_get_result($cur)->fetch_assoc();
mysqli_stmt_close($cur);
$current_mobile = $curRow['mobile'] ?? '';

$curC = mysqli_prepare($conn, "SELECT contact_mobile FROM tb_agent WHERE userid = ?");
mysqli_stmt_bind_param($curC, "i", $agent_uid);
mysqli_stmt_execute($curC);
$curCRow = mysqli_stmt_get_result($curC)->fetch_assoc();
mysqli_stmt_close($curC);
$current_contact = $curCRow['contact_mobile'] ?? '';

include("header.php");
?>

<div class="al-wrap">
  <div class="al-page-title">Mobile Number</div>
  <div class="al-page-sub">Login নাম্বার ও User-দের জন্য Contact নাম্বার পরিচালনা করো</div>

  <?php if ($message): ?>
  <div class="al-alert al-alert-<?php echo $message_type; ?>"><?php echo htmlspecialchars($message); ?></div>
  <?php endif; ?>

  <div class="al-card" style="border-left:3px solid var(--cyan);">
    <div style="font-weight:700;font-size:13px;margin-bottom:4px;color:var(--text);text-transform:uppercase;letter-spacing:.4px;">📱 Login Mobile Number</div>
    <div style="font-size:12px;color:var(--text-dim);margin-bottom:14px;">এটাই তোমার Login ID — সাবধানে বদলাও।</div>

    <div style="background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:10px 14px;font-size:12px;margin-bottom:16px;">
      বর্তমান: <b class="mono" style="color:var(--cyan);"><?php echo htmlspecialchars($current_mobile); ?></b>
    </div>

    <form method="POST">
      <div class="al-field">
        <label>নতুন Mobile Number</label>
        <input class="al-input mono" type="text" name="new_mobile" placeholder="যেমন: 1812345678" required>
      </div>
      <div class="al-field">
        <label>বর্তমান Password (নিশ্চিত করার জন্য)</label>
        <input class="al-input" type="password" name="password_confirm" placeholder="Password" required>
      </div>
      <button type="submit" name="change_mobile" value="1" class="al-btn al-btn-cyan al-btn-block">Mobile Number পরিবর্তন করো</button>
    </form>
  </div>

  <div class="al-card" style="border-left:3px solid var(--gold);margin-top:16px;">
    <div style="font-weight:700;font-size:13px;margin-bottom:4px;color:var(--text);text-transform:uppercase;letter-spacing:.4px;">📞 Contact Number</div>
    <div style="font-size:12px;color:var(--text-dim);margin-bottom:14px;">Balance কম থাকলে User-রা এই number-ই দেখবে।</div>

    <div style="background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:10px 14px;font-size:12px;margin-bottom:16px;">
      বর্তমান: <b class="mono" style="color:var(--gold);"><?php echo $current_contact ? htmlspecialchars($current_contact) : '(set করা হয়নি)'; ?></b>
    </div>

    <form method="POST">
      <div class="al-field">
        <label>নতুন Contact Number</label>
        <input class="al-input mono" type="text" name="contact_mobile" placeholder="যেমন: 1812345678" required>
      </div>
      <button type="submit" name="set_contact" value="1" class="al-btn al-btn-primary al-btn-block">Contact Number সেট করো</button>
    </form>
  </div>
</div>
</body>
</html>
