<?php
include('koneksi.php');
session_start();

if (!isset($_SESSION['status']) || $_SESSION['role'] != "admin") {
    header("location:login.php");
    exit;
}

// --- LOGIKA BUKA/TUTUP FORM (MANUAL VIA FILE) ---
if (isset($_POST['set_status'])) {
    $status_baru = $_POST['status_form'];
    file_put_contents('status_form.txt', $status_baru);
    echo "<script>alert('Status pengajuan berhasil diubah menjadi: " . strtoupper($status_baru) . "'); window.location='admin_dashboard.php';</script>";
}
// Baca status saat ini dari file (default: buka)
$status_sekarang = file_exists('status_form.txt') ? file_get_contents('status_form.txt') : 'buka';


// LOGIKA UPDATE PAPAN INFORMASI
if (isset($_POST['update_papan'])) {
    $info_baru = mysqli_real_escape_string($koneksi, $_POST['isi_info']);
    mysqli_query($koneksi, "UPDATE papan_informasi SET isi_info = '$info_baru' WHERE id = 1");
    echo "<script>alert('Papan informasi berhasil diperbarui!'); window.location='admin_dashboard.php';</script>";
}

// Ambil info saat ini
$info_curr = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT * FROM papan_informasi WHERE id = 1"));

// QUERY STATISTIK
$stat_total = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as jml FROM pengajuan_plagiasi"))['jml'];
$stat_proses = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as jml FROM pengajuan_plagiasi WHERE status_periksa='Proses'"))['jml'];
$stat_selesai = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as jml FROM pengajuan_plagiasi WHERE status_periksa='Selesai'"))['jml'];

// LOGIKA PENCARIAN & PAGINATION
$limit = 10;
$page = isset($_GET['halaman']) ? (int)$_GET['halaman'] : 1;
$start = ($page > 1) ? ($page * $limit) - $limit : 0;
$keyword = isset($_GET['keyword']) ? mysqli_real_escape_string($koneksi, $_GET['keyword']) : '';
$where_clause = !empty($keyword) ? " WHERE nama LIKE '%$keyword%' OR nim LIKE '%$keyword%' OR judul LIKE '%$keyword%'" : "";

