<?php 
require_once "koneksi.php";
$tahunsblm = date("Y") - 1;
$tahun = date("Y");

/* =========================
   HAPUS TRACER
========================= */
if(isset($_POST['hapus_semua'])){
    $koneksi->query("DELETE FROM tracer_study");
    $koneksi->query("ALTER TABLE tracer_study AUTO_INCREMENT = 1");

    echo "<script>
    alert('Data Tracer berhasil dihapus!');
    window.location=window.location.href;
    </script>";
    exit;
}

/* =========================
   HAPUS SNBP
========================= */
if(isset($_POST['hapus_snbp'])){
    $koneksi->query("DELETE FROM snbp");
    $koneksi->query("ALTER TABLE snbp AUTO_INCREMENT = 1");

    echo "<script>
    alert('Data SNBP berhasil dihapus!');
    window.location=window.location.href;
    </script>";
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Info Karir</title>

<!-- Bootstrap -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">

<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap4.min.css">

<style>
body {
    background: #eef2f7;
    font-family: 'Segoe UI', sans-serif;
}

.card-modern {
    border-radius: 16px;
    border: none;
    box-shadow: 0 8px 25px rgba(0,0,0,0.08);
}

.section-title {
    font-weight: 600;
}

.header-box {
    background: linear-gradient(135deg,#1e3c72,#2a5298);
    color: white;
    border-radius: 16px;
    padding: 20px;
}

.table thead {
    background: #1e293b;
    color: white;
}

/* Zebra biar enak dibaca */
.table tbody tr:nth-child(even){
    background:#f8fafc;
}
</style>
</head>

<body>

<div class="container mt-4">

<!-- HEADER -->
<div class="header-box mb-4">
    <a href="dashboard_admin.php" class="btn btn-secondary btn-modern">
    <i class="fas fa-arrow-left"></i> Kembali </a>
    <h3>⚙️ Admin Dashboard</h3>
    <p class="mb-0">Kelola Data Tracer Study & SNBP</p>
</div>

<!-- IMPORT -->
<div class="card card-modern p-4 mb-4">
<h5 class="section-title">📥 Import Data</h5>

<div class="row">

<div class="col-md-6">
    <h6>Tracer Study</h6>
    <form method="POST" action="import_tracerstudy.php" enctype="multipart/form-data">
        <input type="file" name="file" class="form-control mb-2" required>
        <button class="btn btn-primary btn-block">Import Data Tracer Study (Harus Excel Ya ! Dan Format Harus Sesuai Dengan Kolom Dibawah)</button>
    </form>
    <form method="post" onsubmit="return confirm('Hapus semua tracer?')">
        <button name="hapus_semua" class="btn btn-danger btn-block mt-2">Hapus Data Yang Sekarang</button>
    </form>
</div>

<div class="col-md-6">
    <h6>SNBP</h6>
    <form method="POST" action="import_snbp.php" enctype="multipart/form-data">
        <input type="file" name="file" class="form-control mb-2" required>
        <button class="btn btn-success btn-block">Import Data SNBP (Harus Excel Ya ! Dan Format Harus Sesuai Dengan Kolom Dibawah)</button>
    </form>
    <form method="post" onsubmit="return confirm('Hapus semua SNBP?')">
        <button name="hapus_snbp" class="btn btn-danger btn-block mt-2">Hapus Data Yang Sekarang</button>
    </form>
</div>

</div>
</div>

<!-- TRACER TABLE -->
<div class="card card-modern p-4 mb-4">
<h5 class="section-title">📋 Data Tracer Study (Tahun <?= $tahunsblm ?>)</h5>

<div class="table-responsive">
<table id="tableTracer" class="table table-hover table-bordered">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>L/P</th>
<th>TTL</th>
<th>Jurusan</th>
<th>Sasaran</th>
<th>Tempat Sasaran</th>
</tr>
</thead>

<tbody>
<?php
$data = mysqli_query($koneksi, "SELECT * FROM tracer_study");

while ($d = mysqli_fetch_array($data)) {
echo "<tr>
<td>$d[nomor]</td>
<td>$d[nama]</td>
<td>$d[jk]</td>
<td>$d[tempat_lahir] - $d[tanggal_lahir]</td>
<td>$d[jurusan]</td>
<td>$d[sasaran]</td>
<td>$d[tempat_sasaran]</td>
</tr>";
}
?>
</tbody>
</table>
</div>
</div>

<!-- SNBP TABLE -->
<div class="card card-modern p-4">
<h5 class="section-title">🎓 Data SNBP (Tahun <?= $tahun ?>)</h5>

<div class="table-responsive">
<table id="tableSNBP" class="table table-hover table-bordered">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
<th>PTN</th>
<th>Prodi</th>
<th>KIP</th>
</tr>
</thead>

<tbody>
<?php
$data = mysqli_query($koneksi, "SELECT * FROM snbp");

while ($d = mysqli_fetch_array($data)) {
echo "<tr>
<td>$d[nomor]</td>
<td>$d[nama]</td>
<td>$d[kelas]</td>
<td>$d[ptn]</td>
<td>$d[prodi]</td>
<td>$d[kip]</td>
</tr>";
}
?>
</tbody>
</table>
</div>
</div>

</div>

<!-- JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap4.min.js"></script>

<script>
$(document).ready(function() {
    $('#tableTracer').DataTable({
        pageLength: 5
    });

    $('#tableSNBP').DataTable({
        pageLength: 5
    });
});
</script>

</body>
</html>