<div class="card card-primary">
	<div class="card-header">
		<h3 class="card-title">
			<i class="fa fa-edit"></i> Tambah Data</h3>
	</div>
	<form action="" method="post" enctype="multipart/form-data">
		<div class="card-body">

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">NIK Bayi</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="nik" name="nik" placeholder="NIK Bayi" required>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Nama</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="nama" name="nama" placeholder="Nama Bayi" required>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Tgl Lahir</label>
				<div class="col-sm-3">
					<input type="date" class="form-control" id="tgl_lh" name="tgl_lh" required>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Jenis Kelain</label>
				<div class="col-sm-3">
					<select name="jekel" id="jekel" class="form-control">
						<option>- Pilih -</option>
						<option>LK</option>
						<option>PR</option>
					</select>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Keluarga</label>
				<div class="col-sm-6">
					<select name="id_kk" id="id_kk" class="form-control select2bs4" required>
						<option selected="selected">- Pilih KK -</option>
						<?php
                        // ambil data dari database
                        $query = "select * from tb_kk";
                        $hasil = mysqli_query($koneksi, $query);
                        while ($row = mysqli_fetch_array($hasil)) {
                        ?>
						<option value="<?php echo $row['id_kk'] ?>">
							<?php echo $row['no_kk'] ?>
							-
							<?php echo $row['kepala'] ?>
						</option>
						<?php
                        }
                        ?>
					</select>
				</div>


			</div>
			<div class="card-footer">
				<input type="submit" name="Simpan" value="Simpan" class="btn btn-info">
				<a href="?page=data-lahir" title="Kembali" class="btn btn-secondary">Batal</a>
			</div>
	</form>
	</div>

	<?php

    if (isset ($_POST['Simpan'])){
    //mulai proses simpan data
        $sql_simpan = "INSERT INTO tb_lahir (nama, tgl_lh, jekel, id_kk) VALUES (
            '".$_POST['nama']."',
			'".$_POST['tgl_lh']."',
            '".$_POST['jekel']."',
            '".$_POST['id_kk']."')";
        $query_simpan = mysqli_query($koneksi, $sql_simpan);

        // Otomatis tambah ke Data Penduduk
        // Ambil data alamat dari KK
        $id_kk = $_POST['id_kk'];
        $sql_kk = "SELECT desa, rt, rw FROM tb_kk WHERE id_kk='$id_kk'";
        $q_kk = mysqli_query($koneksi, $sql_kk);
        $d_kk = mysqli_fetch_array($q_kk);
        $desa = $d_kk['desa'];
        $rt = $d_kk['rt'];
        $rw = $d_kk['rw'];

        $sql_pdd = "INSERT INTO tb_pdd (nik, nama, tempat_lh, tgl_lh, jekel, desa, rt, rw, agama, kawin, pekerjaan, status) VALUES (
            '".$_POST['nik']."',
            '".$_POST['nama']."',
            '-',
            '".$_POST['tgl_lh']."',
            '".$_POST['jekel']."',
            '$desa', '$rt', '$rw',
            '-', 'Belum', 'Belum/Tidak Bekerja', 'Ada')";
        $query_pdd = mysqli_query($koneksi, $sql_pdd);
        
        // Ambil ID Penduduk yang baru dibuat
        $id_pend_baru = mysqli_insert_id($koneksi);

        // Masukkan ke Anggota KK
        $sql_anggota = "INSERT INTO tb_anggota (id_kk, id_pend, hubungan) VALUES (
            '$id_kk',
            '$id_pend_baru',
            'Anak')";
        $query_anggota = mysqli_query($koneksi, $sql_anggota);

        mysqli_close($koneksi);

    if ($query_simpan && $query_pdd && $query_anggota) {
      echo "<script>
      Swal.fire({title: 'Tambah Data Berhasil',text: '',icon: 'success',confirmButtonText: 'OK'
      }).then((result) => {if (result.value){
          window.location = 'index.php?page=data-lahir';
          }
      })</script>";
      }else{
      echo "<script>
      Swal.fire({title: 'Tambah Data Gagal',text: '',icon: 'error',confirmButtonText: 'OK'
      }).then((result) => {if (result.value){
          window.location = 'index.php?page=add-lahir';
          }
      })</script>";
    }}
     //selesai proses simpan data
