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

session_start();
require "../config.php";

/* ================= FILTER ================= */
$where = "WHERE status='Menunggu'";

$total = $conn->query("SELECT COUNT(*) as jml FROM pengajuan")->fetch_assoc()['jml'];

$diterima = $conn->query("SELECT COUNT(*) as jml FROM pengajuan WHERE status='Diterima'")->fetch_assoc()['jml'];

$ditolak = $conn->query("SELECT COUNT(*) as jml FROM pengajuan WHERE status='Ditolak'")->fetch_assoc()['jml'];

$menunggu = $conn->query("SELECT COUNT(*) as jml FROM pengajuan WHERE status='Menunggu'")->fetch_assoc()['jml'];

/* ===== SEARCH NO TIKET ===== */
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 'layout.php'; ?>

<h3 class="mb-3">📥 Surat Masuk</h3>

<div class="row mb-3">

<div class="col-md-3">
<div class="card bg-primary text-white shadow p-3">
<h6>Total Surat</h6>
<h3><?= $total ?></h3>
</div>
</div>

<div class="col-md-3">
<div class="card bg-success text-white shadow p-3">
<h6>Diterima</h6>
<h3><?= $diterima ?></h3>
</div>
</div>

<div class="col-md-3">
<div class="card bg-danger text-white shadow p-3">
<h6>Ditolak</h6>
<h3><?= $ditolak ?></h3>
</div>
</div>

<div class="col-md-3">
<div class="card bg-warning text-dark shadow p-3">
<h6>Menunggu</h6>
<h3><?= $menunggu ?></h3>
</div>
</div>

</div>
<!-- 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>

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

<table class="table table-bordered table-striped">
<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-warning text-dark">Menunggu</span>
</td>

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

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

<a href="lihat?id=<?= $d['id'] ?>" class="btn btn-primary btn-sm">Lihat</a>



</div>
</td>

</tr>

<!-- MODAL TOLAK -->
<div class="modal fade" id="modalTolak<?= $d['id'] ?>" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">

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

        <div class="modal-header bg-danger text-white">
          <h5 class="modal-title">Tolak Surat</h5>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
        </div>

        <div class="modal-body">
          <input type="hidden" name="id" value="<?= $d['id'] ?>">

          <textarea name="pesan" class="form-control" rows="4" required
          placeholder="Masukkan alasan penolakan..."></textarea>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-danger">Tolak</button>
        </div>

      </form>

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

<?php } ?>

</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>

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