<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

function esc($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

// ===================== PAGINATION =====================
$limit = 10;
$page  = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$start = ($page - 1) * $limit;

$totalQuery = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM keuangan WHERE pengeluaran > 0");
$totalData  = mysqli_fetch_assoc($totalQuery)['total'];
$totalPage  = ceil($totalData / $limit);

$query = mysqli_query($koneksi, "SELECT id, tanggal, keterangan, pengeluaran FROM keuangan WHERE pengeluaran > 0 ORDER BY id DESC LIMIT $start, $limit");

// Page Identifier for Active Menu State
$currentPage = "keuangan_keluar";
?>
<!doctype html>
<html lang="id">
 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <title>Keuangan Keluar — Sistem Informasi Masjid</title>
   <!-- Bootstrap + FontAwesome -->
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
   <!-- Dashboard Styles -->
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/sidebar.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/layout.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/dashboard.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/widgets.css'); ?>">
 </head>
 <body class="dashboard-page">
  <!-- SIDEBAR -->
  <?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>
  <!-- CONTENT WRAPPER -->
  <div id="content-wrapper" class="content-wrapper">
   <?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>
   <main class="container-fluid mt-4">
    <h1 class="page-title">Keuangan Keluar</h1>
    <p class="subtitle small text-muted">Ringkasan cepat — <?= date('d M Y'); ?></p>
    <!--  TABEL  -->
    <div class="card mt-4">
     <div class="card-header bg-success text-white fw-bold">Data Keuangan Keluar</div>
      <div class="card-body">
       <a href="<?= base_url('/pages/keuangan_keluar/php/tambah_data.php'); ?>" class="btn btn-success mb-3">➕ Tambah Pengeluaran</a>
      <table class="table table-striped table-bordered table-center">
       <thead class="table-success">
        <tr>
         <th>No</th>
         <th>ID</th>
         <th>TANGGAL</th>
         <th>KETERANGAN</th>
         <th>PENGELUARAN</th>
         <th>AKSI</th>
        </tr>
       </thead>
       <tbody>
        <?php 
         $no = $start + 1; 
         if ($query && mysqli_num_rows($query) > 0):
         while ($row = mysqli_fetch_assoc($query)): 
        ?>
        <tr>
         <td><?= $no++ ?></td>
         <td><?= esc($row['id']) ?></td>
         <td class="fw-bold text-success"><?= esc($row['tanggal']) ?></td>
         <td class="fw-bold text-success"><?= esc($row['keterangan']) ?></td>
         <td class="fw-bold text-success">Rp.<?= esc($row['pengeluaran']) ?></td>
         <td class="text-center action-buttons">
          <a href="php/edit_data.php?id=<?= $row['id'] ?>" class="btn btn-warning btn-sm btn-icon">
           <i class="fa-solid fa-pen-to-square"></i>
          </a>
          <a href="php/delete_data.php?id=<?= $row['id'] ?>" onclick="return confirm('Yakin hapus data ini?')" class="btn btn-danger btn-sm btn-icon">
           <i class="fa-solid fa-trash"></i>
          </a>
        </td>
        </tr>
        <?php endwhile; else: ?>
        <tr><td colspan="4" class="text-center text-muted">Belum ada data Keuangan Keluar tercatat.</td></tr>
        <?php endif; ?>
       </tbody>
       <?php
       // Hitung total pengeluaran
       $totalQuery = mysqli_query($koneksi, "SELECT SUM(pengeluaran) AS total FROM keuangan WHERE pengeluaran > 0");
       $totalData  = mysqli_fetch_assoc($totalQuery);
       $total = $totalData['total'] ?? 0;
       ?>
       <tr style="background:#e8fff0; font-weight:bold;">
        <td colspan="4" class="text-end">TOTAL PENGELUARAN :</td>
        <td style="color:#0a8a3a;"><?= "Rp " . number_format($total, 0, ',', '.'); ?></td>
       </tr>
      </table>
      <!-- PAGINATION -->
      <nav>
       <ul class="pagination justify-content-center mt-3">
        <!-- Previous -->
        <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>"><a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a></li>
        <!-- Numbers -->
        <?php for ($i = 1; $i <= $totalPage; $i++): ?>
        <li class="page-item <?= $page == $i ? 'active' : '' ?>"><a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a></li>
        <?php endfor; ?>
        <!-- Next -->
        <li class="page-item <?= $page >= $totalPage ? 'disabled' : '' ?>"><a class="page-link" href="?page=<?= $page + 1 ?>">Next</a></li>
       </ul>
      </nav>
     </div>
    </div>
   </main>
  </div>
  <?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>
 </body>
</html>