<?php 

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
include('includes/flash.php');

if(isset($_POST['update']))

{

  $eid=$_SESSION['edid'];

  $permission=$_POST['permission'];

  $mobile=$_POST['mobile'];

  $username=$_POST['username'];

  $name=$_POST['name'];

  $lastname=$_POST['lastname'];

  $email=$_POST['email'];

  $title=$_POST['title'];

  $sql="update tblusers set name=:name,username=:username,lastname=:lastname,email=:email,mobile=:mobile,permission=:permission,title=:title where id=:eid";

  $query=$dbh->prepare($sql);

  $query->bindParam(':name',$name,PDO::PARAM_STR);

  $query->bindParam(':lastname',$lastname,PDO::PARAM_STR);

  $query->bindParam(':username',$username,PDO::PARAM_STR);

  $query->bindParam(':email',$email,PDO::PARAM_STR);

  $query->bindParam(':permission',$permission,PDO::PARAM_STR);

  $query->bindParam(':mobile',$mobile,PDO::PARAM_STR);

  $query->bindParam(':title',$title,PDO::PARAM_STR);

  $query->bindParam(':eid',$eid,PDO::PARAM_STR);

  $query->execute();

  flash_alert('User updated successfully');
  header('Location: userregister.php'); exit;

}

?>

<div style="background:#fff;border-radius:16px;padding:24px;box-shadow:0 4px 20px rgba(15,23,42,.06);">
  <form role="form" method="post" enctype="multipart/form-data">
    <?php
    $eid = $_POST['edit_id'];
    $sql = "SELECT * FROM tblusers WHERE id=:eid";
    $query = $dbh->prepare($sql);
    $query->bindParam(':eid', $eid, PDO::PARAM_STR);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);
    if ($query->rowCount() > 0) {
      foreach ($results as $row) {
        $_SESSION['edid'] = $row->id;
    ?>
    <div class="row">
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Title</label>
        <select name="title" class="form-control modal-select2" required style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;background:#f8fafc;">
          <option value="">Select Title</option>
          <?php foreach (['Mr','Mrs','Miss','Ms','Master','Madam'] as $t): ?>
            <option value="<?= $t ?>" <?= $row->title === $t ? 'selected' : '' ?>><?= $t ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">First Name</label>
        <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($row->name) ?>" required style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;padding:0 14px;background:#f8fafc;font-size:.88rem;">
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Last Name</label>
        <input type="text" name="lastname" class="form-control" value="<?= htmlspecialchars($row->lastname) ?>" required style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;padding:0 14px;background:#f8fafc;font-size:.88rem;">
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Username</label>
        <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($row->username) ?>" style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;padding:0 14px;background:#f8fafc;font-size:.88rem;">
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Email</label>
        <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($row->email) ?>" style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;padding:0 14px;background:#f8fafc;font-size:.88rem;">
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Mobile</label>
        <input type="text" name="mobile" class="form-control" value="<?= htmlspecialchars($row->mobile) ?>" required style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;padding:0 14px;background:#f8fafc;font-size:.88rem;">
      </div>
      <div class="form-group col-md-6">
        <label style="font-size:.72rem;font-weight:600;text-transform:uppercase;letter-spacing:.06em;color:#64748b;margin-bottom:6px;">Permission</label>
        <select name="permission" class="form-control modal-select2" required style="height:44px;border:1.5px solid #e2e8f0;border-radius:12px;background:#f8fafc;">
          <?php foreach (['Super User','Admin','Student Search','Teacher'] as $p): ?>
            <option value="<?= $p ?>" <?= $row->permission === $p ? 'selected' : '' ?>><?= $p === 'Student Search' ? 'Student Search Officer' : $p ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    </div>
    <?php }} ?>
    <div style="margin-top:20px;padding-top:20px;border-top:1px solid #e2e8f0;display:flex;justify-content:flex-end;gap:10px;">
      <button type="button" class="btn" data-dismiss="modal" style="background:#e2e8f0;color:#475569;border:none;border-radius:10px;padding:10px 24px;font-weight:600;font-size:.85rem;">Cancel</button>
      <button type="submit" name="update" class="btn" style="background:linear-gradient(135deg,#1e3a5f,#3b82f6);color:#fff;border:none;border-radius:10px;padding:10px 24px;font-weight:600;font-size:.85rem;box-shadow:0 4px 14px rgba(59,130,246,.4);">
        <i class="fas fa-save"></i> Save Changes
      </button>
    </div>
  </form>
</div>



