<?php
// FILE: validasi_surat_plagiasi.php
include 'koneksi.php';

$nim = isset($_GET['nim']) ? mysqli_real_escape_string($koneksi, $_GET['nim']) : '';

// Cari data pengajuan yang statusnya SUDAH SELESAI
$query = "SELECT * FROM pengajuan_plagiasi WHERE nim = '$nim' AND status_periksa = 'Selesai' ORDER BY id_plagiasi DESC LIMIT 1";
$result = mysqli_query($koneksi, $query);
$data = mysqli_fetch_assoc($result);
$found = ($data != null);

// Fungsi Tanggal Indonesia
function tgl_indo($tanggal) {
    $bulan = array (1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
    $pecah = explode('-', date('Y-m-d', strtotime($tanggal)));
    return $pecah[2] . ' ' . $bulan[(int)$pecah[1]] . ' ' . $pecah[0];
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Validasi SK Plagiasi - PIAUD UIN Madura</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/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: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        .card-validasi { border: none; border-radius: 20px; box-shadow: 0 15px 35px rgba(0,0,0,0.1); overflow: hidden; max-width: 450px; margin: 40px auto; }
        .header-validasi { padding: 40px 20px; text-align: center; color: white; position: relative; }
        .bg-valid { background: linear-gradient(135deg, #28a745 0%, #1e7e34 100%); }
        .bg-invalid { background: linear-gradient(135deg, #dc3545 0%, #a71d2a 100%); }
        .icon-status { font-size: 50px; margin-bottom: 10px; background: rgba(255,255,255,0.2); width: 90px; height: 90px; line-height: 90px; border-radius: 50%; display: inline-block; border: 3px solid rgba(255,255,255,0.5); }
        .data-box { background: white; padding: 25px; }
        .info-item { margin-bottom: 15px; border-bottom: 1px solid #f1f1f1; padding-bottom: 10px; }
        .info-label { font-size: 11px; color: #888; font-weight: bold; text-transform: uppercase; margin-bottom: 2px; }
        .info-value { font-size: 15px; color: #2c3e50; font-weight: 600; line-height: 1.4; }
        .judul-style { font-style: italic; color: #555; font-weight: 500; }
    </style>
</head>
<body>

<div class="container px-3">
    <div class="card card-validasi">
        
        <?php if($found): ?>
            <div class="header-validasi bg-valid">
                <div class="icon-status"><i class="fa fa-check"></i></div>
                <h3 class="font-weight-bold mb-0">DOKUMEN ASLI</h3>
                <p class="mb-0 opacity-75">Terverifikasi Sistem PIAUD</p>
            </div>
            <div class="data-box">
                <div class="text-center mb-4">
                    <img src="images/logouinmaduranobg.png" width="50" class="mb-2">
                    <h6 class="font-weight-bold m-0">PRODI PIAUD UIN MADURA</h6>
                    <small class="text-muted">Sistem Informasi Plagiasi</small>
                </div>

                <div class="info-item">
                    <div class="info-label">Nomor Surat Resmi</div>
                    <div class="info-value"><?= htmlspecialchars($data['no_surat_resmi']) ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">Nama Mahasiswa</div>
                    <div class="info-value"><?= htmlspecialchars($data['nama']) ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">NIM</div>
                    <div class="info-value"><?= htmlspecialchars($data['nim']) ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">Judul Karya Ilmiah</div>
                    <div class="info-value judul-style">"<?= htmlspecialchars($data['judul']) ?>"</div>
                </div>
                <div class="info-item">
                    <div class="info-label">Hasil Similarity</div>
                    <div class="info-value text-success" style="font-size: 20px;"><?= htmlspecialchars($data['hasil_plagiasi']) ?>%</div>
                </div>
                <div class="info-item">
                    <div class="info-label">Tanggal Terbit</div>
                    <div class="info-value"><?= tgl_indo($data['tgl_pengajuan']) ?></div>
                </div>

                <div class="alert alert-success mt-4 mb-0 text-center py-2">
                    <small><i class="fa fa-info-circle"></i> Data ini sesuai dengan database kami.</small>
                </div>
            </div>

        <?php else: ?>
            <div class="header-validasi bg-invalid">
                <div class="icon-status"><i class="fa fa-times"></i></div>
                <h3 class="font-weight-bold mb-0">TIDAK VALID</h3>
                <p class="mb-0 opacity-75">Data Tidak Ditemukan</p>
            </div>
            <div class="data-box text-center">
                <p class="text-muted">Maaf, data untuk NIM <b><?= htmlspecialchars($nim) ?></b> tidak ditemukan atau belum diverifikasi oleh Admin.</p>
                <div class="alert alert-warning text-left small">
                    <i class="fa fa-exclamation-triangle"></i> Pastikan QR Code yang dipindai berasal dari Surat Keterangan resmi yang diterbitkan oleh Prodi PIAUD UIN Madura.
                </div>
                <a href="index.php" class="btn btn-outline-danger btn-block rounded-pill">Kembali ke Beranda</a>
            </div>
        <?php endif; ?>

    </div>
    
    <p class="text-center text-muted small mt-4">Sistem Validasi Digital &copy; <?= date('Y') ?> <br> PIAUD UIN Madura</p>
</div>

</body>
</html>