<?php 
include_once('../header.php');
if(!check_login()){
  header("Location: " . WEB_URL . "logout.php");
  die();
}
if(isset($_SESSION['login_type']) && $_SESSION['login_type'] !== "super"){
  header("Location: ".WEB_URL."logout.php");
  die();
}

/* -------------------------------------------------
   Load settings & language
------------------------------------------------- */
$lang_code_global   = "English";
$global_currency    = "$";
$currency_position  = "left";
$currency_sep       = ".";

$q_settings = mysqli_query($link,"SELECT * FROM settings");
while($s = mysqli_fetch_array($q_settings)){
  $lang_code_global  = $s['lang_code'];
  $global_currency   = $s['currency'];
  $currency_position = $s['currency_position'];
  $currency_sep      = $s['currency_seperator'];
}
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_admin_setup.php');

/* -------------------------------------------------
   Small helper
------------------------------------------------- */
function e($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

/* -------------------------------------------------
   Defaults
------------------------------------------------- */
$name        = '';
$email       = '';
$contact     = '';
$password    = '';
$button_text = $_data['button_text_save'];
$form_url    = WEB_URL . "setting/admin.php";
$hval        = 0;
$__alert     = false;

$image_rnt = WEB_URL . 'img/no_image.jpg';
$img_track = '';

/* -------------------------------------------------
   Handle create/update (logic unchanged)
------------------------------------------------- */
$data = post_input_serilization();
if(isset($data['txtAdminName'])){
  $data["password"] = hash_password($data["txtAdminPassword"]);

  // Create
  if($data['hdnSpid'] == '0'){		
    $result = mysqli_query($link,"SELECT * FROM admins WHERE email = '" . trim($data['txtAdminEmail']). "'");
    if($row = mysqli_fetch_array($result)){
      $__alert = true; // email exists
    } else {
      $image_url = uploadImage();
      $sql = "INSERT INTO `admins`(`name`,`email`,`contact`,`password`,`image`) 
              VALUES ('{$data['txtAdminName']}','{$data['txtAdminEmail']}','{$data['txtAdminContact']}','{$data['password']}','$image_url')";
      $link->query($sql);
      mysqli_close($link);
      header("Location: " . WEB_URL . 'setting/admin.php?m=add');
      exit;
    }
  }
  // Update
  else {
    $image_url = uploadImage();
    if($image_url == '') $image_url = $data['img_exist'];
    $password  = hash_password($data['txtAdminPassword']);

    $sql_update = "
      UPDATE `admins`
      SET name    = '{$data['txtAdminName']}',
          email   = '{$data['txtAdminEmail']}',
          contact = '{$data['txtAdminContact']}',
          password= '{$password}',
          image   = '{$image_url}'
      WHERE user_id = '" . (int)$data['hdnSpid'] . "'
    ";
    $link->query($sql_update);
    mysqli_close($link);
    header("Location: " . WEB_URL . 'setting/admin.php?m=up');
    exit;
  }
}

/* -------------------------------------------------
   Edit mode: load admin
------------------------------------------------- */
if(isset($_GET['spid']) && $_GET['spid'] != ''){
  $result = mysqli_query($link,"SELECT * FROM admins WHERE user_id = '" . (int)$_GET['spid'] . "'");
  if($row = mysqli_fetch_array($result)){
    $name        = $row['name'];
    $email       = $row['email'];
    $contact     = $row['contact'];
    $button_text = $_data['button_text_update'];
    $form_url    = WEB_URL . "setting/admin.php?id=".$_GET['spid'];
    $hval        = $row['user_id'];
    if($row['image'] != ''){
      $image_rnt = WEB_URL . 'img/upload/' . $row['image'];
      $img_track = $row['image'];
    }
  }
}

/* -------------------------------------------------
   Upload helpers (logic unchanged)
------------------------------------------------- */
function uploadImage(){
  if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
    $filename = basename($_FILES['uploaded_file']['name']);
    $ext = substr($filename, strrpos($filename, '.') + 1);
    $ok = ($ext == "jpg" && $_FILES["uploaded_file"]["type"] == 'image/jpeg') ||
          ($ext == "png" && $_FILES["uploaded_file"]["type"] == 'image/png')   ||
          ($ext == "gif" && $_FILES["uploaded_file"]["type"] == 'image/gif');
    if($ok){
      $temp = explode(".",$filename);
      $newfilename = NewGuid() . '.' . end($temp);
      move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], ROOT_PATH . '/img/upload/' . $newfilename);
      return $newfilename;
    }
  }
  return '';
}
function NewGuid(){ 
  $s = strtoupper(md5(uniqid(rand(),true))); 
  return substr($s,0,8).'-'.substr($s,8,4).'-'.substr($s,12,4).'-'.substr($s,16,4).'-'.substr($s,20);
}

