<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require "../config.php";
/* ================= FILTER ================= */
$where = "WHERE status='Diterima' 
AND (status_admin2 IS NULL OR status_admin2 != 'Sudah TNDE')";

/* ===== SEARCH ===== */
if(isset($_GET['search']) && $_GET['search'] != ""){
    $s = $conn->real_escape_string($_GET['search']);
    $where .= " AND kode_tiket LIKE '%$s%'";
}

/* ===== FILTER TANGGAL ===== */
if(isset($_GET['filter']) && $_GET['filter'] != ""){

    $filter = $_GET['filter'];

    if($filter == "hari" && !empty($_GET['tanggal'])){
        $tgl = $_GET['tanggal'];
        $where .= " AND DATE(tanggal_masuk) = '$tgl'";
    }

    elseif($filter == "minggu"){
        $where .= " AND YEARWEEK(tanggal_masuk,1)=YEARWEEK(CURDATE(),1)";
    }

    elseif($filter == "bulan"){
        $where .= " AND MONTH(tanggal_masuk)=MONTH(CURDATE()) 
                   AND YEAR(tanggal_masuk)=YEAR(CURDATE())";
    }

    elseif($filter == "tahun"){
        $where .= " AND YEAR(tanggal_masuk)=YEAR(CURDATE())";
    }
}
/* ================= PAGINATION ================= */

$limit = 10;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

if($page < 1){
    $page = 1;
}

$offset = ($page - 1) * $limit;

