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

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

// Pastikan ID ada
if (!isset($_GET['id'])) {
    header("location:admin_dashboard.php");
    exit;
}

$id = mysqli_real_escape_string($koneksi, $_GET['id']);
$query = mysqli_query($koneksi, "SELECT * FROM pengajuan_plagiasi WHERE id_plagiasi = '$id'");
$data = mysqli_fetch_assoc($query);

if (!$data) {
    die("Data tidak ditemukan!");
}

$pesan = "";
if (isset($_POST['update'])) {
    $no_surat    = mysqli_real_escape_string($koneksi, $_POST['no_surat_resmi']);
    $skor        = mysqli_real_escape_string($koneksi, $_POST['hasil_plagiasi']);
    $status      = mysqli_real_escape_string($koneksi, $_POST['status_periksa']);
    $link_hasil  = mysqli_real_escape_string($koneksi, $_POST['link_hasil']);
    $catatan     = mysqli_real_escape_string($koneksi, $_POST['catatan']);

    // Update query - Pastikan kolom sesuai dengan database Anda
    $update = mysqli_query($koneksi, "UPDATE pengajuan_plagiasi SET 
                no_surat_resmi = '$no_surat',
                hasil_plagiasi = '$skor', 
                status_periksa = '$status',
                link_hasil = '$link_hasil',
                catatan = '$catatan'
                WHERE id_plagiasi = '$id'");

    if ($update) {
        echo "<script>alert('Data berhasil diperbarui!'); window.location='admin_dashboard.php';</script>";
    } else {
        $pesan = "<div class='alert alert-danger'>Gagal memperbarui data: " . mysqli_error($koneksi) . "</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Update Status Pemeriksaan - PIAUD</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">
</head>
<body class="bg-light">

<div class="container mt-5 mb-5">
    <div class="row justify-content-center">
        <div class="col-md-7">
            <div class="card shadow border-0" style="border-radius: 15px; overflow: hidden;">
                <div class="card-header bg-primary text-white p-3">
                    <h5 class="mb-0"><i class="fa fa-edit mr-2"></i> Update Hasil Pemeriksaan</h5>
                </div>
                <div class="card-body p-4">
                    <?= $pesan; ?>
                    
                    <div class="bg-light p-3 rounded mb-4 border-left border-primary">
                        <table class="table table-sm table-borderless mb-0">
                            <tr><td width="35%">Nama Mahasiswa</td><td>: <strong><?= htmlspecialchars($data['nama']) ?></strong></td></tr>
                            <tr><td>NIM</td><td>: <?= htmlspecialchars($data['nim']) ?></td></tr>
                            <tr><td>Judul</td><td>: <span class="small font-italic"><?= htmlspecialchars($data['judul']) ?></span></td></tr>
                            <tr>
                                <td>File Draft</td>
                                <td>: <a href="<?= $data['link_draft'] ?>" target="_blank" class="btn btn-sm btn-outline-info"><i class="fa fa-external-link-alt"></i> Buka Link Drive</a></td>
                            </tr>
                        </table>
                    </div>

                    <form action="" method="POST">
                        <div class="form-group">
                            <label class="font-weight-bold text-dark">Nomor Surat Resmi</label>
                            <input type="text" name="no_surat_resmi" class="form-control" value="<?= htmlspecialchars($data['no_surat_resmi']) ?>" placeholder="Contoh: B-123/Un.33/FT/PIAUD/02/2026" required>
                            <small class="text-muted">*Wajib diisi agar nomor muncul di Surat Keterangan.</small>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Hasil Plagiasi (%)</label>
                                    <div class="input-group">
                                        <input type="number" name="hasil_plagiasi" class="form-control" value="<?= $data['hasil_plagiasi'] ?>" placeholder="0" min="0" max="100" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text">%</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Status Pemeriksaan</label>
                                    <select name="status_periksa" class="form-control" required>
                                        <option value="Proses" <?= $data['status_periksa'] == 'Proses' ? 'selected' : '' ?>>Proses</option>
                                        <option value="Selesai" <?= $data['status_periksa'] == 'Selesai' ? 'selected' : '' ?>>Selesai</option>
                                        <option value="Ditolak" <?= $data['status_periksa'] == 'Ditolak' ? 'selected' : '' ?>>Ditolak</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="font-weight-bold">Link Hasil Pemeriksaan (Google Drive)</label>
                            <input type="url" name="link_hasil" class="form-control" value="<?= htmlspecialchars($data['link_hasil']) ?>" placeholder="https://drive.google.com/...">
                            <small class="text-muted">Tempelkan link file hasil scan Turnitin/Plagiasi.</small>
                        </div>

                        <div class="form-group">
                            <label class="font-weight-bold">Catatan / Alasan (Jika Ditolak)</label>
                            <textarea name="catatan" class="form-control" rows="3" placeholder="Contoh: File tidak bisa dibuka atau persentase terlalu tinggi..."><?= htmlspecialchars($data['catatan']) ?></textarea>
                        </div>

                        <hr>
                        <div class="d-flex justify-content-between align-items-center">
                            <a href="admin_dashboard.php" class="text-muted"><i class="fa fa-arrow-left"></i> Kembali</a>
                            <button type="submit" name="update" class="btn btn-success px-4 font-weight-bold shadow-sm">
                                <i class="fa fa-save mr-1"></i> Simpan Perubahan
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<footer class="text-center pb-4 text-muted small">SI-PLAGIASI &copy; 2026</footer>

</body>
</html>