<?php

session_start();

include '../config/koneksi.php';

$user_id = $_SESSION['id'];



// ==========================
// PROSES UPLOAD
// ==========================

if(isset($_POST['upload'])){

    $nama = $_POST['nama'];

    $asal_kantor = $_POST['asal_kantor'];

    $file = time().'_'.$_FILES['file']['name'];

    $tmp = $_FILES['file']['tmp_name'];

    $folder = "../uploads/".$file;

$ekstensi =
pathinfo($file, PATHINFO_EXTENSION);

$allowed = [
'pdf',
'doc',
'docx',
'png',
'jpg',
'jpeg'
];

if(!in_array($ekstensi,$allowed)){

    echo "
    <script>

    alert('Format file tidak didukung');

    window.location='dashboard.php';

    </script>
    ";

    exit;
}

    $nomor_surat =
"BPKAD-".date('YmdHis');
    
    if(move_uploaded_file($tmp,$folder)){

        mysqli_query(
            $conn,
            "INSERT INTO berkas
            (
                nama_berkas,
                asal_kantor,
                file,
                status,
                nomor_surat,
                user_id
            )

            VALUES
            (
                '$nama',
                '$asal_kantor',
                '$file',
                'pending',
                '$nomor_surat',
                '$user_id'
            )"
        );

        echo "
        <script>

        alert('Berkas berhasil diupload');

        window.location='dashboard.php';

        </script>
        ";
    }
}




// ==========================
// AMBIL DATA BERKAS
// ==========================

$data = mysqli_query(

    $conn,

    "SELECT * FROM berkas
     WHERE user_id='$user_id'
     AND (
        status='pending'
        OR status='revisi'
     )
     ORDER BY id DESC"

);

?>

<!DOCTYPE html>
<html>

<head>

<title>Dashboard User</title>

<link rel="stylesheet"
href="../assets/style.css">

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

</head>

<body>

<div class="wrapper">



<!-- SIDEBAR -->

<div class="sidebar">

<h2>BPKAD</h2>

<a href="dashboard.php">

<i class="fa-solid fa-house"></i>

Dashboard

</a>

<a href="history.php">

<i class="fa-solid fa-clock-rotate-left"></i>

History

</a>

<a href="../logout.php">

<i class="fa-solid fa-right-from-bracket"></i>

Logout

</a>

</div>





<!-- CONTENT -->

<div class="loader"
id="loader">

<div class="spinner"></div>

</div>

<div class="content">

<!-- TOPBAR -->

<div class="topbar">

<div class="header-flex">

<div>

<h1>Dashboard User</h1>

<p>Sistem Verifikasi Berkas BPKAD</p>

</div>

<img src="../assets/img/logo minahasa.png"
class="logo">

</div>

<br>

<button onclick="darkMode()">

🌙 Dark Mode

</button>

</div>





<!-- FORM -->

<div class="table-container">

<h2>Upload Berkas</h2>

<br>

<form method="POST"
enctype="multipart/form-data">

<input type="text"
name="nama"
placeholder="Nama Berkas"
required>

<br><br>

<input type="text"
name="asal_kantor"
placeholder="Nama Kantor / Instansi"
required>

<br><br>

<input type="file"
name="file"
accept=".pdf,.doc,.docx,.png,.jpg,.jpeg"
required>

<br><br>

<button type="submit"
name="upload">

Upload Berkas

</button>

</form>

</div>

<br><br>





<!-- TABLE -->

<div class="table-container">

<h2>Data Berkas</h2>

<br>

<table>

<tr>

<th>No</th>
<th>Nomor Surat</th>
<th>Nama Berkas</th>
<th>Asal Kantor</th>
<th>File</th>
<th>Status</th>
<th>Tanggal Upload</th>
<th>Keterangan Admin</th>
<th>Revisi Berkas</th>

</tr>

<?php

$no = 1;

while($d = mysqli_fetch_assoc($data)){

?>

<tr>

<td>

<?= $no++; ?>

</td>



<td>

<?= $d['nomor_surat']; ?>

</td>



<td>

<?= $d['nama_berkas']; ?>

</td>



<td>

<?= $d['asal_kantor']; ?>

</td>



<td>

<a href="../uploads/<?php echo $d['file']; ?>"
target="_blank">

Lihat File

</a>

</td>



<td class="<?= $d['status']; ?>">

<?= $d['status']; ?>

</td>



<td>

<?= date(
'd-m-Y H:i',
strtotime($d['tanggal_upload'])
); ?>

</td>



<!-- KETERANGAN -->

<td>

<?php

if(!empty($d['keterangan'])){

    echo "
    <div class='keterangan-box'>
    ".$d['keterangan']."
    </div>
    ";

}else{

    echo "
    <div class='pending-box'>
    Belum ada keterangan
    </div>
    ";
}

?>

</td>



<!-- REVISI -->

<td>

<?php

if($d['status'] == 'revisi'){

?>

<form action="upload_revisi.php"
method="POST"
enctype="multipart/form-data">

<input type="hidden"
name="id"
value="<?= $d['id']; ?>">

<input type="file"
name="file"
required>

<br><br>

<button type="submit"
name="upload_revisi"
class="btn-acc">

Upload Ulang

</button>

</form>

<?php

}else{

    echo "
    <div class='pending-box'>
    Tidak perlu revisi
    </div>
    ";
}

?>
</td>

</tr>

<?php } ?>

</table>

</div>

</div>

</div>

<script src="../assets/script.js"></script>

</body>
</html>