/* TOTAL DATA */
$totalData = $conn->query("
SELECT COUNT(*) as total 
FROM pengajuan 
$where
")->fetch_assoc()['total'];

$totalPage = ceil($totalData / $limit);

/* DATA */
$data = $conn->query("
SELECT * FROM pengajuan 
$where 
ORDER BY id DESC 
LIMIT $limit OFFSET $offset
");
?>

<?php include '../admin/layout'; ?>

<h3 class="mb-3">📨 Input TNDE </h3>
<!-- FILTER -->
<form method="GET" class="row mb-3">

    <div class="col-md-3">
        <input type="text" name="search" class="form-control"
        value="<?= $_GET['search'] ?? '' ?>" 
        placeholder="Cari No Tiket...">
    </div>

    <div class="col-md-2">
        <select name="filter" class="form-control">
            <option value="">Semua</option>
            <option value="hari" <?= (@$_GET['filter']=="hari")?"selected":"" ?>>Harian</option>
            <option value="minggu" <?= (@$_GET['filter']=="minggu")?"selected":"" ?>>Mingguan</option>
            <option value="bulan" <?= (@$_GET['filter']=="bulan")?"selected":"" ?>>Bulanan</option>
            <option value="tahun" <?= (@$_GET['filter']=="tahun")?"selected":"" ?>>Tahunan</option>
        </select>
    </div>

    <div class="col-md-2">
        <input type="date" name="tanggal" class="form-control" value="<?= $_GET['tanggal'] ?? '' ?>">
    </div>

    <div class="col-md-2">
        <button class="btn btn-primary w-100">Cari</button>
    </div>

</form>

<div class="card shadow">
<div class="card-body">

<div class="table-responsive">

<table class="table table-bordered table-striped align-middle">

<thead class="table-dark text-center">
<tr>
<th>No</th>
<th>No Tiket</th>
<th>Email</th>
<th>Nama</th>
<th>Status</th>
<th>Tanggal</th>
<th>Aksi</th>
</tr>
</thead>

<tbody>

<?php $no=1; while($d=$data->fetch_assoc()){ ?>

<tr>

<td><?= $no++ ?></td>

<td><?= $d['kode_tiket'] ?></td>

<td><?= $d['email'] ?></td>

<td><?= $d['nama'] ?></td>

<td>
<span class="badge bg-success">
Diterima
</span>
</td>

<td>
<?= date('d-m-Y H:i', strtotime($d['tanggal_masuk'])) ?>
</td>

<td>

<div class="d-flex flex-column gap-2">

<!-- LIHAT -->
<a href="../admin/lihat?id=<?= $d['id'] ?>&from=admin2"
class="btn btn-primary btn-sm">
Lihat
</a>

<!-- TNDE -->
<button
class="btn btn-warning btn-sm"
data-bs-toggle="modal"
data-bs-target="#modal<?= $d['id'] ?>">

TNDE

</button>

</div>

</td>

</tr>

<!-- MODAL -->
<div class="modal fade" id="modal<?= $d['id'] ?>">

<div class="modal-dialog modal-xl modal-dialog-scrollable">

<div class="modal-content">

<form method="POST" action="proses_tnde">

<div class="modal-header bg-warning">

<h5 class="modal-title">
Input TNDE
</h5>

<button
type="button"
class="btn-close"
data-bs-dismiss="modal">
</button>

</div>

<div class="modal-body">

<input
type="hidden"
name="id"
value="<?= $d['id'] ?>">

<!-- NOMOR SURAT -->
<div class="mb-3">

<label class="form-label fw-bold">
Nomor Surat
</label>

<input
type="text"
name="no_surat"
class="form-control"
required
placeholder="Masukkan nomor surat...">

</div>

<!-- DISPOSISI CHECKBOX -->
<div class="mb-3">

<label class="form-label fw-bold">
Disposisi <span class="text-danger">*</span>
</label>

<!-- PILIH SEMUA -->
<div class="form-check mb-2">

    <input 
        class="form-check-input" 
        type="checkbox" 
        id="checkAll<?= $d['id'] ?>">

    <label 
        class="form-check-label fw-bold text-primary"
        for="checkAll<?= $d['id'] ?>">
        Pilih Semua
    </label>

</div>

<hr>

<?php
$daftar_disposisi = [

"Kepala Subbagian Umum dan Tata Usaha - Fajar Andriang, S.IP., M.T.",

"Kepala Seksi Keterpaduan Pembangunan Infrastruktur SDA - Gus Agung Gutoro, S.T., M.T.",

"Kepala Seksi Pelaksanaan - Marthen Duma, S.T.",

"Kepala Seksi Operasi dan Pemeliharaan - Riz Anugerah, S.T., M.T.",

"Analis Tata Laksana - Syahli, S.E.",

"Pengelola SDA Ahli Madya - Adi Sulistyo Utomo, S.T., M.Eng.",

"Pengelola SDA Ahli Pertama - Lina Dwi Damayanti, S.T., M.T.",

"Pengelola SDA Ahli Madya - Lesty Arlenisianti, S.T., M.Eng.",

"Analis Pengelolaan SDA - Adi Kusworo, S.T.",

"Pengelola SDA Ahli Madya SNVT - Indrasto Dwicahyo, S.T., MPSDA.",

"Pengelola SDA Ahli Muda - Muhammad Lugman Nurhakim, S.T.",

"PPK Mandiri - Herry Sulistyo, S.E., M.Si.",

"PPK Mandiri - Nani Lazuarni, S.T.",

"Pengelola SDA Ahli Madya - Ir. Andy Supriyatna, M.T."

];

foreach($daftar_disposisi as $i => $x):

$idCheckbox = "disp".$d['id'].$i;
?>

<div class="form-check mb-2">

    <input
        class="form-check-input disposisi<?= $d['id'] ?>"
        type="checkbox"
        id="<?= $idCheckbox ?>"
        name="disposisi[]"
        value="<?= $x ?>">

    <label 
        class="form-check-label"
        for="<?= $idCheckbox ?>">
        <?= $x ?>
    </label>

</div>

<?php endforeach; ?>

</div>

</div>

<div class="modal-footer">

<button class="btn btn-success">
Simpan
</button>

</div>

</form>

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

<?php } ?>
<script>

document.querySelectorAll('[id^="checkAll"]').forEach(function(checkAll){

    checkAll.addEventListener('change', function(){

        let id = this.id.replace('checkAll','');

        let checkboxes = document.querySelectorAll('.disposisi'+id);

        checkboxes.forEach(function(cb){
            cb.checked = checkAll.checked;
        });

    });

});

</script>
</tbody>
</table>
<?php if($totalPage > 1){ ?>

<nav class="mt-3">
<ul class="pagination justify-content-center">

<?php if($page > 1){ ?>
<li class="page-item">
<a class="page-link" href="?page=<?= $page-1 ?>">
Previous
</a>
</li>
<?php } ?>

<?php for($i=1; $i <= $totalPage; $i++){ ?>

<li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
<a class="page-link" href="?page=<?= $i ?>">
<?= $i ?>
</a>
</li>

<?php } ?>

<?php if($page < $totalPage){ ?>
<li class="page-item">
<a class="page-link" href="?page=<?= $page+1 ?>">
Next
</a>
</li>
<?php } ?>

</ul>
</nav>

<?php } ?>
</div>
</div>
</div>

<?php include '../admin/layout_footer.php'; ?>