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

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

/* ================= LOGIN ================= */

if(!isset($_SESSION['login'])){
    header("Location: /login.php");
    exit;
}

if($_SESSION['role'] != 'admin3'){
    header("Location: /login.php");
    exit;
}
/* ================= TERIMA LAPORAN ================= */

if(isset($_GET['terima'])){

    $id = (int)$_GET['terima'];

    $conn->query("
    UPDATE rekomtek
    SET status='Diterima'
    WHERE id='$id'
    ");

    header("Location: laporan_diterima.php");
    exit;
}

/* ================= FILTER ================= */

$where = "WHERE status='Menunggu'";

$status = $_GET['status'] ?? '';

if($status != ''){
    $status = $conn->real_escape_string($status);
    $where .= " AND status='$status'";
}

/* ================= TOTAL ================= */

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

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

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

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

$selesai = $conn->query("
SELECT COUNT(*) as jml 
FROM rekomtek
WHERE status='Selesai'
")->fetch_assoc()['jml'];

/* ================= SEARCH ================= */

if(isset($_GET['search']) && $_GET['search'] != ''){

    $search = $conn->real_escape_string($_GET['search']);

    $where .= " AND kode_tiket LIKE '%$search%'";
}

/* ================= FILTER TANGGAL ================= */

if(isset($_GET['filter']) && $_GET['filter'] != ''){

    $filter = $_GET['filter'];

    if($filter == "hari" && !empty($_GET['tanggal'])){

        $tgl = $_GET['tanggal'];

        $where .= " AND DATE(created_at)='$tgl'";
    }

    elseif($filter == "minggu"){

        $where .= " 
        AND YEARWEEK(created_at,1)=YEARWEEK(CURDATE(),1)";
    }

    elseif($filter == "bulan"){

        $where .= "
        AND MONTH(created_at)=MONTH(CURDATE())
        AND YEAR(created_at)=YEAR(CURDATE())";
    }

    elseif($filter == "tahun"){

        $where .= "
        AND YEAR(created_at)=YEAR(CURDATE())";
    }
}



/* ================= DATA ================= */

$data = $conn->query("
SELECT *
FROM rekomtek
$where
ORDER BY id DESC

");
?>

<!DOCTYPE html>
<html lang="id">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Monitoring Rekomtek</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet"
href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

body{
    background:#f4f6f9;
    font-family:'Segoe UI',sans-serif;
}

.sidebar{
    width:250px;
    height:100vh;
    position:fixed;
    left:0;
    top:0;
    background:#003b7a;
    padding-top:20px;
}

.sidebar h4{
    color:white;
    text-align:center;
    margin-bottom:30px;
    font-weight:bold;
}

.sidebar a{
    display:block;
    color:white;
    padding:14px 20px;
    text-decoration:none;
}

.sidebar a:hover{
    background:rgba(255,255,255,0.15);
}

.sidebar .badge{
    float:right;
}

.main{
    margin-left:250px;
    padding:30px;
}

.card-box{
    border-radius:15px;
    color:white;
    padding:20px;
}

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

</style>

</head>

<body>

<!-- SIDEBAR -->

<div class="sidebar">

<h4>📁 Admin Laporan Triwulan</h4>


<a href="laporan_masuk">
<i class="fa fa-file-alt"></i>
Laporan Masuk
<span class="badge bg-warning text-dark">
<?= $menunggu ?>
</span>
</a>

<a href="laporan_ditolak">
<i class="fa fa-times-circle"></i>
Laporan Ditolak
<span class="badge bg-danger">
<?= $ditolak ?>
</span>
</a>

<a href="laporan_diterima">
<i class="fa fa-check-circle"></i>
Laporan Diterima
<span class="badge bg-success">
<?= $diterima ?>
</span>
</a>

<a href="rekap_triwulan">

<div class="menu-left">
<i class="fa fa-clipboard-list"></i>
Laporan Triwulan

</div>

</a>

<a href="masa_berlaku_sk">
<i class="fa fa-calendar-times"></i>
SK Perlu Perpanjangan 
</a>

<a href="master_sk">
<i class="fa fa-database"></i>
Master SK
</a>

<a href="/login">
<i class="fa fa-sign-out-alt"></i>
Logout
</a>

</div>

<!-- MAIN -->

<div class="main">

<h3 class="mb-4 fw-bold">
📋 Monitoring Laporan Rekomtek
</h3>

<!-- CARD -->

<div class="row mb-4">

<div class="col-md-3">
<div class="card-box bg-primary shadow">
<h6>Total Laporan</h6>
<h2><?= $total ?></h2>
</div>
</div>

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

<div class="col-md-3">
<div class="card-box bg-success shadow">
<h6>Selesai</h6>
<h2><?= $selesai ?></h2>
</div>
</div>

<div class="col-md-3">
<div class="card-box bg-danger shadow">
<h6>Ditolak</h6>
<h2><?= $ditolak ?></h2>
</div>
</div>

</div>

<!-- FILTER -->

<form method="GET" class="row mb-3">

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

<div class="col-md-2">
<select name="filter" class="form-control">

<option value="">Semua</option>

<option value="hari">Harian</option>
<option value="minggu">Mingguan</option>
<option value="bulan">Bulanan</option>
<option value="tahun">Tahunan</option>

</select>
</div>

<div class="col-md-2">
<input type="date"
name="tanggal"
class="form-control">
</div>

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

</form>

<!-- TABLE -->

<div class="card shadow border-0">

<div class="card-body">

<div class="table-responsive">

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

<thead class="table-dark text-center">

<tr>

<th>No</th>
<th>Kode Tiket</th>
<th>Nomor SK</th>
<th>Sumber Air</th>
<th>Nama Perusahaan</th>
<th>Triwulan</th>
<th>Tahun</th>
<th>Status</th>
<th>Tanggal</th>
<th width="180">Aksi</th>

</tr>

</thead>

<tbody>

<?php
$no =  1;

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

<tr>

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

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

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

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

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

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

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

<td class="text-center">

<?php if($d['status'] == 'Menunggu'){ ?>

<span class="badge bg-warning text-dark">
Menunggu
</span>

<?php } elseif($d['status'] == 'Diterima'){ ?>

<span class="badge bg-primary">
Diterima
</span>

<?php } elseif($d['status'] == 'Selesai'){ ?>

<span class="badge bg-success">
Selesai
</span>

<?php } else { ?>

<span class="badge bg-danger">
Ditolak
</span>

<?php } ?>

</td>

<td>
<?= date('d-m-Y', strtotime($d['created_at'])) ?>
</td>

<td>

<div class="d-grid gap-2">

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

Lihat

</a>



</div>

</td>

</tr>

<!-- MODAL -->

<div class="modal fade"
id="modalTolak<?= $d['id'] ?>"
tabindex="-1">

<div class="modal-dialog">

<div class="modal-content">

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

<div class="modal-header bg-danger text-white">

<h5 class="modal-title">
Tolak Laporan
</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="catatan"
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 endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script src="https://cdn.datatables.net/1.13.8/js/jquery.dataTables.min.js"></script>

<script src="https://cdn.datatables.net/1.13.8/js/dataTables.bootstrap5.min.js"></script>

<script>
$(document).ready(function(){

    $('#tabelRekomtek').DataTable({
        "order": [[8, "desc"]], // kolom tanggal
        "pageLength": 10,
        "lengthMenu": [10, 25, 50, 100],
        "language": {
            "search": "Cari:",
            "lengthMenu": "Tampilkan _MENU_ data",
            "info": "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
            "paginate": {
                "previous": "Sebelumnya",
                "next": "Berikutnya"
            }
        }
    });

});
</script>
</body>
</html>