$sql = "SELECT * FROM pengajuan_plagiasi $where_clause ORDER BY tgl_pengajuan DESC LIMIT $start, $limit";
$result = mysqli_query($koneksi, $sql);
$total_data = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM pengajuan_plagiasi $where_clause"))['total'];
$total_halaman = ceil($total_data / $limit);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Dashboard Admin - SI Plagiasi</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body { background-color: #f4f7f6; }
        .wrapper { display: flex; align-items: stretch; }
        #sidebar { min-width: 250px; max-width: 250px; background: #2c3e50; color: #fff; min-height: 100vh; position: sticky; top: 0; }
        #sidebar .sidebar-header { padding: 20px; background: #1a252f; text-align: center; }
        #sidebar ul li a { padding: 15px 20px; display: block; color: #bdc3c7; text-decoration: none; border-bottom: 1px solid #34495e; }
        #sidebar ul li a:hover { background: #34495e; color: #fff; }
        #sidebar ul li.active a { background: #1abc9c; color: #fff; }
        #content { width: 100%; padding: 20px; }
        .card { border: none; border-radius: 10px; box-shadow: 0 4px 10px rgba(0,0,0,0.05); }
    </style>
</head>
<body>
<div class="wrapper">
    <nav id="sidebar">
        <div class="sidebar-header"><h5>ADMIN PIAUD</h5></div>
        <ul class="list-unstyled">
            <li class="active"><a href="admin_dashboard.php"><i class="fa fa-tachometer-alt mr-2"></i> Data Pengajuan</a></li>
            <li><a href="https://drive.google.com/drive/folders/1QPkegXeRxta8_-svQvEGvSwZgNzJX-wl" target="_blank" class="text-warning"><i class="fab fa-google-drive mr-2"></i> Folder Pengajuan</a></li>
            <li><a href="index.php" target="_blank"><i class="fa fa-globe mr-2"></i> Lihat Beranda</a></li>
            <li><a href="logout.php" onclick="return confirm('Yakin ingin logout?')"><i class="fa fa-sign-out-alt mr-2"></i> Logout</a></li>
        </ul>
    </nav>

    <div id="content">
        <nav class="navbar navbar-expand-lg navbar-light bg-white mb-4 shadow-sm">
            <span class="navbar-brand mb-0 h1">Kelola Pemeriksaan Plagiasi</span>
            <div class="ml-auto text-info"><strong>Status Form: <?= strtoupper($status_sekarang) ?></strong></div>
        </nav>

        <div class="container-fluid">
            <div class="row">
                <div class="col-md-8">
                    <div class="card mb-4 border-left border-warning h-100">
                        <div class="card-body">
                            <h5 class="card-title text-warning small font-weight-bold">UPDATE PAPAN INFORMASI</h5>
                            <form action="" method="POST">
                                <div class="form-group mb-2">
                                    <textarea name="isi_info" class="form-control" rows="2" required><?= $info_curr['isi_info'] ?></textarea>
                                </div>
                                <button type="submit" name="update_papan" class="btn btn-warning btn-sm font-weight-bold">Simpan Info</button>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card mb-4 border-left border-info h-100">
                        <div class="card-body text-center">
                            <h5 class="card-title text-info small font-weight-bold">KONTROL FORMULIR</h5>
                            <p class="mb-2">Status: <span class="badge badge-<?= ($status_sekarang == 'buka') ? 'success' : 'danger' ?>"><?= strtoupper($status_sekarang) ?></span></p>
                            <form action="" method="POST">
                                <input type="hidden" name="status_form" value="<?= ($status_sekarang == 'buka') ? 'tutup' : 'buka' ?>">
                                <button type="submit" name="set_status" class="btn btn-<?= ($status_sekarang == 'buka') ? 'danger' : 'success' ?> btn-block btn-sm shadow-sm font-weight-bold">
                                    <?= ($status_sekarang == 'buka') ? 'TUTUP PENGAJUAN' : 'BUKA PENGAJUAN' ?>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mt-4 mb-4">
                <div class="col-md-4 mb-2">
                    <div class="card bg-primary text-white p-3 shadow-sm h-100 text-center">
                        <small>Total Pengajuan</small><h3 class="m-0"><?= $stat_total ?></h3>
                    </div>
                </div>
                <div class="col-md-4 mb-2">
                    <div class="card bg-warning text-dark p-3 shadow-sm h-100 text-center">
                        <small>Sedang Proses</small><h3 class="m-0"><?= $stat_proses ?></h3>
                    </div>
                </div>
                <div class="col-md-4 mb-2">
                    <div class="card bg-success text-white p-3 shadow-sm h-100 text-center">
                        <small>Selesai</small><h3 class="m-0"><?= $stat_selesai ?></h3>
                    </div>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-body">
                    <form action="" method="GET" class="mb-4">
                        <div class="input-group w-50">
                            <input type="text" name="keyword" class="form-control" placeholder="Cari Nama/NIM..." value="<?= htmlspecialchars($keyword) ?>">
                            <div class="input-group-append"><button class="btn btn-primary" type="submit">Cari</button></div>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-hover border">
                            <thead class="bg-light">
                                <tr>
                                    <th>No</th> <th>Tgl Pengajuan</th> <th>NIM & Nama</th>
                                    <th>Judul</th> <th>Skor</th> <th>Status</th> <th class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                $no = $start + 1;
                                while($row = mysqli_fetch_assoc($result)): 
                                    $st = strtolower($row['status_periksa']);
                                ?>
                                <tr>
                                    <td><?= $no++; ?></td>
                                    <td><small><?= date('d/m/Y H:i', strtotime($row['tgl_pengajuan'])) ?></small></td>
                                    <td><strong><?= $row['nim'] ?></strong><br><small><?= $row['nama'] ?></small></td>
                                    <td><div class="text-truncate" style="max-width: 200px;" title="<?= $row['judul'] ?>"><?= $row['judul'] ?></div></td>
                                    <td><?= $row['hasil_plagiasi'] ?? 0 ?>%</td>
                                    <td><span class="badge badge-pill badge-<?= ($st == 'selesai') ? 'success' : (($st == 'ditolak') ? 'danger' : 'warning') ?>"><?= strtoupper($row['status_periksa']) ?></span></td>
                                    <td class="text-center">
                                        <div class="btn-group">
                                            <a href="update_status.php?id=<?= $row['id_plagiasi'] ?>" class="btn btn-sm btn-warning"><i class="fa fa-edit"></i></a>
                                            <a href="hapus_pengajuan.php?id=<?= $row['id_plagiasi'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Hapus?')"><i class="fa fa-trash"></i></a>
                                        </div>
                                    </td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                    <nav class="mt-3"><ul class="pagination pagination-sm justify-content-center">
                        <?php for($i=1; $i<=$total_halaman; $i++): ?>
                        <li class="page-item <?= ($page == $i) ? 'active' : ''; ?>"><a class="page-link" href="?halaman=<?= $i; ?>&keyword=<?= $keyword; ?>"><?= $i; ?></a></li>
                        <?php endfor; ?>
                    </ul></nav>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>