<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Warga extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        if(!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
        $this->load->model('Warga_model');
        $this->load->model('KartuKeluarga_model');
        $this->load->library('form_validation');
    }

    public function index() {
        $data['title'] = 'Data Warga';
        $data['warga'] = $this->Warga_model->get_all();
        
        // Statistik Dashboard
        $data['statistik'] = $this->Warga_model->get_statistik_dashboard();
        $data['statistik']['total_kk'] = $this->KartuKeluarga_model->count_all();
        
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('warga/index', $data);
        $this->load->view('templates/footer');
    }

    public function tambah() {
        $data['title'] = 'Tambah Warga';
        $data['status_hubungan'] = $this->Warga_model->get_status_hubungan_options();
        $data['status_warga'] = $this->Warga_model->get_status_warga_options();
        
        $this->form_validation->set_rules('nik', 'NIK', 'required|is_unique[warga.nik]');
        $this->form_validation->set_rules('nama_lengkap', 'Nama Lengkap', 'required');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('warga/tambah', $data);
            $this->load->view('templates/footer');
        } else {
            $data_warga = array(
                'nik' => $this->input->post('nik'),
                'no_kk' => $this->input->post('no_kk'),
                'nama_lengkap' => $this->input->post('nama_lengkap'),
                'tempat_lahir' => $this->input->post('tempat_lahir'),
                'tanggal_lahir' => $this->input->post('tanggal_lahir'),
                'jenis_kelamin' => $this->input->post('jenis_kelamin'),
                'status_perkawinan' => $this->input->post('status_perkawinan'),
                'pekerjaan' => $this->input->post('pekerjaan'),
                'agama' => $this->input->post('agama'),
                'status_hubungan' => $this->input->post('status_hubungan'),
                'status_warga' => $this->input->post('status_warga'),
                'keterangan_domisili' => $this->input->post('keterangan_domisili')
            );

            $this->Warga_model->insert($data_warga);
            $this->session->set_flashdata('success', 'Data warga berhasil ditambahkan');
            redirect('warga');
        }
    }

    public function edit($id) {
        $data['title'] = 'Edit Warga';
        $data['warga'] = $this->Warga_model->get_by_id($id);
        $data['status_hubungan'] = $this->Warga_model->get_status_hubungan_options();
        $data['status_warga'] = $this->Warga_model->get_status_warga_options();

        if(!$data['warga']) {
            $this->session->set_flashdata('error', 'Data warga tidak ditemukan');
            redirect('warga');
        }

        $this->form_validation->set_rules('nama_lengkap', 'Nama Lengkap', 'required');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('warga/edit', $data);
            $this->load->view('templates/footer');
        } else {
            $data_warga = array(
                'no_kk' => $this->input->post('no_kk'),
                'nama_lengkap' => $this->input->post('nama_lengkap'),
                'tempat_lahir' => $this->input->post('tempat_lahir'),
                'tanggal_lahir' => $this->input->post('tanggal_lahir'),
                'jenis_kelamin' => $this->input->post('jenis_kelamin'),
                'status_perkawinan' => $this->input->post('status_perkawinan'),
                'pekerjaan' => $this->input->post('pekerjaan'),
                'agama' => $this->input->post('agama'),
                'status_hubungan' => $this->input->post('status_hubungan'),
                'status_warga' => $this->input->post('status_warga'),
                'keterangan_domisili' => $this->input->post('keterangan_domisili')
            );

            $this->Warga_model->update($id, $data_warga);
            $this->session->set_flashdata('success', 'Data warga berhasil diupdate');
            redirect('warga');
        }
    }


public function statistik()
{
    $data['title']    = 'Statistik Kependudukan';
    $data['warga']    = $this->Warga_model->get_all();
    $data['statistik']= $this->Warga_model->get_statistik_dashboard();
    $data['statistik']['total_kk'] = $this->KartuKeluarga_model->count_all();

    $this->load->view('templates/header', $data);
    $this->load->view('templates/sidebar', $data); // ← tambah ini
    $this->load->view('warga/statistik', $data);
    $this->load->view('templates/footer');
}

    public function hapus($id) {
        $this->Warga_model->delete($id);
        $this->session->set_flashdata('success', 'Data warga berhasil dihapus');
        redirect('warga');
    }

    
}