<?php
require_once __DIR__ . '/config/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $tanggal        = $_POST['tanggal'];
    $kode_kegiatan  = $_POST['kode_kegiatan'];
    $kode_rekening  = $_POST['kode_rekening'];
    $nobukti        = $_POST['nobukti'];
    $uraian         = $_POST['uraian'];
    $jumb           = (int) $_POST['jumb'];
    $satuanb        = $_POST['satuanb'];
    $yang_menerima  = $_POST['yang_menerima'];

    // bersihkan format rupiah
    $pengeluaran = preg_replace('/[^0-9]/', '', $_POST['pengeluaran']);

    $sql = "INSERT INTO kwitansi
        (tanggal, kode_kegiatan, kode_rekening, nobukti, uraian, jumb, satuanb, pengeluaran, yang_menerima)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        die("Prepare gagal: " . $conn->error);
    }

    $stmt->bind_param(
        "sssssisds",
        $tanggal,
        $kode_kegiatan,
        $kode_rekening,
        $nobukti,
        $uraian,
        $jumb,
        $satuanb,
        $pengeluaran,
        $yang_menerima
    );

    if ($stmt->execute()) {
        echo "<script>alert('Data berhasil disimpan');window.location='list_kwitansi.php';</script>";
        exit;
    } else {
        echo "Gagal simpan: " . $stmt->error;
    }

    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Tambah Kwitansi</title>
<style>
body{font-family:"Segoe UI";background:#f4f6f9;padding:20px}
.card{max-width:720px;margin:auto;background:#fff;border-radius:12px;padding:25px;box-shadow:0 10px 30px rgba(0,0,0,.08)}
.form-grid{display:grid;grid-template-columns:1fr 1fr;gap:15px}
.form-group{display:flex;flex-direction:column}
.form-group.full{grid-column:1/-1}
input,textarea{padding:10px;border-radius:8px;border:1px solid #ccc}
.btn{padding:10px 18px;border-radius:8px;border:none;cursor:pointer}
.btn-primary{background:#0b2c4d;color:#fff}
.btn-secondary{background:#eee}
</style>
</head>

<body>
<div class="card">
<h3>Tambah Kwitansi</h3>

<form method="post">
<div class="form-grid">

<div class="form-group">
<label>Tanggal</label>
<input type="date" name="tanggal" required>
</div>

<div class="form-group">
<label>No Bukti</label>
<input name="nobukti" required>
</div>

<div class="form-group">
<label>Kode Kegiatan</label>
<input name="kode_kegiatan" required>
</div>

<div class="form-group">
<label>Kode Rekening</label>
<input name="kode_rekening" required>
</div>

<div class="form-group">
<label>Jumlah Barang</label>
<input name="jumb" type="number" required>
</div>

<div class="form-group">
<label>Satuan Barang</label>
<input name="satuanb" required>
</div>

<div class="form-group">
<label>Pengeluaran</label>
<input name="pengeluaran" id="pengeluaran" required>
</div>

<div class="form-group full">
<label>Uraian</label>
<textarea name="uraian" required></textarea>
</div>

<div class="form-group full">
<label>Yang Menerima</label>
<input name="yang_menerima" required>
</div>

</div>

<br>
<button class="btn btn-primary">💾 Simpan</button>
<a href="list_kwitansi.php" class="btn btn-secondary">↩ Batal</a>
</form>
</div>

<script>
const rupiah = document.getElementById('pengeluaran');
rupiah.addEventListener('keyup', function(){
    let angka = this.value.replace(/[^,\d]/g,'');
    let sisa  = angka.length % 3;
    let rupiah = angka.substr(0, sisa);
    let ribuan = angka.substr(sisa).match(/\d{3}/g);
    if(ribuan){
        rupiah += (sisa ? '.' : '') + ribuan.join('.');
    }
    this.value = 'Rp. ' + rupiah;
});
</script>

</body>
</html>
