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

if (!isset($_GET['id'])) {
    header("Location: list_kwitansi.php");
    exit;
}

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

/* =====================
   AMBIL DATA
===================== */
$q = $conn->prepare("SELECT * FROM kwitansi WHERE id_kw = ?");
$q->bind_param("i", $id);
$q->execute();
$data = $q->get_result()->fetch_assoc();

if (!$data) {
    die("Data tidak ditemukan");
}

/* =====================
   UPDATE DATA
===================== */
if (isset($_POST['update'])) {

    // Bersihkan format rupiah
    $pengeluaran = str_replace(['Rp', '.', ' ', ','], '', $_POST['pengeluaran']);

    $stmt = $conn->prepare("
        UPDATE kwitansi SET
            tanggal        = ?,
            kode_kegiatan  = ?,
            kode_rekening  = ?,
            nobukti        = ?,
            uraian         = ?,
            jumb           = ?,
            satuanb        = ?,
            pengeluaran    = ?,
            yang_menerima  = ?
        WHERE id_kw = ?
    ");

    $stmt->bind_param(
        "sssssssisi",
        $_POST['tanggal'],
        $_POST['kode_kegiatan'],
        $_POST['kode_rekening'],
        $_POST['nobukti'],
        $_POST['uraian'],
        $_POST['jumb'],
        $_POST['satuanb'],
        $pengeluaran,
        $_POST['yang_menerima'],
        $id
    );

    if ($stmt->execute()) {
        header("Location: list_kwitansi.php");
        exit;
    } else {
        die("Gagal update data: " . $stmt->error);
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Edit Kwitansi</title>

<style>
body{
    font-family:"Segoe UI", Arial;
    background:#f4f6f9;
    padding:20px;
}
.card{
    max-width:720px;
    margin:auto;
    background:#fff;
    border-radius:12px;
    box-shadow:0 10px 30px rgba(0,0,0,.08);
    padding:25px;
}
.card h3{
    margin:0 0 20px;
    color:#0b2c4d;
}
.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;
}
label{
    font-size:13px;
    margin-bottom:5px;
    color:#555;
}
input, textarea{
    padding:10px;
    border:1px solid #ccc;
    border-radius:8px;
    font-size:14px;
}
textarea{
    resize:vertical;
    min-height:80px;
}
input:focus, textarea:focus{
    outline:none;
    border-color:#0b2c4d;
    box-shadow:0 0 0 2px rgba(11,44,77,.1);
}
.btn-group{
    margin-top:20px;
    display:flex;
    gap:10px;
}
.btn{
    padding:10px 18px;
    border-radius:8px;
    font-size:14px;
    border:none;
    cursor:pointer;
}
.btn-primary{
    background:#0b2c4d;
    color:#fff;
}
.btn-secondary{
    background:#eee;
    color:#333;
}
</style>
</head>

<body>

<div class="card">
<h3>Edit Kwitansi</h3>

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

    <div class="form-group">
        <label>Tanggal</label>
        <input type="date" name="tanggal" value="<?= $data['tanggal'] ?>" required>
    </div>

    <div class="form-group">
        <label>No Bukti</label>
        <input name="nobukti" value="<?= htmlspecialchars($data['nobukti']) ?>" required>
    </div>

    <div class="form-group">
        <label>Kode Kegiatan</label>
        <input name="kode_kegiatan" value="<?= htmlspecialchars($data['kode_kegiatan']) ?>" required>
    </div>

    <div class="form-group">
        <label>Kode Rekening</label>
        <input name="kode_rekening" value="<?= htmlspecialchars($data['kode_rekening']) ?>" required>
    </div>

    <div class="form-group">
        <label>Jumlah Barang</label>
        <input name="jumb" value="<?= htmlspecialchars($data['jumb']) ?>" required>
    </div>

    <div class="form-group">
        <label>Satuan Barang</label>
        <input name="satuanb" value="<?= htmlspecialchars($data['satuanb']) ?>" required>
    </div>

    <div class="form-group">
        <label>Pengeluaran</label>
        <input name="pengeluaran" id="pengeluaran"
               value="Rp. <?= number_format($data['pengeluaran'],0,',','.') ?>" required>
    </div>

    <div class="form-group full">
        <label>Uraian</label>
        <textarea name="uraian" required><?= htmlspecialchars($data['uraian']) ?></textarea>
    </div>

    <div class="form-group full">
        <label>Yang Menerima</label>
        <input name="yang_menerima" value="<?= htmlspecialchars($data['yang_menerima']) ?>" required>
    </div>

</div>

<div class="btn-group">
    <button class="btn btn-primary" name="update">💾 Update</button>
    <a href="list_kwitansi.php" class="btn btn-secondary">↩ Batal</a>
</div>
</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){
        let separator = sisa ? '.' : '';
        rupiah += separator + ribuan.join('.');
    }
    this.value = 'Rp. ' + rupiah;
});
</script>

</body>
</html>
