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

class DataStunting extends CI_Controller
{

  public function __construct()
  {
    parent::__construct();
    // $this->load->library('notif');
    $this->load->model("ModelDataStunting");
    $this->load->model("ModelDataDesa");
    $this->load->library('PHPExcel');
  }

  function index()
  {
    $stunting = $this->ModelDataStunting->get_data();
    $data = array(
      'body' => 'DataStunting/list',
      'stunting' => $stunting
    );
    $this->load->view('index', $data);
  }
  public function input()
  {
    $stunting = $this->ModelDataStunting->get_data();
    $desa = $this->ModelDataDesa->get_data();
    $data = array(
      'body' => 'DataStunting/input',
      'form' => 'DataStunting/form',
      'stunting' => $stunting,
      'desa' => $desa,
    );
    $this->load->view('index', $data);
  }
  public function insert()
  {
    $data = array(
      'id_desa' => $this->input->post('id_desa'),
      'tahun' => $this->input->post('tahun'),
      'total' => $this->input->post('total'),
    );
    if ($this->db->insert("stunting", $data)) {
      $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Input Data"));
      redirect(base_url() . "DataStunting");
    } else {
      $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Input Data"));
      redirect(base_url() . "DataStunting");
    }
    // echo json_encode($data);
  }

  function edit()
  {
    $id = $this->uri->segment(3);
    $stunting = $this->ModelDataStunting->get_data_edit($id);
    $desa = $this->ModelDataDesa->get_data();
    $data = array(
      'body' => 'DataStunting/edit',
      'form' => 'DataStunting/form',
      'stunting' => $stunting,
      'desa' => $desa,
    );
    $this->load->view('index', $data);
  }
  function update()
  {
    $id = $this->uri->segment(3);
    $data = array(
      'id_desa' => $this->input->post('id_desa'),
      'tahun' => $this->input->post('tahun'),
      'total' => $this->input->post('total'),
    );
    $this->db->where("id_stunting", $id);
    if ($this->db->update("stunting", $data)) {
      // code...
      $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Input Data"));
      redirect(base_url() . "DataStunting");
    } else {
      $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Input Data"));
      redirect(base_url() . "DataStunting");
    }
  }

  function delete()
  {
    $id = $this->input->post("id_stunting");
    $this->db->where_in("id_stunting", $id);
    if ($this->db->delete("stunting")) {
      $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Input Data"));
      redirect(base_url() . "DataStunting");
      // code...
    } else {
      $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Input Data"));
      redirect(base_url() . "DataStunting");
    }
  }

  public function import()
  {
    $data = array(
      'body' => 'DataStunting/import',
    );
    $this->load->view('index', $data);
  }

  public function proses_import_excel()
  {
    if (isset($_FILES["fileExcel"]["name"])) {
      $path = $_FILES["fileExcel"]["tmp_name"];
      $object = PHPExcel_IOFactory::load($path);
      $dbxl_stunting = array();
      $log = array();
      foreach ($object->getWorksheetIterator() as $worksheet) {
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();
        for ($row = 4; $row <= $highestRow; $row++) {
          $nama_desa = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
          $total = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
          $tahun = $worksheet->getCellByColumnAndRow(4, $row)->getValue();

          // Panggil model untuk mendapatkan data desa berdasarkan nama_desa
          $data_desa = $this->ModelDataDesa->get_desa_bynama($nama_desa);

          // Debugging: Periksa nilai yang dikembalikan
          // var_dump($data_desa);

          if (is_array($data_desa) && isset($data_desa['id_desa'])) {
            $id_desa = $data_desa['id_desa'];

            $temp_db = array(
              'id_desa' => $id_desa,
              'total' => $total,
              'tahun' => $tahun,
            );
            $cekData = $this->db->get_where("stunting", array("id_desa" => $id_desa, "tahun" => $tahun))->row_array();
            if ($cekData) {
              $this->db->where("id_stunting", $cekData['id_stunting']);
              $this->db->update("stunting", $temp_db);
            } else {
              $dbxl_stunting[] = $temp_db;
            }
          } else {
            $log[] = "Desa dengan nama '$nama_desa' tidak ditemukan di database.";
          }
        }
      }
      if (sizeof($dbxl_stunting) > 0) {
        $this->db->insert_batch("stunting", $dbxl_stunting);
      }
      echo json_encode(
        array(
          "db" => $dbxl_stunting,
          "log" => $log
        )
      );
    } else {
      echo "Tidak ada file yang masuk";
    }
  }


}