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

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

$id = $_GET['id'];

// 1. Ambil data lama balita
$stmt = $pdo->prepare("SELECT * FROM balita WHERE id_balita = ?");
$stmt->execute([$id]);
$b = $stmt->fetch();

if (!$b) { die("Data balita tidak ditemukan."); }

// 2. Proses Simpan Perubahan
if (isset($_POST['update'])) {
    $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'];

    try {
        $sql = "UPDATE balita SET 
                nama_balita=?, nik_balita=?, tgl_lahir=?, jenis_kelamin=?, 
                nama_ayah=?, nama_ibu=?, nik_ibu_kunci=?, anak_ke=?, 
                bb_lahir=?, pb_lahir=?, rt=?, rw=?, dusun=? 
                WHERE id_balita=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$nama, $nik, $tgl_lahir, $jk, $ayah, $ibu, $nik_ibu, $anak_ke, $bb_lahir, $pb_lahir, $rt, $rw, $dusun, $id]);
        
        echo "<script>alert('Perubahan data berhasil disimpan!'); window.location='balita_index.php';</script>";
        exit;
    } catch (PDOException $e) {
        $error = "Gagal simpan perubahan! Periksa kembali NIK atau koneksi database.";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Balita - Posyandu</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background-color: #f8f9fa; font-family: 'Segoe UI', sans-serif; }
        .bg-header { background: #0d6efd; color: white; padding: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; }
        .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; }
        .section-title { color: #0d6efd; font-weight: bold; border-left: 4px solid #0d6efd; padding-left: 10px; margin-top: 20px; margin-bottom: 15px; }
    </style>
</head>
<body>

<div class="bg-header 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 fs-4"><i class="fa fa-arrow-left"></i></a>
        <h5 class="mb-0 fw-bold">Edit Data Balita</h5>
    </div>
</div>

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

    <div class="card form-card">
        <div class="card-body p-4">
            <form method="POST">
                <h6 class="section-title">Identitas Balita</h6>
                <div class="mb-3">
                    <label class="form-label">Nama Lengkap Balita</label>
                    <input type="text" name="nama_balita" class="form-control" value="<?= htmlspecialchars($b['nama_balita']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">NIK Balita</label>
                    <input type="number" name="nik_balita" class="form-control" value="<?= $b['nik_balita'] ?>">
                </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" value="<?= $b['tgl_lahir'] ?>" 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" <?= $b['jenis_kelamin'] == 'L' ? 'selected' : '' ?>>Laki-laki</option>
                            <option value="P" <?= $b['jenis_kelamin'] == 'P' ? 'selected' : '' ?>>Perempuan</option>
                        </select>
                    </div>
                </div>

                <h6 class="section-title">Identitas Orang Tua</h6>
                <div class="mb-3">
                    <label class="form-label">Nama Ayah</label>
                    <input type="text" name="nama_ayah" class="form-control" value="<?= htmlspecialchars($b['nama_ayah']) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Nama Ibu</label>
                    <input type="text" name="nama_ibu" class="form-control" value="<?= htmlspecialchars($b['nama_ibu']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">NIK Ibu (Kunci Login)</label>
                    <input type="number" name="nik_ibu_kunci" class="form-control" value="<?= $b['nik_ibu_kunci'] ?>" 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" value="<?= $b['anak_ke'] ?>">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">BBL (kg)</label>
                        <input type="number" step="0.1" name="bb_lahir" class="form-control" value="<?= $b['bb_lahir'] ?>">
                    </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" value="<?= $b['pb_lahir'] ?>">
                    </div>
                </div>

                <h6 class="section-title">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" value="<?= $b['rt'] ?>">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">RW</label>
                        <input type="text" name="rw" class="form-control" value="<?= $b['rw'] ?>">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">Dusun</label>
                        <input type="text" name="dusun" class="form-control" value="<?= htmlspecialchars($b['dusun']) ?>">
                    </div>
                </div>

                <div class="d-grid mt-4">
                    <button type="submit" name="update" class="btn btn-primary btn-save shadow">SIMPAN PERUBAHAN DATA</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>