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

/* =========================
   PROTEKSI LOGIN
========================= */
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    die("Akses ditolak!");
}

$npsn = $_SESSION['username'];

/* =========================
   AMBIL NAMA SEKOLAH
========================= */
$nama_sekolah = '';
$stmtSekolah = $conn->prepare("SELECT nama_sekolah FROM sekolah WHERE npsn = ?");
$stmtSekolah->bind_param("s", $npsn);
$stmtSekolah->execute();
$resultSekolah = $stmtSekolah->get_result();
if ($rowSekolah = $resultSekolah->fetch_assoc()) {
    $nama_sekolah = $rowSekolah['nama_sekolah'];
}

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

    $tahun       = $_POST['tahun'];
    $bulan       = $_POST['bulan'];
    $saldo_awal  = (float) str_replace('.', '', $_POST['saldo_awal']);
    $saldo_akhir = (float) str_replace('.', '', $_POST['saldo_akhir']);
    $upload_file = '';

    if (!empty($_FILES['upload_file']['name'])) {

        $folder = __DIR__ . "/upload/";
        if (!is_dir($folder)) {
            mkdir($folder, 0777, true);
        }

        $ext = strtolower(pathinfo($_FILES['upload_file']['name'], PATHINFO_EXTENSION));
        $allowed = ['pdf','jpg','jpeg','png'];

        if (!in_array($ext, $allowed)) {
            die("Format file tidak diizinkan!");
        }

        $upload_file = 'rekening_' . time() . '.' . $ext;
        move_uploaded_file($_FILES['upload_file']['tmp_name'], $folder . $upload_file);
    }

    $stmt = $conn->prepare("
        INSERT INTO rekening_koran
        (npsn, nama_sekolah, tahun, bulan, saldo_awal, saldo_akhir, upload_file)
        VALUES (?,?,?,?,?,?,?)
    ");

    $stmt->bind_param(
        "ssssdds",
        $npsn,
        $nama_sekolah,
        $tahun,
        $bulan,
        $saldo_awal,
        $saldo_akhir,
        $upload_file
    );

    if ($stmt->execute()) {
        header("Location: list_rekening_koran.php?status=sukses");
        exit;
    } else {
        die("Gagal menyimpan data: " . $stmt->error);
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Tambah Rekening Koran</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
</head>

<body class="bg-light">

<div class="container mt-4">
<div class="card shadow-sm">
    <div class="card-header bg-primary text-white">
        <i class="bi bi-plus-circle"></i> Tambah Rekening Koran
    </div>

    <div class="card-body">
    <form method="post" enctype="multipart/form-data">

        <div class="row mb-3">
            <div class="col-md-6">
                <label class="form-label">NPSN</label>
                <input type="text" class="form-control" value="<?= htmlspecialchars($npsn) ?>" readonly>
            </div>
            <div class="col-md-6">
                <label class="form-label">Nama Sekolah</label>
                <input type="text" class="form-control" value="<?= htmlspecialchars($nama_sekolah) ?>" readonly>
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-4">
                <label class="form-label">Tahun</label>
                <input type="number" name="tahun" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label class="form-label">Bulan</label>
                <select name="bulan" class="form-select" required>
                    <option value="">- Pilih -</option>
                    <?php
                    $bulanList = [
                        'Januari','Februari','Maret','April','Mei','Juni',
                        'Juli','Agustus','September','Oktober','November','Desember'
                    ];
                    foreach ($bulanList as $b) {
                        echo "<option value=\"$b\">$b</option>";
                    }
                    ?>
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">Saldo Awal</label>
                <input type="text" name="saldo_awal" class="form-control" required>
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-4">
                <label class="form-label">Saldo Akhir</label>
                <input type="text" name="saldo_akhir" class="form-control" required>
            </div>
        </div>

        <div class="mb-3">
            <label class="form-label">Upload Rekening Koran (PDF/JPG/PNG)</label>
            <input type="file" name="upload_file" class="form-control">
        </div>

        <div class="text-end">
            <a href="list_rekening_koran.php" class="btn btn-secondary">
                <i class="bi bi-arrow-left"></i> Kembali
            </a>
            <button type="submit" name="simpan" class="btn btn-primary">
                <i class="bi bi-save"></i> Simpan
            </button>
        </div>

    </form>
    </div>
</div>
</div>

</body>
</html>
