<?php
session_start();
require_once __DIR__ . '/config/config.php';

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

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

/* ===============================
   AMBIL DATA LAMA
================================ */
$stmt = $conn->prepare("SELECT * FROM penganggaran WHERE id_anggaran=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$data) {
    echo "<div class='alert alert-danger'>Data tidak ditemukan</div>";
    exit;
}

/* ===============================
   SIMPAN PERUBAHAN
================================ */
if (isset($_POST['update'])) {

    $tahun         = (int) $_POST['tahun'];
    $barjas        = (float) $_POST['barjas'];
    $habis_pakai   = (float) $_POST['habis_pakai'];
    $modal_mesin   = (float) $_POST['modal_mesin'];
    $modal_lainnya = (float) $_POST['modal_lainnya'];

    // TOTAL TANPA HONOR
    $total = $barjas + $habis_pakai + $modal_mesin + $modal_lainnya;

    $stmt = $conn->prepare("
        UPDATE penganggaran SET
            tahun=?,
            barjas=?,
            habis_pakai=?,
            modal_mesin=?,
            modal_lainnya=?,
            total=?
        WHERE id_anggaran=?
    ");

    $stmt->bind_param(
        "idddddi",
        $tahun,
        $barjas,
        $habis_pakai,
        $modal_mesin,
        $modal_lainnya,
        $total,
        $id
    );

    $stmt->execute();
    $stmt->close();

    header("Location: list_penganggaran.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Edit Penganggaran</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{
    background:#f4f6f9;
}
.card{
    border:none;
    border-radius:12px;
    box-shadow:0 6px 20px rgba(0,0,0,.06);
}
.form-label{
    font-weight:600;
}
</style>
</head>

<body>

<div class="container mt-4">

    <a href="list_penganggaran.php" class="text-decoration-none text-muted">
        ← Kembali ke Data Penganggaran
    </a>

    <h4 class="mt-2 fw-bold">✏ Edit Penganggaran</h4>
    <p class="text-muted">Perbarui data RKAS / Kertas Kerja BOS</p>

    <div class="card">
        <div class="card-body">

            <form method="post">

                <div class="row g-3">

                    <div class="col-md-3">
                        <label class="form-label">Tahun</label>
                        <input type="number"
                               name="tahun"
                               class="form-control"
                               value="<?= $data['tahun']; ?>"
                               required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Barjas</label>
                        <input type="number"
                               name="barjas"
                               class="form-control"
                               value="<?= $data['barjas']; ?>">
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Habis Pakai</label>
                        <input type="number"
                               name="habis_pakai"
                               class="form-control"
                               value="<?= $data['habis_pakai']; ?>">
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Modal Mesin</label>
                        <input type="number"
                               name="modal_mesin"
                               class="form-control"
                               value="<?= $data['modal_mesin']; ?>">
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Modal Lainnya</label>
                        <input type="number"
                               name="modal_lainnya"
                               class="form-control"
                               value="<?= $data['modal_lainnya']; ?>">
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Total</label>
                        <input type="text"
                               class="form-control bg-light fw-bold text-success"
                               value="Rp <?= number_format($data['total'],0,',','.'); ?>"
                               readonly>
                    </div>

                </div>

                <div class="mt-4">
                    <button type="submit" name="update" class="btn btn-success">
                        💾 Simpan Perubahan
                    </button>
                    <a href="list_penganggaran.php" class="btn btn-secondary">
                        ⬅ Kembali
                    </a>
                </div>

            </form>

        </div>
    </div>

</div>

</body>
</html>
