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

// Cek status dari file manual (Default: buka)
$status_form = file_exists('status_form.txt') ? file_get_contents('status_form.txt') : 'buka';

$pesan = "";
if (isset($_POST['submit'])) {
    // Keamanan jika user mencoba bypass lewat inspect element
    if ($status_form == 'tutup') {
        die("Akses Ditolak: Pengajuan sedang ditutup.");
    }

    $nim   = mysqli_real_escape_string($koneksi, $_POST['nim']);
    $nama  = mysqli_real_escape_string($koneksi, $_POST['nama']);
    $judul = mysqli_real_escape_string($koneksi, $_POST['judul']);
    $link  = mysqli_real_escape_string($koneksi, $_POST['link_draft']);

    $query = "INSERT INTO pengajuan_plagiasi (nim, nama, judul, link_draft, status_periksa) 
              VALUES ('$nim', '$nama', '$judul', '$link', 'Proses')";

    if (mysqli_query($koneksi, $query)) {
        $pesan = "<div class='alert alert-success'>Pengajuan berhasil dikirim! Silakan pantau status di halaman utama.</div>";
    } else {
        $pesan = "<div class='alert alert-danger'>Gagal mengirim: " . 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>Form Pengajuan Plagiasi - 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: #f4f7f6; padding-top: 50px; }
        .card { border-radius: 15px; border: none; box-shadow: 0 10px 25px rgba(0,0,0,0.1); }
        .card-header { background: #3c8dbc; color: white; border-radius: 15px 15px 0 0 !important; text-align: center; }
        .btn-submit { background: #3c8dbc; color: white; border-radius: 50px; padding: 10px 30px; }
    </style>
</head>
<body>

<div class="container mb-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header p-4">
                    <h4 class="mb-0">Formulir Pengajuan Cek Plagiasi</h4>
                </div>
                <div class="card-body p-4">
                    
                    <?php if ($status_form == 'tutup') : ?>
                        <div class="text-center py-5">
                            <i class="fa fa-lock fa-4x text-danger mb-3"></i>
                            <h4 class="text-danger font-weight-bold">Pengajuan Plagiasi Sedang Ditutup</h4>
                            <p class="text-muted">Mohon maaf, saat ini kami tidak menerima pengajuan baru.<br>
                            Silakan pantau terus <strong>Papan Informasi Akademik</strong> di halaman depan untuk jadwal pembukaan berikutnya.</p>
                            <hr>
                            <a href="index.php" class="btn btn-primary px-5 shadow-sm mt-3">Kembali ke Beranda</a>
                        </div>
                    <?php else : ?>
                        <?= $pesan; ?>
                        
                        <div class="alert alert-info small shadow-sm border-0">
                            <i class="fa fa-info-circle"></i> <strong>Instruksi:</strong><br>
                            1. Upload file Anda ke folder Google Drive Prodi.<br>
                            2. Klik kanan pada file, pilih <b>Bagikan/Get Link</b>.<br>
                            3. Pastikan akses diatur ke <b>"Siapa saja yang memiliki link"</b>.<br>
                            4. Tempelkan link tersebut pada kolom di bawah ini.
                        </div>

                        <form action="" method="POST">
                            <div class="form-group">
                                <label class="font-weight-bold">NIM Mahasiswa</label>
                                <input type="text" name="nim" class="form-control" placeholder="Contoh: 202107010..." required>
                            </div>
                            <div class="form-group">
                                <label class="font-weight-bold">Nama Lengkap</label>
                                <input type="text" name="nama" class="form-control" placeholder="Masukkan nama sesuai KRS" required>
                            </div>
                            <div class="form-group">
                                <label class="font-weight-bold">Judul Skripsi / Karya Ilmiah</label>
                                <textarea name="judul" class="form-control" rows="3" placeholder="Tuliskan judul lengkap..." required></textarea>
                            </div>
                            <div class="form-group">
                                <label class="font-weight-bold">Link Google Drive (Draft)</label>
                                <input type="url" name="link_draft" class="form-control" placeholder="https://drive.google.com/..." required>
                            </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> Kembali</a>
                                <button type="submit" name="submit" class="btn btn-submit shadow font-weight-bold">
                                    <i class="fa fa-paper-plane mr-2"></i> Kirim Pengajuan
                                </button>
                            </div>
                        </form>
                    <?php endif; ?>

                </div>
            </div>
            <p class="text-center text-muted mt-4 small">&copy; 2026 PIAUD UIN Madura</p>
        </div>
    </div>
</div>

</body>
</html>