<?php
session_start();
if (!isset($_SESSION['login'])) {
    die("Akses ditolak!");
}

$role          = $_SESSION['role'];
$npsn_session  = $_SESSION['username']; // NPSN sekolah

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

$id = $_GET['id'] ?? '';

$data = [
    'npsn' => $npsn_session,
    'tahun' => '',
    'barjas' => 0,
    'habis_pakai' => 0,
    'modal_mesin' => 0,
    'modal_lainnya' => 0
];

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

    if (empty($data['npsn'])) {
        $data['npsn'] = $npsn_session;
    }
    $stmt->close();
}

/* =========================
   SIMPAN DATA
========================= */
if (isset($_POST['simpan'])) {

    $npsn  = ($role === 'admin') ? $_POST['npsn'] : $npsn_session;
    $tahun = (int) $_POST['tahun'];

    $barjas = (float) $_POST['barjas'];
    $habis  = (float) $_POST['habis_pakai'];
    $mesin  = (float) $_POST['modal_mesin'];
    $lain   = (float) $_POST['modal_lainnya'];

    // TOTAL TANPA HONOR
    $total = $barjas + $habis + $mesin + $lain;

    if ($id) {
        $stmt = $conn->prepare("
            UPDATE penganggaran SET
                npsn=?,
                tahun=?,
                barjas=?,
                habis_pakai=?,
                modal_mesin=?,
                modal_lainnya=?,
                total=?
            WHERE id_anggaran=?
        ");
        $stmt->bind_param(
            "sidddddi",
            $npsn, $tahun, $barjas, $habis, $mesin, $lain, $total, $id
        );
    } else {
        $stmt = $conn->prepare("
            INSERT INTO penganggaran
            (npsn, tahun, barjas, habis_pakai, modal_mesin, modal_lainnya, total)
            VALUES (?,?,?,?,?,?,?)
        ");
        $stmt->bind_param(
            "siddddd",
            $npsn, $tahun, $barjas, $habis, $mesin, $lain, $total
        );
    }

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

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

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title><?= $id ? 'Edit' : 'Tambah' ?> 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">
        <?= $id ? '✏ Edit' : '➕ Tambah' ?> Penganggaran
    </h4>

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

            <form method="post">
                <div class="row g-3">

                    <!-- NPSN -->
                    <div class="col-md-3">
                        <label class="form-label">NPSN</label>
                        <input type="text"
                               name="npsn"
                               class="form-control"
                               value="<?= htmlspecialchars($data['npsn']) ?>"
                               <?= ($role !== 'admin') ? 'readonly' : 'required' ?>>
                    </div>

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

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

                    <!-- HABIS PAKAI -->
                    <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>

                    <!-- MODAL MESIN -->
                    <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>

                    <!-- MODAL LAINNYA -->
                    <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>

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

            </form>

        </div>
    </div>

</div>

</body>
</html>