/* -------------------------------------------------
   Prefill phone on edit
------------------------------------------------- */
?>
<?php if($contact != ""){ ?>
<script>
  $(function(){
    const inst = window.intlTelInputGlobals.getInstance($("#txtAdminContact")[0]);
    if(inst){ inst.setNumber("<?= e($contact) ?>"); }
  });
</script>
<?php } ?>

<!-- Page Header -->
<section class="content-header">
  <h1 style="display:flex;align-items:center;gap:.6rem;">
    <i class="fa fa-user-shield"></i> <?= e($_data['page_title']); ?>
  </h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i> <?= e($_data['home']); ?></a></li>
    <li class="active"><?= e($_data['settings']); ?></li>
    <li class="active"><?= e($_data['page_title']); ?></li>
  </ol>
</section>

<!-- Subtle, low-contrast visual polish -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
<style>
  :root{
    /* Calm, desaturated palette */
    --bg:#f5f6f8; --bg2:#eef1f5; --card:#ffffff;
    --ink:#101828; --ink2:#344054; --muted:#667085;
    --line:#e5e7eb;
    --brand:#3b82f6; --ok:#16a34a; --warn:#f59e0b; --danger:#ef4444;
    --shadow:0 10px 26px rgba(2,6,23,.08);
  }
  .content        { background:var(--bg); }
  .content-header h1{ font-family:'Inter',system-ui; font-weight:800; color:var(--ink); letter-spacing:.2px; }

  /* Card */
  .box.box-success{
    background:var(--card);
    border:1px solid var(--line);
    border-radius:16px;
    box-shadow:var(--shadow);
    overflow:hidden;
  }
  .box.box-success .box-header{
    background:linear-gradient(180deg, var(--bg2), #fff);
    border-bottom:1px solid var(--line);
  }
  .box.box-success .box-title{ font-weight:800; color:var(--ink); }

  /* Typography + form */
  body, .content-wrapper, .box, label, .table, input, select, textarea, .btn{
    font-family:'Inter', system-ui, Segoe UI, Roboto, Helvetica, Arial;
    color:var(--ink2);
  }
  .form-control{
    height:42px; border:1px solid var(--line); border-radius:12px;
    box-shadow:none !important; outline:none !important;
  }
  .form-control:focus{ border-color:#c7d2fe; box-shadow:0 0 0 3px rgba(59,130,246,.12); }
  textarea.form-control{ height:auto; min-height:96px; padding-top:10px; }

  /* Input with icon */
  .input-group-addon{
    background:#fff; border:1px solid var(--line); border-right:0; color:#8a94a6; border-radius:12px 0 0 12px;
  }
  .with-icon .form-control{ border-left:0; border-radius:0 12px 12px 0; }
  .errorStar{ color:var(--danger); }

  /* Buttons */
  .btn{ border-radius:10px; border:none; font-weight:700; }
  .btn-success{ background:var(--ok); }
  .btn-warning{ background:var(--warn); color:#111827; }
  .btn-danger { background:var(--danger); }
  .btn-primary{ background:var(--brand); }
  .btn:hover{ filter:brightness(.97); }

  /* Alerts */
  .alert{ border:none; border-radius:12px; box-shadow:0 10px 22px rgba(2,6,23,.10); }
  .alert-success{ background:#ecfdf5; color:#065f46; }
  .alert-warning{ background:#fff7ed; color:#7c2d12; }
  .alert-danger { background:#fef2f2; color:#7f1d1d; }

  /* Avatar preview */
  #output{
    width:110px;height:110px;border-radius:14px;object-fit:cover;
    border:1px dashed var(--line); background:#fff; box-shadow:0 8px 18px rgba(2,6,23,.06);
  }

  /* Table */
  .table thead th{
    background:#fafafa; color:var(--ink); font-weight:800;
    border-bottom:1px solid var(--line) !important;
  }
  .table td, .table th{ vertical-align:middle !important; }
  .table>tbody>tr:hover{ background:#f9fafb; }
  .photo_img_round{ border-radius:50%; border:2px solid #fff; box-shadow:0 6px 16px rgba(2,6,23,.08); }

  /* Modal */
  .modal-content{ border:1px solid var(--line); border-radius:16px; overflow:hidden; box-shadow:0 20px 44px rgba(2,6,23,.18); }
  .green_header{
    background:linear-gradient(180deg,#eef8f1,#ffffff);
    border-bottom:1px solid var(--line);
    color:#065f46;
  }
  .model_view .model_title{ font-weight:800; margin-top:8px; color:var(--ink); }
</style>

<!-- Main content -->
<section class="content">
  <div class="row">
    <div class="col-md-12">

      <!-- Email exists alert -->
      <?php if($__alert) { ?>
        <div class="alert alert-warning alert-dismissable">
          <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
          <h4 style="margin:0 0 6px;"><i class="icon fa fa-exclamation-triangle"></i> <?= e($_data['email_exist_warning']); ?></h4>
          <?= e($_data['email_exist']); ?>
        </div>
      <?php } ?>

      <!-- Admin form -->
      <div class="box box-success">
        <div class="box-header">
          <h3 class="box-title"><i class="fa fa-user-plus"></i> <?= e($_data['page_form']); ?></h3>
        </div>

        <form onsubmit="return validateMe();" action="<?= e($form_url); ?>" method="post" enctype="multipart/form-data">
          <div class="box-body row" style="padding:16px;">

            <div class="form-group col-md-6">
              <label for="txtAdminEmail"><span class="errorStar">*</span> <?= e($_data['label_email']); ?> :</label>
              <div class="input-group with-icon">
                <span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
                <input type="email" name="txtAdminEmail" id="txtAdminEmail" value="<?= e($email); ?>" class="form-control" placeholder="e.g. admin@company.com" required />
              </div>
            </div>

            <div class="form-group col-md-6">
              <label for="txtAdminName"><span class="errorStar">*</span> <?= e($_data['label_name']); ?> :</label>
              <div class="input-group with-icon">
                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                <input type="text" name="txtAdminName" id="txtAdminName" value="<?= e($name); ?>" class="form-control" placeholder="Full name" />
              </div>
            </div>

            <div class="form-group col-md-6">
              <label for="txtAdminContact"><?= e($_data['profile_update_contact']); ?> :</label>
              <div class="input-group with-icon">
                <span class="input-group-addon"><i class="fa fa-phone"></i></span>
                <input type="text" id="txtAdminContact" class="form-control phone" placeholder="+233 20 000 0000" />
              </div>
              <input type="hidden" name="txtAdminContact" id="adminContact" />
            </div>

            <div class="form-group col-md-6">
              <label for="txtAdminPassword"><span class="errorStar">*</span> New Password :</label>
              <div class="input-group with-icon">
                <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                <input type="password" name="txtAdminPassword" id="txtAdminPassword" class="form-control" placeholder="Set a strong password" />
              </div>
              <small class="text-muted" style="display:inline-block;margin-top:6px;">
                Use 8+ characters with a mix of letters & numbers.
              </small>
            </div>

            <!-- Avatar -->
            <div class="form-group col-md-12">
              <label><?= e($_data['label_image']); ?> :</label><br>
              <img src="<?= e($image_rnt); ?>" id="output" alt="Admin preview" />
              <input type="hidden" name="img_exist" value="<?= e($img_track); ?>" />
            </div>
            <div class="form-group col-md-12" style="display:flex;gap:10px;align-items:center;">
              <label class="btn btn-default" style="margin:0;">
                <i class="fa fa-image"></i> <?= e($_data['upload_image']); ?>
                <input type="file" name="uploaded_file" accept="image/*" style="display:none;" onchange="loadFile(event)" />
              </label>
              <span class="text-muted" style="font-size:12px;">JPG / PNG / GIF • A square image looks best</span>
            </div>

            <div class="form-group col-md-12" style="text-align:right;margin-top:6px;">
              <input type="submit" class="btn btn-success" value="<?= e($button_text); ?>"/>
              &nbsp;
              <input type="reset" onclick="window.location.href='<?= WEB_URL; ?>setting/admin.php';" value="<?= e($_data['btn_reset']); ?>" class="btn btn-warning"/>
            </div>

          </div>
          <input type="hidden" name="hdnSpid" value="<?= (int)$hval; ?>"/>
        </form>

        <h4 style="text-align:center; color:#b91c1c; margin: 10px 0 18px;"><?= e($_data['label_warning']); ?></h4>
      </div>

      <!-- Flash alerts for list actions -->
      <?php
        $delinfo = 'none'; $addinfo = 'none'; $msg = "";
        if(isset($_GET['delid']) && $_GET['delid'] != '' && $_GET['delid'] > 0){
          $sqlx= "DELETE FROM `admins` WHERE user_id = ".(int)$_GET['delid'];
          mysqli_query($link,$sqlx); 
          $delinfo = 'block';
        }
        if(isset($_GET['m']) && $_GET['m'] == 'add'){ $addinfo = 'block'; $msg = $_data['save_success']; }
        if(isset($_GET['m']) && $_GET['m'] == 'up') { $addinfo = 'block'; $msg = $_data['save_update']; }
      ?>

      <div class="alert alert-danger alert-dismissable" style="display:<?= $delinfo; ?>;margin-top:8px;">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
        <h4 style="margin:0 0 6px;"><i class="icon fa fa-trash"></i> <?= e($_data['delete_text']); ?>!</h4>
        <?= e($_data['delete_success']); ?>
      </div>

      <div class="alert alert-success alert-dismissable" style="display:<?= $addinfo; ?>;">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
        <h4 style="margin:0 0 6px;"><i class="icon fa fa-check"></i> <?= e($_data['success']); ?>!</h4>
        <?= e($msg); ?>
      </div>

      <!-- Admin list -->
      <div class="box box-success">
        <div class="box-header">
          <h3 class="box-title"><i class="fa fa-list"></i> <?= e($_data['admin_list']); ?></h3>
        </div>
        <div class="box-bodry" style="padding:12px;">
          <table class="table sakotable table-bordered table-striped dt-responsive">
            <thead>
              <tr>
                <th><?= e($_data['column_0']); ?></th>
                <th><?= e($_data['column_1']); ?></th>
                <th><?= e($_data['column_2']); ?></th>
                <th><?= e($_data['column_33']); ?></th>
                <th><?= e($_data['column_4']); ?></th>
              </tr>
            </thead>
            <tbody>
              <?php
                $result = $link->query("SELECT * FROM admins ORDER BY `name` DESC");
                while($row = mysqli_fetch_array($result)){
                  $image = WEB_URL . 'img/no_image.jpg';	
                  if($row['image'] != '' && file_exists(ROOT_PATH . '/img/upload/' . $row['image'])){
                    $image = WEB_URL . 'img/upload/' . $row['image'];
                  }
              ?>
              <tr>
                <td><img class="photo_img_round" style="width:50px;height:50px;" src="<?= e($image); ?>" alt="admin photo"/></td>
                <td><?= e($row['name']); ?></td>
                <td><?= e($row['email']); ?></td>
                <td><?= e($row['contact']); ?></td>
                <td>
                  <div class="btn-group">
                    <a class="btn btn-success" data-toggle="tooltip" href="javascript:;" onclick="$('#employee_view_<?= (int)$row['user_id']; ?>').modal('show');" title="View">
                      <i class="fa fa-eye"></i>
                    </a>
                    <a class="btn btn-warning" data-toggle="tooltip" href="<?= WEB_URL;?>setting/admin.php?spid=<?= (int)$row['user_id']; ?>" title="Edit">
                      <i class="fa fa-pencil"></i>
                    </a>
                    <a class="btn btn-danger" data-toggle="tooltip" onclick="deleteAdmin(<?= (int)$row['user_id'] ?>);" href="javascript:;" title="Delete">
                      <i class="fa fa-trash"></i>
                    </a>
                  </div>

                  <!-- View Modal -->
                  <div id="employee_view_<?= (int)$row['user_id']; ?>" class="modal fade emp-modal" tabindex="-1" role="dialog" aria-labelledby="empTitle_<?= (int)$row['user_id']; ?>" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">

      <!-- Header -->
      <div class="modal-header emp-header">
        <h3 class="modal-title" id="empTitle_<?= (int)$row['user_id']; ?>">
          <span class="emp-title-icon"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
          <?= e($_data['admin_details']); ?>
        </h3>
        <button aria-label="Close" data-dismiss="modal" class="close emp-close" type="button">
          <span aria-hidden="true"><i class="fa fa-close"></i></span>
        </button>
      </div>

      <!-- Profile -->
      <div class="modal-body">
        <div class="emp-profile">
          <img class="emp-avatar" src="<?= e($image); ?>" alt="<?= e($row['name']); ?> photo">
          <div class="emp-name"><?= e($row['name']); ?></div>
          <div class="emp-meta">
            <span class="chip chip-id" title="Internal ID">
              <i class="fa fa-hashtag" aria-hidden="true"></i> <?= (int)$row['user_id']; ?>
            </span>
          </div>
        </div>

        <!-- Details -->
        <h4 class="emp-section-title">
          <i class="fa fa-list-ul" aria-hidden="true"></i>
          <?= e($_data['details_information']); ?>
        </h4>

        <div class="emp-grid">
          <div class="emp-kv">
            <div class="emp-k"><i class="fa fa-user" aria-hidden="true"></i> <?= e($_data['column_1']); ?>:</div>
            <div class="emp-v"><?= e($row['name']); ?></div>
          </div>

          <div class="emp-kv">
            <div class="emp-k"><i class="fa fa-envelope" aria-hidden="true"></i> <?= e($_data['column_2']); ?>:</div>
            <div class="emp-v emp-v-inline">
              <a href="mailto:<?= e($row['email']); ?>" class="emp-link"><?= e($row['email']); ?></a>
              <div class="emp-actions">
                <a href="mailto:<?= e($row['email']); ?>" class="btn btn-xs btn-light" title="Send Email">
                  <i class="fa fa-paper-plane" aria-hidden="true"></i>
                </a>
                <button type="button" class="btn btn-xs btn-light emp-copy" data-copy="<?= e($row['email']); ?>" title="Copy Email">
                  <i class="fa fa-copy" aria-hidden="true"></i>
                </button>
              </div>
            </div>
          </div>

          <div class="emp-kv">
            <div class="emp-k"><i class="fa fa-phone" aria-hidden="true"></i> <?= e($_data['column_33']); ?>:</div>
            <div class="emp-v emp-v-inline">
              <a href="tel:<?= e($row['contact']); ?>" class="emp-link"><?= e($row['contact']); ?></a>
              <div class="emp-actions">
                <a href="tel:<?= e($row['contact']); ?>" class="btn btn-xs btn-light" title="Call">
                  <i class="fa fa-phone-square" aria-hidden="true"></i>
                </a>
                <button type="button" class="btn btn-xs btn-light emp-copy" data-copy="<?= e($row['contact']); ?>" title="Copy Number">
                  <i class="fa fa-copy" aria-hidden="true"></i>
                </button>
              </div>
            </div>
          </div>
        </div>

      </div>

      <!-- Footer -->
      <div class="modal-footer emp-footer">
        <a href="mailto:<?= e($row['email']); ?>" class="btn btn-primary btn-pill">
          <i class="fa fa-envelope-o" aria-hidden="true"></i> Email
        </a>
        <a href="tel:<?= e($row['contact']); ?>" class="btn btn-success btn-pill">
          <i class="fa fa-phone" aria-hidden="true"></i> Call
        </a>
        <button type="button" class="btn btn-ghost btn-pill" data-dismiss="modal">
          <i class="fa fa-close" aria-hidden="true"></i> Close
        </button>
      </div>

    </div>
  </div>
</div>

<!-- Scoped Styles (only affect this modal) -->
<style>
  .emp-modal .modal-content{
    border-radius:16px; border:1px solid #e5e7eb; box-shadow:0 24px 60px rgba(2,6,23,.12);
    font-family:"Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; color:#0f172a;
  }
  .emp-header{
    display:flex; align-items:center; justify-content:space-between;
    padding:14px 18px; background:linear-gradient(135deg, #10b981, #059669);
    color:#fff; border-bottom:1px solid rgba(255,255,255,.15);
  }
  .emp-title-icon{
    display:inline-flex; align-items:center; justify-content:center; width:34px; height:34px;
    border-radius:999px; background:rgba(255,255,255,.15); margin-right:10px;
  }
  .emp-header .modal-title{ margin:0; font-weight:800; letter-spacing:.2px; display:flex; align-items:center; }
  .emp-close{ opacity:.9; color:#fff; }
  .emp-close:hover{ opacity:1; }

  .emp-profile{
    display:flex; flex-direction:column; align-items:center; gap:8px;
    border:1px dashed #e5e7eb; border-radius:14px; padding:16px; background:#f8fafc; margin-bottom:14px;
  }
  .emp-avatar{
    width:110px; height:110px; border-radius:50%; object-fit:cover; border:3px solid #eef2ff; box-shadow:0 8px 20px rgba(2,6,23,.08);
  }
  .emp-name{ font-weight:900; letter-spacing:.2px; color:#0f172a; font-size:18px; }
  .emp-meta{ display:flex; gap:8px; margin-top:2px; }
  .chip{
    display:inline-flex; align-items:center; gap:6px; padding:6px 10px; border-radius:999px; font-weight:800; font-size:12px;
    border:1px solid transparent; background:#eef2ff; color:#3730a3; border-color:#e0e7ff;
  }

  .emp-section-title{
    display:flex; align-items:center; gap:8px; font-weight:800; color:#0f172a; margin:14px 0 12px; letter-spacing:.2px;
  }

  .emp-grid{ display:grid; grid-template-columns:1fr; gap:10px; }
  .emp-kv{ display:grid; grid-template-columns:200px 1fr; gap:10px; align-items:center; }
  @media (max-width: 600px){ .emp-kv{ grid-template-columns:1fr; gap:4px; } }

  .emp-k{
    color:#64748b; font-weight:700; text-transform:uppercase; letter-spacing:.3px; font-size:11px;
    display:flex; align-items:center; gap:6px;
  }
  .emp-v{ font-weight:800; color:#0f172a; }
  .emp-v.emp-v-inline{ display:flex; align-items:center; justify-content:space-between; gap:10px; }
  .emp-link{ color:#2563eb; text-decoration:none; word-break:break-all; }
  .emp-link:hover{ text-decoration:underline; }

  .emp-actions{ display:flex; gap:6px; }
  .btn.btn-xs{ padding:4px 8px; font-size:12px; line-height:1.2; border-radius:999px; }
  .btn-light{ background:#fff; border:1px solid #e5e7eb; color:#0f172a; }
  .btn-light:hover{ background:#f8fafc; }

  .emp-footer{ display:flex; justify-content:flex-end; gap:8px; background:#fbfdff; border-top:1px solid #e5e7eb; }
  .btn-pill{ border-radius:999px; }
  .btn-ghost{ background:#fff; border:1px solid #e5e7eb; color:#0f172a; }
  .btn-ghost:hover{ background:#f8fafc; }
</style><!-- /.modal -->
                </td>
              </tr>
              <?php } mysqli_close($link); $link = NULL; ?>
            </tbody>
          </table>
        </div>
      </div>

    </div><!-- /.col -->
  </div><!-- /.row -->
</section>

<script>
  // Gentle validation (logic unchanged)
  function validateMe() {
    if($("#txtAdminEmail").val().trim() === ''){
      alert('<?= e($_data['required_1']); ?>');
      $("#txtAdminEmail").focus();
      return false;
    } else if($("#txtAdminName").val().trim() === ''){
      alert('<?= e($_data['required_2']); ?>');
      $("#txtAdminName").focus();
      return false;
    } else if($("#txtAdminPassword").val().trim() === ''){
      alert('<?= e($_data['required_3']); ?>');
      $("#txtAdminPassword").focus();
      return false;
    } else {
      try {
        let iti = window.intlTelInputGlobals.getInstance($("#txtAdminContact")[0]);
        $("#adminContact").val(iti ? iti.getNumber() : $("#txtAdminContact").val());
      } catch(e){}
      return true;
    }
  }

  function deleteAdmin(Id){
    var iAnswer = confirm("<?= e($_data['confirm_delete']); ?>");
    if(iAnswer){
      window.location = '<?= WEB_URL;?>setting/admin.php?delid=' + Id;
    }
  }

  // Live preview avatar (logic unchanged)
  function loadFile(event){
    const img = document.getElementById('output');
    if(event.target.files && event.target.files[0]){
      img.src = URL.createObjectURL(event.target.files[0]);
      img.onload = function(){ URL.revokeObjectURL(img.src); }
    }
  }

  // Auto-dim alerts
  $(function(){
    setTimeout(function(){ $('.alert:visible').fadeOut(300); }, 3500);
  });


  (function(){
    var $modal = $('#employee_view_<?= (int)$row['user_id']; ?>');
    $modal.on('click', '.emp-copy', function(){
      var text = $(this).data('copy');
      if (!text) return;
      var ta = document.createElement('textarea');
      ta.value = text;
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand('copy'); } catch(e){}
      document.body.removeChild(ta);

      // Tiny feedback pulse
      var $btn = $(this);
      $btn.addClass('copied');
      setTimeout(function(){ $btn.removeClass('copied'); }, 600);
    });
  })();

</script>

<?php include_once('../footer.php'); ?>
