<?php
defined('BASEPATH') or exit('No direct script access allowed');
date_default_timezone_set('Asia/Jakarta');
class Pengunjung extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('M_pengunjung');
		$this->load->model('M_pasien');
		$this->load->model('M_pelayanan');
		$this->load->helper('url');
		$this->load->library('form_validation');

		if ($this->session->userdata('status') != "login") {
			redirect(base_url("Auth"));
		}
	}

	//TAMPIL PENGUNJUNG
	public function index()
	{
		$bulan = $this->input->get('bulan');
		$tahun = $this->input->get('tahun');
		$id_pelayanan = $this->input->get('id_pelayanan');
		$data['data'] = $this->M_pengunjung->list_data($id_pelayanan, $bulan, $tahun)->result();
		$data['pasien'] = $this->M_pasien->get_all()->result();
		$data['pelayanan'] = $this->M_pelayanan->get_all()->result();
		$data['tahun'] = $this->M_pengunjung->get_tahun()->result();
		$this->load->view('pengunjung', $data);
	}

	// TAMBAH PENGUNJUNG
	function save()
	{

		$this->form_validation->set_rules('tanggal', 'tanggal', 'required');
		$this->form_validation->set_rules('id_pasien', 'Pasien', 'required');
		$this->form_validation->set_rules('id_pelayanan', 'Pelayanan', 'required');
		if ($this->form_validation->run() == true) {
			$tanggal = $this->input->post('tanggal');
			$id_pasien = $this->input->post('id_pasien');
			$id_pelayanan = $this->input->post('id_pelayanan');
			$keterangan = $this->input->post('keterangan');
			$umur_bulan = $this->input->post('umur_bulan');

			$data = array(
				'tanggal' => $tanggal,
				'id_pasien' => $id_pasien,
				'id_pelayanan' => $id_pelayanan,
				'keterangan' => $keterangan,
				'umur_bulan' => $umur_bulan,
				'created_at' => date('Y-m-d H:i:s')
			);

			$this->session->set_flashdata('success', 'Data berhasil disimpan !');
			$this->M_pengunjung->insert($data, 'pengunjung');
			redirect('Pengunjung');
		} else {
			redirect('Pengunjung');
		}
	}

	// EDIT PENGUNJUNG
	function edit($id)
	{
		$where = array('id' => $id);
		$data['data'] = $this->M_pengunjung->get_detail($id)->row_array();
		echo json_encode($data);
	}

	// UPDATE PENGUNJUNG
	function update()
	{
		$this->form_validation->set_rules('tanggal', 'tanggal', 'required');
		$this->form_validation->set_rules('id_pasien', 'Pasien', 'required');
		$this->form_validation->set_rules('id_pelayanan', 'Pelayanan', 'required');
		if ($this->form_validation->run() == true) {
			$id = $this->input->post('id');
			$tanggal = $this->input->post('tanggal');
			$id_pasien = $this->input->post('id_pasien');
			$id_pelayanan = $this->input->post('id_pelayanan');
			$keterangan = $this->input->post('keterangan');
      $umur_bulan = $this->input->post('umur_bulan');

			$data = array(
				'tanggal' => $tanggal,
				'id_pasien' => $id_pasien,
				'id_pelayanan' => $id_pelayanan,
				'keterangan' => $keterangan,
        'umur_bulan' => $umur_bulan,
				'updated_at' => date('Y-m-d H:i:s')
			);

			$where = array(
				'id' => $id
			);

			$this->session->set_flashdata('success', 'Data berhasil diubah !');
			$this->M_pengunjung->update($where, $data, 'pengunjung');
			redirect('Pengunjung');
		} else {
			redirect('Pengunjung');
		}
	}

	// HAPUS PENGUNJUNG
	public function delete($id)
	{
		$where = array('id' => $id);
		$this->M_pengunjung->delete($where, 'pengunjung');
		$this->session->set_flashdata('hapus', 'Data berhasil dihapus !');
		redirect('Pengunjung');
	}

	function save_bulk()
	{
		for ($i = 0; $i < 34; $i++) {
			$data = array(
				'tanggal' => '2024-03-10',
				'id_pasien' => 3,
				'id_pelayanan' => 3,
				'keterangan' => null,
				'created_at' => date('Y-m-d H:i:s')
			);

			$this->M_pengunjung->insert($data, 'pengunjung');
		}

		echo "save bulk pengunjung";
	}
}
