<?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');

    }

    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

		if(!empty($this->input->get('id_alatEdit'))){
			$this->data['edit'] = $this->m_peralatan->getPeralatan(false, false, false, $this->input->get('id_alatEdit'));
		}

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

	function create_edit(){
		$data = $this->input->post();

		$param = array(
			"nama_alat" => $data['nama_alat'],
      "jenis_alat" => $data['jenis_alat'],
      "jenis_uji" => $data['jenis_uji'],
			"keterangan" => $data['keterangan'],
			"last_modified_user" => $this->idUser
		);
		$status = $this->m_peralatan->createEdit($param, $data['id_alat']);

		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');
	}

	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');
	}
}
