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

	function __construct()
	{
		parent::__construct();
		$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 JENIS PELAYANAN
	public function index()
	{
		$data['data'] = $this->M_jenis_pelayanan->list_data()->result();
		$this->load->view('jenisPelayanan', $data);
	}

	// TAMBAH JENIS PELAYANAN
	function save()
	{

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

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

			$this->session->set_flashdata('success', 'Data berhasil disimpan !');
			$this->M_jenis_pelayanan->insert($data, 'jenis_pelayanan');
			redirect('JenisPelayanan');
		} else {
			redirect('JenisPelayanan');
		}
	}

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

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

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

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

			$this->session->set_flashdata('success', 'Data berhasil diubah !');
			$this->M_jenis_pelayanan->update($where, $data, 'jenis_pelayanan');
			redirect('JenisPelayanan');
		} else {
			redirect('JenisPelayanan');
		}
	}

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