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

if (!isset($_SESSION['login']) || $_SESSION['role'] !== 'admin') {
    die("Akses ditolak");
}

/* =====================
   PENCARIAN
===================== */
$search = trim($_GET['search'] ?? '');

$sql = "SELECT * FROM rekening_map";
$params = [];

if ($search !== '') {
    $sql .= " WHERE 
        kode_rekening LIKE ? OR
        nama_rekening LIKE ? OR
        kelompok LIKE ? OR
        jenis_belanja LIKE ?";
    $like = "%$search%";
    $params = [$like, $like, $like, $like];
}

$sql .= " ORDER BY kode_rekening ASC";

$stmt = $conn->prepare($sql);
if ($params) {
    $stmt->bind_param("ssss", ...$params);
}
$stmt->execute();
$q = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="id">
<head>
<title>List Rekening Map</title>
<link rel="icon" href="assets/images/logomu.ico">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
.card-header-primary {
    background: #0d6efd;
    color: #fff;
    font-weight: 600;
}

/* === WRAPPER SCROLL === */
.table-wrapper{
    max-height:420px;
    overflow-y:auto;
    width:100%;
}

/* === TABLE === */
.table{
    width:100%;
    font-size:11px;
    white-space:nowrap;
    margin-bottom:0;
}

/* === HEADER STICKY === */
.table thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#212529;
    color:#fff;
    text-align:center;
    vertical-align:middle;
}

/* === BODY === */
.table tbody td{
    vertical-align:middle;
}

/* hover */
.table tbody tr:hover{
    background:#f1f7ff;
}
</style>
</head>

<body class="bg-light">
<div class="container mt-4">

    <!-- JUDUL + TOMBOL TAMBAH -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">📘 Daftar Rekening Map</h4>
        <a href="rekening_map.php" class="btn btn-primary btn-sm">
            ➕ Tambah Data
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

            <!-- FORM PENCARIAN -->
            <form method="get" class="row g-2 mb-3">
                <div class="col-md-4">
                    <input type="text" name="search"
                           class="form-control form-control-sm"
                           placeholder="🔍 Cari kode / nama / kelompok / jenis belanja"
                           value="<?= htmlspecialchars($search) ?>">
                </div>
                <div class="col-md-auto">
                    <button class="btn btn-sm btn-primary">Cari</button>
                    <a href="<?= basename(__FILE__) ?>" class="btn btn-sm btn-secondary">Reset</a>
                </div>
            </form>

            <div class="table-wrapper">
                <table class="table table-bordered table-striped table-sm">
                    <thead class="table-dark text-center">
                        <tr>
                            <th width="5%">No</th>
                            <th>Kode Rekening</th>
                            <th>Nama Rekening</th>
                            <th>Kelompok</th>
                            <th>Jenis Belanja</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php if ($q->num_rows > 0): ?>
                        <?php $no=1; while($d = $q->fetch_assoc()): ?>
                        <tr>
                            <td class="text-center"><?= $no++ ?></td>
                            <td><?= htmlspecialchars($d['kode_rekening']) ?></td>
                            <td><?= htmlspecialchars($d['nama_rekening']) ?></td>
                            <td class="text-center"><?= htmlspecialchars($d['kelompok']) ?></td>
                            <td><?= htmlspecialchars($d['jenis_belanja']) ?></td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted">
                                Data tidak ditemukan
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>

    <!-- TOMBOL KEMBALI -->
    <div class="mt-3">
        <a href="dashboard.php" class="btn btn-secondary btn-sm">
            ⬅ Kembali ke Dashboard
        </a>
    </div>

</div>
</body>
</html>
