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

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

/* ================= PAGINATION ================= */
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

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

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

/* ================= FILTER ================= */
$where = "WHERE pesan_balasan IS NOT NULL";

/* ===== 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())";
    }
}

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

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

/* ================= 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'; ?>

<style>
.table-responsive{
    overflow-x:auto;
}

.table td,
.table th{
    vertical-align: middle;
    white-space: nowrap;
}

.card{
    border-radius:12px;
}

.pagination .page-link{
    border-radius:8px;
    margin:0 2px;
}
</style>

<h3 class="mb-3">📦 Surat Selesai</h3>

<!-- FILTER -->
<form method="GET" class="row mb-3 g-2">

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

    <div class="col-md-3">
        <a href="export_surat_selesai.php?<?= http_build_query($_GET) ?>" 
        class="btn btn-success w-100">
        Export Excel
        </a>
    </div>

</form>

<!-- TABLE -->
<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>Balasan</th>
<th>Tanggal</th>
<th>Surat Masuk</th>
<th>Surat Keluar</th>
</tr>
</thead>

<tbody>

<?php 
$no = $start + 1;

while($d = $data->fetch_assoc()){ 
?>

<tr>

<td class="text-center"><?= $no++ ?></td>

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

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

<td style="white-space:normal;">
<?= $d['nama'] ?>
</td>

<td style="white-space:normal; max-width:250px;">
<?= nl2br($d['pesan_balasan']) ?>
</td>

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

<td class="text-center">

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

</td>

<td class="text-center">

<?php if(!empty($d['file_balasan'])){ ?>

<a href="../uploads/<?= urlencode($d['file_balasan']) ?>" 
class="btn btn-success btn-sm"
target="_blank">

Download

</a>

<?php } else { ?>

<span class="badge bg-secondary">
Tidak Ada
</span>

<?php } ?>

</td>

</tr>

<?php } ?>

</tbody>
</table>

</div>

<?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 } ?>

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