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

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

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

	//TAMPIL PELAYANAN
	public function index()
	{
		$id_jenis = $this->input->get('id_jenis_pelayanan');
		$data['data'] = $this->M_pelayanan->list_data($id_jenis)->result();
		$data['jenis_pelayanan'] = $this->M_jenis_pelayanan->get_all()->result();
		$this->load->view('pelayanan', $data);
	}

	// TAMBAH PELAYANAN
	function save()
	{

		$this->form_validation->set_rules('nama', 'Nama Pelayanan', 'required|is_unique[pelayanan.nama]');
		if ($this->form_validation->run() == true) {
			$nama = $this->input->post('nama');
			$id_jenis_pelayanan = $this->input->post('id_jenis_pelayanan');

			$data = array(
				'nama' => $nama,
				'id_jenis_pelayanan' => $id_jenis_pelayanan,
				'created_at' => date('Y-m-d H:i:s')
			);

			$this->session->set_flashdata('success', 'Data berhasil disimpan !');
			$this->M_pelayanan->insert($data, 'pelayanan');
			redirect('Pelayanan');
		} else {
			redirect('Pelayanan');
		}
	}

	// EDIT PELAYANAN
	function edit($id)
	{
		$where = array('id' => $id);
		$data['data'] = $this->M_pelayanan->edit($where, 'pelayanan')->row_array();
		echo json_encode($data);
	}

	// UPDATE PELAYANAN
	function update()
	{
		$this->form_validation->set_rules('nama', 'Nama Pelayanan', 'required');
		if ($this->form_validation->run() == true) {
			$id = $this->input->post('id');
			$nama = $this->input->post('nama');
			$id_jenis_pelayanan = $this->input->post('id_jenis_pelayanan');

			$data = array(
				'nama' => $nama,
				'id_jenis_pelayanan' => $id_jenis_pelayanan,
				'updated_at' => date('Y-m-d H:i:s')
			);

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

			$this->session->set_flashdata('success', 'Data berhasil diubah !');
			$this->M_pelayanan->update($where, $data, 'pelayanan');
			redirect('Pelayanan');
		} else {
			redirect('Pelayanan');
		}
	}

	// HAPUS PELAYANAN
	public function delete($id)
	{
		$where = array('id' => $id);
		$this->M_pelayanan->delete($where, 'pelayanan');
		$this->session->set_flashdata('hapus', 'Data berhasil dihapus !');
		redirect('Pelayanan');
	}
}
