<?php

session_start();

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

$user_id = $_SESSION['id'];

$data = mysqli_query(
    $conn,
    "SELECT * FROM berkas
     WHERE user_id='$user_id'
     ORDER BY id DESC"
);

?>

<!DOCTYPE html>
<html lang="id">

<head>

<meta charset="UTF-8">

<meta name="viewport"
content="width=device-width, initial-scale=1.0">

<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="upload.php">
<i class="fa-solid fa-upload"></i>
Upload Berkas
</a>

<a href="../logout.php"
onclick="return konfirmasiLogout()">

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

</a>

</div>



<!-- CONTENT -->

<div class="content">

<div class="topbar">

<h1>Dashboard User</h1>

</div>



<!-- CARD -->

<div class="cards">

<?php

$total = mysqli_num_rows($data);

$pending = mysqli_num_rows(
mysqli_query(
$conn,
"SELECT * FROM berkas
WHERE user_id='$user_id'
AND status='pending'"
)
);

$diterima = mysqli_num_rows(
mysqli_query(
$conn,
"SELECT * FROM berkas
WHERE user_id='$user_id'
AND status='diterima'"
)
);

$ditolak = mysqli_num_rows(
mysqli_query(
$conn,
"SELECT * FROM berkas
WHERE user_id='$user_id'
AND status='ditolak'"
)
);

?>

<div class="card">
<h3>Total Berkas</h3>
<p><?= $total; ?></p>
</div>

<div class="card">
<h3>Pending</h3>
<p><?= $pending; ?></p>
</div>

<div class="card">
<h3>Diterima</h3>
<p><?= $diterima; ?></p>
</div>

<div class="card">
<h3>Ditolak</h3>
<p><?= $ditolak; ?></p>
</div>

</div>



<!-- TABLE -->

<div class="table-container">

<h2>Data Berkas</h2>

<table>

<tr>

<th>No</th>
<th>Nama Berkas</th>
<th>Tanggal Upload</th>
<th>File</th>
<th>Status</th>
<th>Koreksi Admin</th>

</tr>

<?php

$no = 1;

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

?>

<tr>

<td><?= $no++; ?></td>

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

<!-- TANGGAL & WAKTU UPLOAD -->

<td class="tanggal-upload">

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

</td>

<td>

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

Lihat File

</a>

</td>


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

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

</td>


<td>

<?php

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

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

} elseif($d['status'] == 'diterima') {

    echo "
    <div class='diterima-box'>
    Berkas sudah lengkap
    </div>
    ";

} else {

    echo "
    <div class='pending-box'>
    Menunggu pemeriksaan admin
    </div>
    ";
}

?>

</td>

</tr>

<?php } ?>

</table>

</div>

</div>

</div>

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

</body>
</html>