<?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';
function esc($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
// Pastikan ID ada
if (!isset($_GET['id']) || intval($_GET['id']) < 1) {
    header("Location: ../index.php");
    exit;
}

$id = intval($_GET['id']);

// Ambil data lama
$q = mysqli_query($koneksi, "SELECT * FROM keuangan WHERE id=$id LIMIT 1");
$data = mysqli_fetch_assoc($q);

if (!$data) {
    header("Location: ../index.php?notfound=1");
    exit;
}

// Jika submit
if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $keterangan = mysqli_real_escape_string($koneksi, $_POST['keterangan']);
    $pemasukan  = floatval($_POST['pemasukan']);
    $now        = date("Y-m-d H:i:s");
    $user       = getSession('pengurus_nama') ?? 'Unknown';

    if ($pemasukan >= 0) {

        $q1 = "UPDATE keuangan SET 
                keterangan='$keterangan',
                pemasukan=$pemasukan,
                user_update='$user',
                waktu_update='$now'
               WHERE id=$id";

        if (mysqli_query($koneksi, $q1)) {
            
            // Log: data dirubah
            $tanggalLama = $data['tanggal']; 
            $q2 = "INSERT INTO log_keuangan 
                   (alasan, id_keuangan, tanggal, keterangan, jenis, nilai, data_update, user_update)
                   VALUES
                   ('data dirubah',$id,'$tanggalLama','$keterangan','pemasukan',$pemasukan,'$now','$user')";
            mysqli_query($koneksi, $q2);

            header("Location: ../index.php?updated=1");
            exit;
        } else {
            $err = "Gagal memperbarui data!";
        }
    } else {
        $err = "Nominal pemasukan wajib valid!";
    }
}
?>
<!doctype html>
<html lang="id">
 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <title>Edit Pemasukan — 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">Edit Pemasukan</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 Edit Pemasukan Masjid</strong></div>
    <div class="card-body">

        <?php if (!empty($err)): ?>
        <div class="alert alert-danger"><?= esc($err) ?></div>
        <?php endif; ?>

        <form method="POST">

            <div class="mb-3">
                            <label class="form-label">Tanggal</label>
                            <input type="text" class="form-control bg-light" 
                                   value="<?= esc($data['tanggal']) ?>" readonly>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Keterangan</label>
                            <input type="text" name="keterangan" class="form-control"
                                   value="<?= esc($data['keterangan']) ?>">
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Nominal Pemasukan</label>
                            <input type="number" name="pemasukan" class="form-control"
                                   min="0"
                                   value="<?= esc($data['pemasukan']) ?>" required>
                        </div>

                        <button type="submit" class="btn btn-warning text-dark px-4">
                            <i class="fa fa-save"></i> Simpan Perubahan
                        </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>