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

// Pastikan ID ada sebelum diproses
if (!isset($_GET['id'])) {
    header("location:index.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);

// Gunakan strtolower agar tidak sensitif terhadap huruf besar/kecil
if (!$data || strtolower($data['status_periksa']) != 'ditolak') {
    header("location:index.php");
    exit;
}

$pesan = "";
if (isset($_POST['update'])) {
    $judul = mysqli_real_escape_string($koneksi, $_POST['judul']);
    $link  = mysqli_real_escape_string($koneksi, $_POST['link_draft']);

    // Update data: status balik ke 'Proses' dan hapus catatan penolakan
    $update = mysqli_query($koneksi, "UPDATE pengajuan_plagiasi SET 
                judul = '$judul', 
                link_draft = '$link', 
                status_periksa = 'Proses',
                catatan = NULL 
                WHERE id_plagiasi = '$id'");

    if ($update) {
        echo "<script>alert('Perbaikan berhasil dikirim! Silakan cek status secara berkala.'); window.location='index.php';</script>";
    } else {
        $pesan = "<div class='alert alert-danger'>Gagal memperbarui: " . 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>Perbaiki Pengajuan - 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">
    <style>
        body { background-color: #f8f9fa; }
        .card { border-radius: 15px; }
        .card-header { border-radius: 15px 15px 0 0 !important; }
    </style>
</head>
<body>

<div class="container mt-5 mb-5">
    <div class="row justify-content-center">
        <div class="col-md-7">
            <div class="card shadow border-0">
                <div class="card-header bg-warning text-dark p-3">
                    <h5 class="mb-0 font-weight-bold"><i class="fa fa-tools mr-2"></i> Perbaiki Pengajuan Plagiasi</h5>
                </div>
                <div class="card-body p-4">
                    <?= $pesan; ?>
                    
                    <div class="alert alert-danger border-0 shadow-sm mb-4">
                        <strong class="d-block mb-1"><i class="fa fa-exclamation-circle"></i> Alasan Penolakan:</strong>
                        <span class="font-italic"><?= htmlspecialchars($data['catatan']) ?></span>
                    </div>

                    <form action="" method="POST">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="small font-weight-bold">NIM</label>
                                    <input type="text" class="form-control bg-light" value="<?= htmlspecialchars($data['nim']) ?>" readonly>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="small font-weight-bold">Nama Lengkap</label>
                                    <input type="text" class="form-control bg-light" value="<?= htmlspecialchars($data['nama']) ?>" readonly>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="font-weight-bold">Judul Skripsi / Karya Ilmiah</label>
                            <textarea name="judul" class="form-control" rows="3" required><?= htmlspecialchars($data['judul']) ?></textarea>
                        </div>

                        <div class="form-group">
                            <label class="font-weight-bold">Link Google Drive Baru</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fab fa-google-drive"></i></span>
                                </div>
                                <input type="url" name="link_draft" class="form-control" value="<?= htmlspecialchars($data['link_draft']) ?>" placeholder="https://drive.google.com/..." required>
                            </div>
                            <small class="text-muted">Pastikan link sudah "Public" (Anyone with the link).</small>
                        </div>
                        
                        <hr>
                        <div class="d-flex justify-content-between align-items-center">
                            <a href="index.php" class="text-muted"><i class="fa fa-arrow-left"></i> Batal</a>
                            <button type="submit" name="update" class="btn btn-primary px-5 font-weight-bold shadow">
                                Kirim Perbaikan <i class="fa fa-paper-plane ml-1"></i>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>