<?php
session_start();
$page_title = 'My Profile';
$active_nav = 'profile';
include("includes/header.php");

$error = $success = "";

$stmt = $conn->prepare("SELECT * FROM users WHERE id=?");
$stmt->bind_param("i",$uid); $stmt->execute();
$u = $stmt->get_result()->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $cur_pw  = $_POST['current_password'] ?? '';
    $new_pw  = $_POST['new_password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (!$name) { $error = "Name is required."; }
    else {
        $conn->begin_transaction();
        try {
            $up = $conn->prepare("UPDATE users SET name=?,phone=?,updated_at=NOW() WHERE id=?");
            $up->bind_param("ssi",$name,$phone,$uid); $up->execute();
            $_SESSION['name'] = $name;

            if ($cur_pw) {
                if (!password_verify($cur_pw,$u['password'])) throw new Exception("Current password is incorrect.");
                if (strlen($new_pw)<6) throw new Exception("New password must be at least 6 characters.");
                if ($new_pw !== $confirm) throw new Exception("Passwords do not match.");
                $hash = password_hash($new_pw, PASSWORD_DEFAULT);
                $pp = $conn->prepare("UPDATE users SET password=? WHERE id=?");
                $pp->bind_param("si",$hash,$uid); $pp->execute();
            }
            $conn->commit();
            $success = "Profile updated successfully!";
            $stmt->execute();
            $u = $stmt->get_result()->fetch_assoc();
        } catch(Exception $e) {
            $conn->rollback();
            $error = $e->getMessage();
        }
    }
}
?>

<div style="max-width:580px;margin:0 auto">
  <div style="margin-bottom:28px">
    <h1 style="font-size:1.5rem;font-weight:800;letter-spacing:-.03em;margin-bottom:4px">My Profile</h1>
    <p style="color:var(--text-muted);font-size:.875rem">Manage your super admin account</p>
  </div>

  <div class="card" style="padding:24px;margin-bottom:20px;display:flex;align-items:center;gap:18px;flex-wrap:wrap">
    <div style="width:68px;height:68px;border-radius:50%;background:var(--primary);display:flex;align-items:center;justify-content:center;font-size:1.4rem;font-weight:800;color:#fff;flex-shrink:0"><?php echo $__initials; ?></div>
    <div>
      <div style="font-size:1.1rem;font-weight:800;margin-bottom:2px"><?php echo htmlspecialchars($u['name']); ?></div>
      <div style="font-size:.85rem;color:var(--text-muted)"><?php echo htmlspecialchars($u['email']); ?></div>
      <span class="badge badge-error" style="margin-top:6px">Super Admin</span>
    </div>
    <div style="margin-left:auto;text-align:right">
      <div style="font-size:.7rem;color:var(--text-muted);text-transform:uppercase;letter-spacing:.08em;font-weight:700;margin-bottom:3px">Since</div>
      <div style="font-size:.875rem;font-weight:700"><?php echo date('M Y', strtotime($u['created_at'])); ?></div>
    </div>
  </div>

  <?php if($error): ?><div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div><?php endif; ?>
  <?php if($success): ?><div class="alert alert-success"><svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg><?php echo $success; ?></div><?php endif; ?>

  <form method="POST">
    <div class="card" style="margin-bottom:16px">
      <div class="card-header"><h3>Personal Details</h3></div>
      <div class="card-body">
        <div class="form-group">
          <label>Full Name</label>
          <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($u['name']); ?>" required>
        </div>
        <div class="form-group">
          <label>Email Address</label>
          <input type="email" class="form-control" value="<?php echo htmlspecialchars($u['email']); ?>" disabled style="opacity:.6;cursor:not-allowed">
        </div>
        <div class="form-group" style="margin-bottom:0">
          <label>Phone Number</label>
          <input type="tel" name="phone" class="form-control" value="<?php echo htmlspecialchars($u['phone']??''); ?>" placeholder="08012345678">
        </div>
      </div>
    </div>

    <div class="card" style="margin-bottom:20px">
      <div class="card-header"><h3>Change Password <span style="font-size:.75rem;font-weight:400;color:var(--text-muted)">(leave blank to keep current)</span></h3></div>
      <div class="card-body">
        <div class="form-group">
          <label>Current Password</label>
          <input type="password" name="current_password" class="form-control" placeholder="Enter current password">
        </div>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px">
          <div class="form-group" style="margin-bottom:0">
            <label>New Password</label>
            <input type="password" name="new_password" class="form-control" placeholder="Min. 6 characters">
          </div>
          <div class="form-group" style="margin-bottom:0">
            <label>Confirm New Password</label>
            <input type="password" name="confirm_password" class="form-control" placeholder="Repeat">
          </div>
        </div>
      </div>
    </div>

    <button type="submit" class="btn btn-primary btn-lg btn-block">Save Changes</button>
  </form>
</div>

<?php include("includes/footer.php"); ?>
