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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'kader') {
    header("Location: ../login.php");
    exit;
}

if (isset($_POST['simpan'])) {
    $nama = $_POST['nama_balita'];
    $nik = $_POST['nik_balita'];
    $tgl_lahir = $_POST['tgl_lahir'];
    $jk = $_POST['jenis_kelamin'];
    $ayah = $_POST['nama_ayah'];
    $ibu = $_POST['nama_ibu'];
    $nik_ibu = $_POST['nik_ibu_kunci'];
    $anak_ke = $_POST['anak_ke'];
    $bb_lahir = $_POST['bb_lahir'];
    $pb_lahir = $_POST['pb_lahir'];
    $rt = $_POST['rt'];
    $rw = $_POST['rw'];
    $dusun = $_POST['dusun'];
    $id_posyandu = $_SESSION['posyandu'];

    try {
        $stmt = $pdo->prepare("INSERT INTO balita (nama_balita, nik_balita, tgl_lahir, jenis_kelamin, nama_ayah, nama_ibu, nik_ibu_kunci, anak_ke, bb_lahir, pb_lahir, rt, rw, dusun, id_posyandu) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        $stmt->execute([$nama, $nik, $tgl_lahir, $jk, $ayah, $ibu, $nik_ibu, $anak_ke, $bb_lahir, $pb_lahir, $rt, $rw, $dusun, $id_posyandu]);
        echo "<script>alert('Data Balita Berhasil Ditambah!'); window.location='balita_index.php';</script>";
    } catch (PDOException $e) {
        $error = "Gagal simpan! NIK mungkin sudah ada.";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah Balita - Posyandu</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f8f9fa; }
        .form-card { border: none; border-radius: 20px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        .form-label { font-weight: bold; font-size: 0.85rem; color: #555; }
        .btn-save { border-radius: 10px; padding: 12px; font-weight: bold; }
    </style>
</head>
<body>

<div class="bg-primary text-white p-3 mb-4 shadow-sm">
    <div class="container d-flex align-items-center">
        <a href="balita_index.php" class="text-white text-decoration-none me-3"><i class="fa fa-arrow-left"></i></a>
        <h5 class="mb-0 fw-bold">Tambah Balita Baru</h5>
    </div>
</div>

<div class="container pb-5">
    <?php if(isset($error)): ?> <div class="alert alert-danger"><?= $error ?></div> <?php endif; ?>

    <div class="card form-card">
        <div class="card-body p-4">
            <form method="POST">
                <h6 class="text-primary fw-bold mb-3">Identitas Balita</h6>
                <div class="mb-3">
                    <label class="form-label">Nama Lengkap Balita</label>
                    <input type="text" name="nama_balita" class="form-control" placeholder="Nama lengkap" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">NIK Balita</label>
                    <input type="number" name="nik_balita" class="form-control" placeholder="16 digit NIK">
                </div>
                <div class="row">
                    <div class="col-6 mb-3">
                        <label class="form-label">Tgl Lahir</label>
                        <input type="date" name="tgl_lahir" class="form-control" required>
                    </div>
                    <div class="col-6 mb-3">
                        <label class="form-label">Jenis Kelamin</label>
                        <select name="jenis_kelamin" class="form-select" required>
                            <option value="L">Laki-laki</option>
                            <option value="P">Perempuan</option>
                        </select>
                    </div>
                </div>

                <h6 class="text-primary fw-bold mt-3 mb-3">Identitas Orang Tua</h6>
                <div class="mb-3">
                    <label class="form-label">Nama Ayah</label>
                    <input type="text" name="nama_ayah" class="form-control">
                </div>
                <div class="mb-3">
                    <label class="form-label">Nama Ibu</label>
                    <input type="text" name="nama_ibu" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">NIK Ibu (Penting untuk Login Ibu)</label>
                    <input type="number" name="nik_ibu_kunci" class="form-control" placeholder="16 digit NIK Ibu" required>
                </div>
                
                <div class="row">
                    <div class="col-4 mb-3">
                        <label class="form-label">Anak Ke-</label>
                        <input type="number" name="anak_ke" class="form-control">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">BBL (kg)</label>
                        <input type="number" step="0.01" name="bb_lahir" class="form-control" placeholder="0.00">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">PBL (cm)</label>
                        <input type="number" step="0.1" name="pb_lahir" class="form-control" placeholder="0.0">
                    </div>
                </div>

                <h6 class="text-primary fw-bold mt-3 mb-3">Alamat</h6>
                <div class="row">
                    <div class="col-4 mb-3">
                        <label class="form-label">RT</label>
                        <input type="text" name="rt" class="form-control" placeholder="001">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">RW</label>
                        <input type="text" name="rw" class="form-control" placeholder="001">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">Dusun</label>
                        <input type="text" name="dusun" class="form-control" placeholder="Nama dusun">
                    </div>
                </div>

                <div class="d-grid mt-4">
                    <button type="submit" name="simpan" class="btn btn-primary btn-save shadow">SIMPAN DATA BALITA</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
</body>
</html>