<?php
if (isset($_POST['edit']) || isset($_POST['hapusfoto'])) {
	update_user();
}

global $koneksi;
$detail_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($detail_id > 0) {
	$detail_user = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM tb_user WHERE id='$detail_id'"));
	if (!$detail_user) { echo '<script>window.location.href="index.php?m=admin&s=title";</script>'; exit; }
} else {
	$detail_user = null;
}
?>
<?php include 'comp/header.php'; ?>
<?php if ($detail_user) { $key = $detail_user; } ?>
<?php $judul = "Detail User"; ?>
   <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="page-header">
          <div>
            <h1>Detail Pengguna</h1>
            <ul class="page-breadcrumb">
              <li><a href="index.php">Home</a></li>
              <li><span class="sep">›</span></li>
              <li><a href="index.php?m=admin&s=title">Pengguna</a></li>
              <li><span class="sep">›</span></li>
              <li><?= $key['nama']; ?></li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    <section class="content">
      <div class="container-fluid">
        <div class="table-responsive">
          <table class="table table-borderless">
            <tbody>
              <tr>
                <td style="font-weight:600;width:150px;">Nama</td>
                <td><?= $key['nama']; ?></td>
              </tr>
              <tr>
                <td style="font-weight:600;">Username</td>
                <td><?= $key['username']; ?></td>
              </tr>
              <tr>
                <td style="font-weight:600;">Level User</td>
                <td>
                  <?php if ($key['level']=="admin"): ?>
                    <span class="badge badge-level-admin"><i class="fas fa-shield-alt" style="font-size:10px;"></i> Admin</span>
                  <?php elseif ($key['level']=="kasir"): ?>
                    <span class="badge badge-level-kasir"><i class="fas fa-cash-register" style="font-size:10px;"></i> Kasir</span>
                  <?php elseif ($key['level']=="logistik"): ?>
                    <span class="badge badge-level-logistik"><i class="fas fa-truck" style="font-size:10px;"></i> Logistik</span>
                  <?php elseif ($key['level']=="gudang"): ?>
                    <span class="badge badge-level-gudang"><i class="fas fa-warehouse" style="font-size:10px;"></i> Gudang</span>
                  <?php else: ?>
                    <span class="badge"><?= $key['level']; ?></span>
                  <?php endif; ?>
                </td>
              </tr>
              <tr>
                <td style="font-weight:600;">Foto</td>
                <td>
                  <?php if ($key['foto']!= ''): ?>
                    <img src="img/<?= $key['foto'] ?>" data-bs-target="#view_image" data-bs-toggle="modal" height="150" style="cursor:pointer;border-radius:8px;">
                  <?php else: ?>
                    <img src="img/user_logo.png" height="150" style="border-radius:8px;">
                  <?php endif; ?>
                  <!-- modal img -->
                  <div class="modal fade" id="view_image" tabindex="-1" role="dialog" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                      <div class="modal-content"><div class="modal-body" style="text-align:center;">
                        <img src="img/<?= $key['foto'];?>" style="max-width:100%;max-height:70vh;">
                      </div></div>
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <td style="font-weight:600;">Aksi</td>
                <td>
                  <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#edit-profil<?= $key['id'] ?>">
                    <i class="fas fa-edit"></i> Edit Pengguna
                  </button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </section>
  </div>

<?php
$is_admin_locked = ($key['level'] == 'admin');
?>
<!-- MODAL EDIT PENGGUNA (Outside wrapper) -->
<div class="modal fade" id="edit-profil<?= $key['id'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><i class="fas fa-edit" style="color:var(--primary);margin-right:8px;"></i><?= $is_admin_locked ? 'Profil Admin' : 'Edit Data Pengguna' ?></h5>
        <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
      </div>
      <div class="modal-body">
        <form action="" method="POST" enctype="multipart/form-data">
          <input type="hidden" value="<?= $key['id'] ?>" name="id">
          <?php if ($is_admin_locked): ?>
          <div class="form-group">
            <label>Username</label>
            <input type="text" class="form-control" value="<?= $key['username'];?>" readonly style="background:var(--surface-2);color:var(--text-muted);">
          </div>
          <div class="form-group">
            <label>Nama User</label>
            <input type="text" class="form-control" value="<?= $key['nama'];?>" readonly style="background:var(--surface-2);color:var(--text-muted);">
          </div>
          <div class="form-group">
            <label>Level User</label>
            <input type="text" class="form-control" value="Admin" readonly style="background:var(--surface-2);color:var(--text-muted);font-weight:600;">
          </div>
          <?php else: ?>
          <div class="form-group">
            <label>Username</label>
            <input type="text" class="form-control" value="<?= $key['username'];?>" name="username" placeholder="Masukkan Username">
          </div>
          <div class="form-group">
            <label>Password <span style="font-weight:400;text-transform:none;color:var(--text-muted);">(Kosongkan jika tidak diubah)</span></label>
            <input type="password" class="form-control" name="password" placeholder="Masukkan password baru">
          </div>
          <div class="form-group">
            <label>Nama User</label>
            <input type="text" class="form-control" value="<?= $key['nama'];?>" name="nama" placeholder="Masukkan nama">
          </div>
          <div class="form-group">
            <label>Level User</label>
            <select name="level" class="form-control">
              <option value="kasir" <?= ($key['level']=='kasir') ? 'selected' : '' ?>>Kasir</option>
            </select>
          </div>
          <?php endif; ?>
          <div class="form-group">
            <label>Foto User</label><br>
            <?php if ($key['foto']!= ''): ?>
              <img src="img/<?= $key['foto'] ?>" height="150" style="border-radius:8px;">
            <?php else: ?>
              <img src="img/user_logo.png" height="150" style="border-radius:8px;">
            <?php endif; ?>
          </div>
          <div class="form-group">
            <label>Ganti Foto</label>
            <input type="file" name="foto">
          </div>
          <?php if ($key['foto']!= ''): ?>
          <div class="form-group">
            <button type="button" class="btn btn-sm btn-danger" id="btnHapusFoto<?= $key['id'] ?>" onclick="hapusFoto(this)" style="border-radius:8px;">
              <i class="fas fa-trash-alt"></i> Hapus Foto
            </button>
            <input type="hidden" name="hapusfoto" id="hapusfoto<?= $key['id'] ?>" value="">
          </div>
          <?php endif; ?>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="submit" name="edit" class="btn btn-primary"><i class="fas fa-save"></i> <?= $is_admin_locked ? 'Simpan Foto' : 'Save changes' ?></button>
      </div>
        </form>
    </div>
  </div>
</div>

<script>
function hapusFoto(btn) {
  var id = <?= $key['id'] ?>;
  document.getElementById('hapusfoto' + id).value = '1';
  btn.closest('form').submit();
}
</script>
<?php include 'comp/footer.php'; ?>
