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

// Cek status form dari file manual (untuk mengubah tampilan tombol di hero)
$status_form = file_exists('status_form.txt') ? file_get_contents('status_form.txt') : 'buka';

// Ambil teks Papan Informasi dari database
$info_curr = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT isi_info FROM papan_informasi WHERE id = 1"));

// LOGIKA PENCARIAN STATUS
$keyword = isset($_GET['keyword']) ? mysqli_real_escape_string($koneksi, $_GET['keyword']) : '';
$data_ditemukan = false;
$result = null;

if (!empty($keyword)) {
    $sql = "SELECT * FROM pengajuan_plagiasi WHERE nim = '$keyword' OR nama LIKE '%$keyword%' ORDER BY tgl_pengajuan DESC";
    $result = mysqli_query($koneksi, $sql);
    if (mysqli_num_rows($result) > 0) {
        $data_ditemukan = true;
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SI-PLAGIASI PIAUD UIN MADURA</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>
        :root { --primary: #2c3e50; --accent: #1abc9c; }
        body { background-color: #f8f9fa; font-family: 'Segoe UI', sans-serif; }
        .hero-section { background: linear-gradient(135deg, var(--primary) 0%, #34495e 100%); color: white; padding: 60px 0; border-bottom: 5px solid var(--accent); }
        .card-menu { border: none; border-radius: 15px; transition: transform 0.3s; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        .card-menu:hover { transform: translateY(-5px); }
        .icon-circle { width: 70px; height: 70px; background: #ecf0f1; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 15px; color: var(--primary); font-size: 30px; }
        .status-badge { padding: 5px 12px; border-radius: 20px; font-size: 11px; font-weight: bold; }
        .btn-custom { border-radius: 50px; padding: 10px 25px; font-weight: 600; }
        .papan-informasi { background: #fffcf0; border: 1px solid #ffeeba; border-left: 5px solid #ffc107; padding: 20px; border-radius: 10px; margin-bottom: 30px; }
        .step-number { width: 30px; height: 30px; background: var(--primary); color: white; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: bold; margin-right: 10px; }
        .instruction-list { list-style: none; padding-left: 0; }
        .instruction-list li { margin-bottom: 15px; display: flex; align-items: flex-start; }
        /* Perbaikan agar tabel tidak memanjang */
        .table td { vertical-align: middle !important; }
        .alasan-box { font-size: 11px; line-height: 1.2; max-width: 200px; margin-top: 5px; color: #dc3545; }
    </style>
</head>
<body>

<div class="hero-section text-center">
    <div class="container">
        <img src="images/logouinmaduranobg.png" alt="Logo UIN" style="width: 80px; margin-bottom: 20px;">
        <h1 class="display-5 font-weight-bold">SI-PLAGIASI PIAUD</h1>
        <p class="lead">Sistem Informasi Pengajuan & Monitoring Cek Plagiarisme Mahasiswa PIAUD UIN Madura</p>
        <div class="mt-3">
            <?php if(!isset($_SESSION['status'])): ?>
                <a href="login.php" class="btn btn-outline-light btn-sm mr-2"><i class="fa fa-lock mr-1"></i> Login Admin</a>
            <?php else: ?>
                <a href="admin_dashboard.php" class="btn btn-warning btn-sm mr-2"><i class="fa fa-th-large mr-1"></i> Dashboard Admin</a>
            <?php endif; ?>
        </div>
    </div>
</div>

<div class="container" style="margin-top: -40px;">
    <div class="row justify-content-center mb-5">
        <div class="col-md-4 mb-3">
            <div class="card card-menu text-center p-4 h-100">
                <div class="icon-circle" style="color: #ea4335;"><i class="fab fa-google-drive"></i></div>
                <h5>1. Upload Berkas</h5>
                <p class="text-muted small">Upload file draft Anda ke Google Drive Prodi. Penamaan File : <b>NAMA_NIM</b>.</p>
                <a href="https://drive.google.com/drive/folders/1tNWKHQ07YZra792GB19qhKgiPIYugts-?usp=sharing" target="_blank" class="btn btn-danger btn-custom">Buka Folder Drive</a>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card card-menu text-center p-4 h-100 border-primary" style="border-width: 2px;">
                <div class="icon-circle" style="color: #4285f4;"><i class="fa fa-file-signature"></i></div>
                <h5>2. Isi Formulir</h5>
                <p class="text-muted small">Salin link file Anda dari Drive dan tempelkan pada formulir pengajuan.</p>
                
                <?php if($status_form == 'buka'): ?>
                    <a href="pengajuan_plagiasi.php" class="btn btn-primary btn-custom shadow">Mulai Pengajuan</a>
                <?php else: ?>
                    <button class="btn btn-secondary btn-custom" disabled><i class="fa fa-lock mr-1"></i> Sedang Ditutup</button>
                <?php endif; ?>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card card-menu text-center p-4 h-100">
                <div class="icon-circle" style="color: #17a2b8;"><i class="fa fa-info-circle"></i></div>
                <h5>Panduan Alur</h5>
                <p class="text-muted small">Klik tombol di bawah untuk melihat langkah-langkah penggunaan sistem.</p>
                <button type="button" class="btn btn-outline-secondary btn-custom" data-toggle="modal" data-target="#modalPanduan">Lihat Panduan</button>
            </div>
        </div>
    </div>

    <div class="papan-informasi shadow-sm">
        <h5 class="font-weight-bold"><i class="fa fa-bullhorn mr-2"></i> Papan Informasi Akademik</h5>
        <hr>
        <div class="text-dark"><?= nl2br(htmlspecialchars($info_curr['isi_info'])) ?></div>
    </div>

    <div class="card card-menu mb-5">
        <div class="card-body p-4">
            <h4 class="text-center mb-4"><i class="fa fa-search mr-2"></i> Cek Status & Download Hasil</h4>
            <form action="" method="GET" class="mb-4">
                <div class="input-group input-group-lg shadow-sm">
                    <input type="text" name="keyword" class="form-control border-0" placeholder="Masukkan NIM atau Nama Anda..." value="<?= htmlspecialchars($keyword) ?>" required>
                    <div class="input-group-append">
                        <button class="btn btn-success text-white px-4" type="submit">Cari Data</button>
                    </div>
                </div>
            </form>

            <?php if (!empty($keyword)): ?>
                <?php if ($data_ditemukan): ?>
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover bg-white">
                            <thead class="bg-light text-center">
                                <tr>
                                    <th width="12%">Tgl Pengajuan</th>
                                    <th width="15%">NIM</th>
                                    <th>Nama</th>
                                    <th>Judul</th>
                                    <th width="10%">Status</th>
                                    <th width="18%">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while($row = mysqli_fetch_assoc($result)): 
                                    $st = strtolower($row['status_periksa']);
                                    $badge = ($st == 'selesai') ? 'badge-success' : (($st == 'ditolak') ? 'badge-danger' : 'badge-warning');
                                ?>
                                <tr>
                                    <td class="text-center small"><?= date('d/m/Y', strtotime($row['tgl_pengajuan'])) ?></td>
                                    <td class="text-center font-weight-bold"><?= htmlspecialchars($row['nim']) ?></td>
                                    <td><?= htmlspecialchars($row['nama']) ?></td>
                                    <td class="small"><?= htmlspecialchars($row['judul']) ?></td>
                                    <td class="text-center">
                                        <span class="badge status-badge <?= $badge ?>"><?= strtoupper($row['status_periksa']) ?></span>
                                    </td>
                                    <td>
                                        <div class="text-center">
                                            <?php if(!empty($row['link_hasil'])): ?>
                                                <a href="<?= $row['link_hasil'] ?>" target="_blank" class="btn btn-xs btn-info py-1 px-2 mb-1 shadow-sm" style="font-size: 11px;">
                                                    <i class="fab fa-google-drive"></i> Hasil Cek
                                                </a>
                                            <?php endif; ?>

                                            <?php if($st == 'selesai'): ?>
                                                <a href="cetak_surat.php?id=<?= $row['id_plagiasi'] ?>" class="btn btn-xs btn-dark py-1 px-2 mb-1 shadow-sm" style="font-size: 11px;">
                                                    <i class="fa fa-print"></i> Surat Ket.
                                                </a>
                                            <?php elseif($st == 'ditolak'): ?>
                                                <a href="edit_pengajuan.php?id=<?= $row['id_plagiasi'] ?>" class="btn btn-xs btn-warning py-1 px-2 mb-1 shadow-sm font-weight-bold" style="font-size: 11px;">
                                                    <i class="fa fa-edit"></i> Perbaiki Data
                                                </a>
                                                <div class="alasan-box">
                                                    <b>Alasan:</b> <i><?= htmlspecialchars($row['catatan']) ?></i>
                                                </div>
                                            <?php elseif($st == 'proses'): ?>
                                                <span class="text-muted small font-italic">Menunggu Pemeriksaan</span>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-warning text-center shadow-sm">
                        Data dengan kata kunci <strong>"<?= htmlspecialchars($keyword) ?>"</strong> tidak ditemukan.
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>

<div class="modal fade" id="modalPanduan" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content" style="border-radius: 20px;">
            <div class="modal-header border-0">
                <h5 class="modal-title font-weight-bold text-primary">Panduan Pengajuan</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body p-4">
                <ul class="instruction-list">
                    <li><div class="step-number">1</div><div><b>Upload:</b> Beri nama file NAMA_NIM, upload ke Drive.</div></li>
                    <li><div class="step-number">2</div><div><b>Link:</b> Ubah akses Drive ke "Siapa saja yang memiliki link".</div></li>
                    <li><div class="step-number">3</div><div><b>Daftar:</b> Isi formulir pengajuan di website ini.</div></li>
                    <li><div class="step-number">4</div><div><b>Cek:</b> Pantau status di kolom pencarian secara berkala.</div></li>
                </ul>
            </div>
        </div>
    </div>
</div>

<footer class="text-center py-4 text-muted small">&copy; 2026 PIAUD UIN Madura</footer>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>