<?php

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

class Peralatan extends MY_Controller {

    protected $template = "app";
    protected $module = "peralatan";

	protected $per_page = 20;

    public function __construct() {
        parent::__construct();
		$this->load->model('m_peralatan');
		$this->load->model('m_master');

    }

    public function index() {
        $this->data['halaman'] = "Daftar Data Peralatan";
        $this->data['sub_halaman'] = "";


		$total_data = $this->m_peralatan->getPeralatan(true);
        $this->data['total_data'] = $total_data;

        //CONFIG PAGINATION
        $this->load->library('pagination');

        $url = site_url('inventaris/peralatan/index');
        $per_page = $this->per_page;
        $uri_segment = 3;

        $this->data['link_paging'] = $this->paging->set_paging2($url, $total_data, $per_page,$uri_segment);
        if ($this->uri->segment($uri_segment)) {
            $this->cur_page = $page = $this->uri->segment($uri_segment);
        } else {
            $this->cur_page = $page = 0;
        }

        $this->data['per_page']     = $per_page;
        $this->data['data']         = $this->m_peralatan->getPeralatan(false, $per_page, $page);
        //END CONFIG PAGINATION

        $this->render('index');
    }

	function create_edit($id = ''){
        if($this->input->post()){
            $data = $this->input->post();
            $json_p_uji = json_encode($data['jenis_uji']);
            $param = array(
                "nama_alat" => $data['nama_alat'],
                "jenis_alat" => $data['jenis_alat'],
                "jenis_uji" => $json_p_uji,
                "keterangan" => $data['keterangan'],
                "kategori" => $data['kategori'],
                "last_modified_user" => $this->id_user
            );
            if($id == ''){
                $status = $this->m_peralatan->createEdit($param);
            }else{
                $status = $this->m_peralatan->createEdit($param, $id);
            }

            if($status){
                $msg = 'Peralatan berhasil disimpan';
                $this->session->set_flashdata('msg', $msg);
            }else{
                $msg = 'Peralatan gagal disimpan';
                $this->session->set_flashdata('msgw', $msg);
            }
            redirect('inventaris/peralatan/index');
        }

        if($id != ''){
            $this->data['edit'] = $this->m_peralatan->get_alat_by_id($id);
        }

        $this->data['halaman'] = "Tambah Data Peralatan";
        $this->data['sub_halaman'] = "";

        $this->data['parameter_uji'] = $this->m_master->getDataMaster('parameter_uji');
        $this->data['jenis_alat'] = ['Peralatan Utama', 'Pendukung', 'Penunjang Pengujian'];
        $this->data['kategori'] = ['Oven', 'Digestor', 'Furnace', 'Kjeltec', 'Buret', 'Neraca', 'Kode Fibertec', 'Lain-lain'];
        $this->render('create_edit');
	}

	function hapus_alat(){
		$id_alat = $this->input->get('id_alat');

		$status = $this->m_peralatan->hapusAlat($id_alat);

		if($status){
			$msg = 'Peralatan berhasil dihapus';
			$this->session->set_flashdata('msg', $msg);
		}else{
			$msg = 'Peralatan gagal dihapus';
			$this->session->set_flashdata('msgw', $msg);
		}
		redirect('inventaris/peralatan/index');
	}
}
