<?php
session_start();
require_once __DIR__ . '/config/config.php';

/* =========================
   PROTEKSI LOGIN
========================= */
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    die("Akses ditolak!");
}

$role     = $_SESSION['role'];
$username = $_SESSION['username']; // NPSN sekolah

/* =========================
   QUERY SESUAI ROLE
========================= */
if ($role === 'admin') {
    $stmt = $conn->prepare("
        SELECT * FROM rekening_koran
        ORDER BY tahun DESC, bulan DESC, id_koran DESC
    ");
} else {
    $stmt = $conn->prepare("
        SELECT * FROM rekening_koran
        WHERE npsn = ?
        ORDER BY tahun DESC, bulan DESC, id_koran DESC
    ");
    $stmt->bind_param("s", $username);
}

$stmt->execute();
$data = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Data Rekening Koran</title>
    <link rel="icon" href="assets/images/logomu.ico">
    <!-- BOOTSTRAP -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- ICON -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
</head>

<body class="bg-light">

<div class="container-fluid mt-4 px-4">
	<a href="<?= $role==='admin'?'dashboard.php':'dashboard.php' ?>"
        class="text-decoration-none text-muted">
                ← Kembali ke Dashboard
            </a>
    <div class="card shadow-sm">
        <div class="card-body">

            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">
                    <i class="bi bi-bank"></i> Data Rekening Koran
                </h5>

                <a href="tambah_rekening_koran.php" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> Tambah Rekening
                </a>
            </div>

            <div class="table-responsive">
			<?php if (isset($_GET['status']) && $_GET['status'] == 'hapus'): ?>
					<div class="alert alert-success alert-dismissible fade show">
						Data berhasil dihapus
						<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
					</div>
				<?php endif; ?>
                <table class="table table-bordered table-hover align-middle">
                    <thead class="table-primary text-center">
                        <tr>
                            <th width="50">No</th>
                            <th>NPSN</th>
                            <th>Nama Sekolah</th>
                            <th>Tahun</th>
                            <th>Bulan</th>
                            <th>Saldo Awal</th>
                            <th>Saldo Akhir</th>
                            <th>File</th>
                            <th width="80">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>

                    <?php if ($data->num_rows > 0): ?>
                        <?php $no=1; while($r = $data->fetch_assoc()): ?>
                        <tr>
                            <td class="text-center"><?= $no++ ?></td>

                            <td class="text-center">
                                <span class="badge bg-info">
                                    <?= htmlspecialchars($r['npsn']) ?>
                                </span>
                            </td>

                            <td><?= htmlspecialchars($r['nama_sekolah']) ?></td>

                            <td class="text-center">
                                <span class="badge bg-secondary">
                                    <?= $r['tahun'] ?>
                                </span>
                            </td>

                            <td class="text-center"><?= $r['bulan'] ?></td>

                            <td class="text-end fw-semibold text-primary">
                                Rp <?= number_format($r['saldo_awal'],0,',','.') ?>
                            </td>

                            <td class="text-end fw-semibold text-success">
                                Rp <?= number_format($r['saldo_akhir'],0,',','.') ?>
                            </td>

                            <td class="text-center">
                                <?php if (!empty($r['upload_file'])): ?>
                                    <a href="upload/<?= $r['upload_file'] ?>" target="_blank"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>

                            <td class="text-center">
								<div class="d-flex justify-content-center gap-1">
									<a href="edit_rekening_koran.php?id=<?= $r['id_koran'] ?>"
									   class="btn btn-sm btn-warning">
										<i class="bi bi-pencil"></i>
									</a>

									<form action="hapus_rekening_koran.php" method="post"
										  onsubmit="return confirm('Yakin ingin menghapus data ini?')">
										<input type="hidden" name="id" value="<?= $r['id_koran'] ?>">
										<button type="submit" class="btn btn-sm btn-danger">
											<i class="bi bi-trash"></i>
										</button>
									</form>
								</div>
							</td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="9" class="text-center text-muted">
                                Data belum tersedia
                            </td>
                        </tr>
                    <?php endif; ?>

                    </tbody>
                </table>
            </div>

        </div>
    </div>

</div>

</body>
</html>
