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

class Petugas extends CI_Controller
{
    /**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
    }

    function index()
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Petugas';

        $data['petugas'] = $this->db->query("SELECT * from tbl_mst_petugas");

        $this->template->load('includes/header', 'petugas/view_petugas', $data);
    }

    function add_view()
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Petugas';

        $this->template->load('includes/header', 'petugas/add_petugas', $data);
    }

    function update_view($mst_petugas_id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Petugas';

        $data['petugas'] = $this->db->query("SELECT * from tbl_mst_petugas where mst_petugas_id='$mst_petugas_id'");

        $this->template->load('includes/header', 'petugas/update_petugas', $data);
    }

    function add()
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');

        $mst_petugas_kode = "PT-" . rand(1000, 9999);
        $mst_petugas_nama_lengkap = $this->input->post("txt_mst_petugas_nama_lengkap", true);
        $mst_petugas_email = $this->input->post("txt_mst_petugas_email", true);

        $password = $this->input->post("txt_mst_petugas_password", true);
        $mst_petugas_password = sha1($password);
        $mst_petugas_status = $this->input->post("txt_mst_petugas_status", true);

        $query = $this->db->query("insert into tbl_mst_petugas
            (mst_petugas_kode,
            mst_petugas_nama_lengkap,
			mst_petugas_email,
            mst_petugas_password,
            mst_petugas_status) values(
                '$mst_petugas_kode',
                '$mst_petugas_nama_lengkap',
				'$mst_petugas_email',
                '$mst_petugas_password',
                '$mst_petugas_status')");

        if ($query) {
            $this->session->set_flashdata('berhasil', 'Sukses menyimpan data');
        } else {
            $this->session->set_flashdata('gagal', 'Gagal menyimpan data');
        }



        redirect("petugas/hakakses");
    }


    function update($mst_petugas_id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');


        $mst_petugas_nama_lengkap = $this->input->post("txt_mst_petugas_nama_lengkap", true);
        $mst_petugas_email = $this->input->post("txt_mst_petugas_email", true);
        $password = $this->input->post("txt_mst_petugas_password", true);
        $mst_petugas_password = sha1($password);


        $mst_petugas_status = $this->input->post("txt_mst_petugas_status", true);

        if ($password == "") {
            $this->db->query("UPDATE tbl_mst_petugas set
				mst_petugas_nama_lengkap='$mst_petugas_nama_lengkap',
				mst_petugas_email='$mst_petugas_email',
				mst_petugas_status='$mst_petugas_status'
				where 
				mst_petugas_id='$mst_petugas_id'");
            $this->session->set_flashdata('berhasil', 'Sukses update data');
        } else {
            $this->db->query("UPDATE tbl_mst_petugas set
				mst_petugas_nama_lengkap='$mst_petugas_nama_lengkap',
				mst_petugas_email='$mst_petugas_email',
				mst_petugas_password='$mst_petugas_password',
				mst_petugas_status='$mst_petugas_status'
				where mst_petugas_id='$mst_petugas_id'");
            $this->session->set_flashdata('berhasil', 'Sukses update data');
        }



        redirect('petugas/hakakses');
    }

    function hapus($mst_petugas_id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/petugas');


        $query = $this->db->query("DELETE from tbl_mst_petugas where mst_petugas_id='$mst_petugas_id'");
        if ($query) {
            $this->session->set_flashdata('berhasil', 'Sukses hapus data');
        } else {
            $this->session->set_flashdata('gagal', 'Gagal hapus data');
        }

        $data['title'] = 'Bons - Petugas';
        $data['header'] = 'Data Petugas';

        redirect('petugas/petugas');
    }
}
