<?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";
// Ambil data user
$nama     = getSession('pengurus_nama') ?? 'Pengurus';
$currentPage = "tambah_keuangan_keluar";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $keterangan     = mysqli_real_escape_string($koneksi, $_POST['keterangan']);
    $pengeluaran    = floatval($_POST['pengeluaran']);
    $tanggal = date("Y-m-d");
    $user           = getSession('pengurus_nama');
    $now            = date("Y-m-d H:i:s");
    if ($pengeluaran > 0) {
        $q1 = "INSERT INTO keuangan (tanggal, keterangan, pengeluaran, user_update, waktu_input, waktu_update)
               VALUES ('$tanggal','$keterangan',$pengeluaran,'$user','$now','$now')";
        if (mysqli_query($koneksi, $q1)) {
            $lastID = mysqli_insert_id($koneksi);
            // LOG activity
            $q2 = "INSERT INTO log_keuangan (alasan, id_keuangan, tanggal, keterangan, jenis, nilai, data_update, user_update)
                   VALUES ('data dibuat','$lastID','$tanggal','$keterangan','pengeluaran',$pengeluaran,'$now','$user')";
            mysqli_query($koneksi, $q2);
            header("Location: ../index.php?success=1");
            exit;
        }
        $err = "Gagal menyimpan data!";
    } else {
        $err = "Nominal pengeluaran wajib diisi!";
    }
}
?>
<!doctype html>
<html lang="id">
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Tambah Pengeluaran — 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">Tambah Pengeluaran</h1>
       <p class="subtitle small text-muted">Ringkasan cepat — <?= date('d M Y'); ?></p>
       <!--  TABEL  -->
       <div class="card shadow-sm mt-4">
        <div class="card-header bg-success text-white"><strong>Form Pengeluran Masjid</strong></div>
        <div class="card-body">
         <?php if (!empty($err)): ?>
         <div class="alert alert-danger"><?= esc($err) ?></div>
         <?php endif; ?>
         <form method="POST">
          <!-- Tanggal tampil tapi non-edit -->
          <div class="mb-3">
           <label class="form-label">Tanggal Transaksi</label>
           <input type="text" class="form-control" value="<?= date('d M Y'); ?>" disabled>
          </div>
          <div class="mb-3">
           <label class="form-label">Keterangan</label>
           <input type="text" name="keterangan" class="form-control" placeholder="Contoh: Pembelian Alat">
          </div>
          <div class="mb-3">
           <label class="form-label">Nominal Pengeluaran</label>
           <input type="number" name="pengeluaran" min="1" class="form-control" placeholder="Contoh: 100000" required>
          </div>
          <button type="submit" class="btn btn-success px-4"><i class="fa fa-save"></i> Simpan</button>
          <a href="../index.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Kembali</a>
         </form>
        </div>
       </div>
      </nav>
     </div>
    </div>
   </main>
  </div>
  <?php include __DIR__ . "/../../../assets/component/dashboard_footer.php"; ?>
 </body>
</html>