<?php
defined('BASEPATH') OR exit('No direct script access allowed');
date_default_timezone_set("Asia/Jakarta");

class Balita extends MY_Controller{
  
  function __construct()
  {
    parent::__construct();
    $this->load->model('login_m'); 
    $this->load->model('Balita_m');
    $this->load->model('Kriteria_m');
    $this->load->model('Alternatif_m');
    $this->load->model('BobotPreferensi_m');
    $this->load->model('Hasil_m');
  }

  public function index()
  { 
    // Memeriksa apakah pengguna telah login sebelum melanjutkan eksekusi fungsi
    // memanggil MY_controller di folder core
    $this->check_login();
    
    $email = $this->session->userdata('email');
    $data['profil'] = $this->login_m->get_row(['email' =>$email ]); 
    
    $data['list_balita'] = $this->Balita_m->get_all_data();   
    $data['title']  = 'Kelola Data Balita';
    $data['menu'] = 1;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/balita/v_balita',$data);
    $this->load->view('admin/template/footer');
  }

// fungsi untuk proses tambah , edit dan hapus
  public function prosesbalita(){
    if ($this->POST('tambah')) {
      $data = [ 
        'nama_balita' => $this->input->post('nama_balita'), // Mengambil nilai 'nama_balita' dari input POST
        'jk' => $this->input->post('jk'), // Mengambil nilai 'jk' (jenis kelamin) dari input POST
        'umur' => $this->input->post('umur'), // Mengambil nilai 'umur' dari input POST
        'nama_ortu' => $this->input->post('nama_ortu'), // Mengambil nilai 'nama_ortu' dari input POST
        'berat_badan' => $this->input->post('berat_badan'), // Mengambil nilai 'berat_badan' dari input POST
        'tinggi_badan' => $this->input->post('tinggi_badan'), // Mengambil nilai 'tinggi_badan' dari input POST
        'status_gizi' => $this->input->post('status_gizi') // Mengambil nilai 'tinggi_badan' dari input POST
        
      ];
      $this->Balita_m->insert($data);
      $this->flashmsg('Data Balita berhasil ditambah!', 'success');
      redirect('balita');
      exit();
    }

    if ($this->POST('edit')) {
      $id = $this->POST('id_balita');
      $data = [ 
        'nama_balita' => $this->input->post('nama_balita'), // Mengambil nilai 'nama_balita' dari input POST
        'jk' => $this->input->post('jk'), // Mengambil nilai 'jk' (jenis kelamin) dari input POST
        'umur' => $this->input->post('umur'), // Mengambil nilai 'umur' dari input POST
        'nama_ortu' => $this->input->post('nama_ortu'), // Mengambil nilai 'nama_ortu' dari input POST
        'berat_badan' => $this->input->post('berat_badan'), // Mengambil nilai 'berat_badan' dari input POST
        'status_gizi' => $this->input->post('status_gizi') // Mengambil nilai 'status_gizi' dari input POST
        
      ];

      $this->Balita_m->update($id,$data);
      $this->flashmsg('Data Balita berhasil disimpan!', 'success');
      redirect('balita');
      exit();
    }

    if ($this->POST('hapus')) {
      $this->Balita_m->delete($this->POST('id_balita'));
      $this->flashmsg('Data Balita berhasil dihapus!', 'success');
      redirect('balita');
      exit();
    } 
    
  } 

  public function detailbalita($id)
  { 
    // Memeriksa apakah pengguna telah login sebelum melanjutkan eksekusi fungsi
    // memanggil MY_controller di folder core
    $this->check_login();
    
    $email = $this->session->userdata('email');
    $data['profil'] = $this->login_m->get_row(['email' =>$email ]); 
    
    $data['data_balita'] = $this->Balita_m->get_by_id($id);   
    $data['title']  = 'Detail Data Balita';
    $data['menu'] = 1;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/balita/v_detailbalita',$data);
    $this->load->view('admin/template/footer');
  } 

  public function proseshitunggizi(){
      $id = $this->POST('id_balita');
      $umur = $this->POST('umur');
      $tinggi_badan = $this->POST('tinggi_badan');

      
      $data = [
        'status_gizi' => ''
        
      ];

      $this->Balita_m->update($id,$data);    
      $this->flashmsg('Hitung Status Gizi berhasil!', 'success');
      redirect('balita/detailbalita/'.$id);
      exit();
  }

  // perhitungan metode SAW Tiap Balita
  public function perhitungan($id)
  { 
    // Memeriksa apakah pengguna telah login sebelum melanjutkan eksekusi fungsi
    // memanggil MY_controller di folder core
    $this->check_login();
    
    $email = $this->session->userdata('email');
    $data['profil'] = $this->login_m->get_row(['email' =>$email ]); 
    
    $data['data_balita'] = $this->Balita_m->get_by_id($id);  
    $data['list_kriteria'] = $this->Kriteria_m->get_all_data(); 
    $data['list_bobotpreferensi'] = $this->BobotPreferensi_m->get_all_data();
    $data['list_alternatif'] = $this->Alternatif_m->get_all_data(); 
    $data['title']  = 'Detail Data Balita';
    $data['menu'] = 1;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/hasil/v_perhitungan_saw',$data);
    $this->load->view('admin/template/footer');
  } 

  public function proses_perhitungan()
  {
    // Mendapatkan data dari form
    $bobot = $this->input->post('bobot');
    $periode = $this->input->post('periode');
    $id_balita = $this->input->post('id_balita');
    $data = array(
      'periode' => $periode,
      'id_balita' => $id_balita
    );
    $this->Hasil_m->insertperhitungan($data);
    $id_perhitungan = $this->db->insert_id();

    // Looping data bobot untuk disimpan ke database
    foreach ($bobot as $id_alternatif => $kriteria) {
      foreach ($kriteria as $id_kriteria => $id_bobot) {
            $data2 = array(
                'id_perhitungan' => $id_perhitungan,
                // 'id_perhitungan' => '1',
                'id_alternatif' => $id_alternatif,
                'id_kriteria' => $id_kriteria,
                'id_bobot' => $id_bobot
            );
            $this->Hasil_m->insertdetailperhitungan($data2);
        }
    }
    // var_dump($data2);
    // die;
    
    $this->flashmsg('Proses Perhitungan berhasil!', 'success');
    redirect('hasil/detailhasil/'.$id_perhitungan);
    exit();
    // Redirect atau tampilkan pesan sukses
  }
} 
?>