-- Initialize database for SIDAK application

-- Create database if not exists
CREATE DATABASE IF NOT EXISTS `data_penduduk`;
USE `data_penduduk`;

-- Table structure for table `tb_anggota`
CREATE TABLE IF NOT EXISTS `tb_anggota` (
  `id_anggota` int(11) NOT NULL,
  `id_kk` int(11) NOT NULL,
  `id_pend` int(11) NOT NULL,
  `hubungan` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_datang`
CREATE TABLE IF NOT EXISTS `tb_datang` (
  `id_datang` int(11) NOT NULL,
  `nik` varchar(20) NOT NULL,
  `nama_datang` varchar(20) NOT NULL,
  `jekel` enum('LK','PR') NOT NULL,
  `tgl_datang` date NOT NULL,
  `pelapor` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_kk`
CREATE TABLE IF NOT EXISTS `tb_kk` (
  `id_kk` int(11) NOT NULL,
  `no_kk` varchar(30) NOT NULL,
  `kepala` varchar(20) NOT NULL,
  `desa` varchar(20) NOT NULL,
  `rt` varchar(5) NOT NULL,
  `rw` varchar(5) NOT NULL,
  `kec` varchar(20) NOT NULL,
  `kab` varchar(20) NOT NULL,
  `prov` varchar(20) NOT NULL,
  `foto_kk` varchar(255) NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_lahir`
CREATE TABLE IF NOT EXISTS `tb_lahir` (
  `id_lahir` int(11) NOT NULL,
  `nama` varchar(30) NOT NULL,
  `tgl_lh` date NOT NULL,
  `jekel` enum('LK','PR') NOT NULL,
  `id_kk` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_mendu`
CREATE TABLE IF NOT EXISTS `tb_mendu` (
  `id_mendu` int(11) NOT NULL,
  `id_pdd` int(11) NOT NULL,
  `tgl_mendu` date NOT NULL,
  `sebab` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_pdd`
CREATE TABLE IF NOT EXISTS `tb_pdd` (
  `id_pend` int(11) NOT NULL,
  `nik` varchar(20) NOT NULL,
  `nama` varchar(30) NOT NULL,
  `tempat_lh` varchar(20) NOT NULL,
  `tgl_lh` date NOT NULL,
  `jekel` enum('LK','PR') NOT NULL,
  `desa` varchar(20) NOT NULL,
  `rt` varchar(5) NOT NULL,
  `rw` varchar(5) NOT NULL,
  `agama` varchar(15) NOT NULL,
  `kawin` varchar(15) NOT NULL,
  `pekerjaan` varchar(20) NOT NULL,
  `foto_ktp` varchar(255) NULL,
  `status` varchar(10) NOT NULL,
  `kecamatan` varchar(50) NULL,
  `kabupaten` varchar(50) NULL,
  `provinsi` varchar(50) NULL,
  `kewarganegaraan` varchar(10) NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_pengguna`
CREATE TABLE IF NOT EXISTS `tb_pengguna` (
  `id_pengguna` int(11) NOT NULL,
  `nama_pengguna` varchar(30) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(100) NOT NULL,
  `level` enum('admin','kaur') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_pindah`
CREATE TABLE IF NOT EXISTS `tb_pindah` (
  `id_pindah` int(11) NOT NULL,
  `id_pdd` int(11) NOT NULL,
  `tgl_pindah` date NOT NULL,
  `alasan` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Table structure for table `tb_profil`
CREATE TABLE IF NOT EXISTS `tb_profil` (
  `id_profil` int(11) NOT NULL PRIMARY KEY,
  `nama_desa` varchar(50) NOT NULL,
  `alamat` text NOT NULL,
  `kecamatan` varchar(50) NOT NULL,
  `kabupaten` varchar(50) NOT NULL,
  `provinsi` varchar(50) NOT NULL,
  `kepala_desa` varchar(50) NOT NULL,
  `nip_kades` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Add primary keys and indexes
ALTER TABLE `tb_anggota`
  ADD PRIMARY KEY (`id_anggota`),
  ADD KEY `id_kk` (`id_kk`),
  ADD KEY `id_pend` (`id_pend`);

ALTER TABLE `tb_datang`
  ADD PRIMARY KEY (`id_datang`),
  ADD KEY `pelapor` (`pelapor`);

ALTER TABLE `tb_kk`
  ADD PRIMARY KEY (`id_kk`);

ALTER TABLE `tb_lahir`
  ADD PRIMARY KEY (`id_lahir`),
  ADD KEY `id_kk` (`id_kk`);

ALTER TABLE `tb_mendu`
  ADD PRIMARY KEY (`id_mendu`),
  ADD KEY `id_pdd` (`id_pdd`);

ALTER TABLE `tb_pdd`
  ADD PRIMARY KEY (`id_pend`);

ALTER TABLE `tb_pengguna`
  ADD PRIMARY KEY (`id_pengguna`);

ALTER TABLE `tb_pindah`
  ADD PRIMARY KEY (`id_pindah`),
  ADD KEY `id_pdd` (`id_pdd`);

-- Add auto increments
ALTER TABLE `tb_anggota`
  MODIFY `id_anggota` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_datang`
  MODIFY `id_datang` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_kk`
  MODIFY `id_kk` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_lahir`
  MODIFY `id_lahir` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_mendu`
  MODIFY `id_mendu` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_pdd`
  MODIFY `id_pend` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_pengguna`
  MODIFY `id_pengguna` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tb_pindah`
  MODIFY `id_pindah` int(11) NOT NULL AUTO_INCREMENT;

-- Add foreign key constraints
ALTER TABLE `tb_anggota`
  ADD CONSTRAINT `tb_anggota_ibfk_1` FOREIGN KEY (`id_kk`) REFERENCES `tb_kk` (`id_kk`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `tb_anggota_ibfk_2` FOREIGN KEY (`id_pend`) REFERENCES `tb_pdd` (`id_pend`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `tb_datang`
  ADD CONSTRAINT `tb_datang_ibfk_1` FOREIGN KEY (`pelapor`) REFERENCES `tb_pengguna` (`id_pengguna`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `tb_lahir`
  ADD CONSTRAINT `tb_lahir_ibfk_1` FOREIGN KEY (`id_kk`) REFERENCES `tb_kk` (`id_kk`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `tb_mendu`
  ADD CONSTRAINT `tb_mendu_ibfk_1` FOREIGN KEY (`id_pdd`) REFERENCES `tb_pdd` (`id_pend`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `tb_pindah`
  ADD CONSTRAINT `tb_pindah_ibfk_1` FOREIGN KEY (`id_pdd`) REFERENCES `tb_pdd` (`id_pend`) ON DELETE CASCADE ON UPDATE CASCADE;

-- Insert default user (admin/admin)
INSERT INTO `tb_pengguna` (`id_pengguna`, `nama_pengguna`, `username`, `password`, `level`) VALUES
(1, 'Administrator', 'admin', MD5('admin'), 'admin'),
(2, 'Kepala Urusan', 'kaur', MD5('kaur'), 'kaur');

-- Insert default profile data
INSERT INTO `tb_profil` (`id_profil`, `nama_desa`, `alamat`, `kecamatan`, `kabupaten`, `provinsi`, `kepala_desa`, `nip_kades`) VALUES
(1, 'Desa Percontohan', 'Jl. Contoh No. 1', 'Kecamatan Contoh', 'Kabupaten Contoh', 'Jawa Barat', 'Budi Santoso', '123456789');

-- Insert sample KK data
INSERT INTO `tb_kk` (`id_kk`, `no_kk`, `kepala`, `desa`, `rt`, `rw`, `kec`, `kab`, `prov`) VALUES
(1, '32010101010001', 'Budi Santoso', 'Sukamaju', '01', '02', 'Cibinong', 'Bogor', 'Jawa Barat'),
(2, '32010101010002', 'Siti Aminah', 'Sukamaju', '02', '02', 'Cibinong', 'Bogor', 'Jawa Barat');

-- Insert sample penduduk data
INSERT INTO `tb_pdd` (`id_pend`, `nik`, `nama`, `tempat_lh`, `tgl_lh`, `jekel`, `desa`, `rt`, `rw`, `agama`, `kawin`, `pekerjaan`, `status`) VALUES
(1, '3201010101000001', 'Budi Santoso', 'Bogor', '1980-01-01', 'LK', 'Sukamaju', '01', '02', 'Islam', 'Kawin', 'Karyawan', 'Ada'),
(2, '3201010101000002', 'Siti Aminah', 'Bogor', '1985-05-05', 'PR', 'Sukamaju', '02', '02', 'Islam', 'Cerai Mati', 'Wiraswasta', 'Ada'),
(3, '3201010101000003', 'Ani Santoso', 'Bogor', '2005-01-01', 'PR', 'Sukamaju', '01', '02', 'Islam', 'Belum', 'Pelajar', 'Ada'),
(4, '3201010101000004', 'Ahmad Santoso', 'Bogor', '2008-01-01', 'LK', 'Sukamaju', '01', '02', 'Islam', 'Belum', 'Pelajar', 'Ada'),
(5, '3201010101000005', 'Dewi Lestari', 'Bandung', '1990-10-10', 'PR', 'Sukamaju', '03', '03', 'Islam', 'Belum', 'Guru', 'Pindah'),
(6, '3201010101000006', 'Alm. Kakek', 'Jakarta', '1950-01-01', 'LK', 'Sukamaju', '01', '02', 'Islam', 'Cerai Mati', 'Pensiunan', 'Meninggal');

-- Insert sample anggota keluarga
INSERT INTO `tb_anggota` (`id_kk`, `id_pend`, `hubungan`) VALUES
(1, 1, 'Kepala Keluarga'),
(1, 3, 'Anak'),
(1, 4, 'Anak'),
(2, 2, 'Kepala Keluarga');