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

class Production_cash_item extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('az_auth');
        az_check_auth('production_cash_item');
        $this->table = 'production_cash_item';
        $this->load->helper('az_crud');
        $this->load->library('AZApp');
        $this->crud = $this->azapp->add_crud();
        $this->role_name = $this->session->userdata('role_name');
        $this->is_admin = ($this->role_name == 'administrator');
    }

    public function index() {
        $azapp = $this->azapp;
        $crud = $this->crud;

        $crud->set_column(array(azlang('No'), 'Nama Item', 'Tipe', azlang('Action')));
        $crud->set_id($this->table);
        $crud->set_default_url(true);

        $v_modal = $this->load->view('production_cash_item/v_production_cash_item', '', true);
        $crud->set_form('form');
        $crud->set_modal($v_modal);
        $crud->set_modal_title('Item Kas Produksi');
        $v_modal = $crud->generate_modal();
        $crud = $crud->render();
        $crud .= $v_modal;
        $azapp->add_content($crud);

        $data_header['title'] = 'Item Kas Produksi';
        $data_header['breadcrumb'] = array('production_cash', 'production_cash_item');
        $azapp->set_data_header($data_header);

        echo $azapp->render();
    }

    public function get() {
        $crud = $this->crud;
        $crud->set_select('idproduction_cash_item, item_name, item_type');
        $crud->set_filter('item_name, item_type');
        $crud->set_sorting('item_name, item_type');
        $crud->set_id($this->table);
        $crud->add_where("status > 0");
        
        // Produksi hanya lihat miliknya
        if (!$this->is_admin) {
            $crud->add_where('iduser = ' . $this->session->userdata('iduser'));
        }
        
        $crud->set_table($this->table);
        $crud->set_custom_style('custom_style');
        echo $crud->get_table();
    }

    function custom_style($key, $value, $data) {
        if ($key == 'item_type') {
            $lbl = ($value == 'income') ? 'success' : 'danger';
            $txt = ($value == 'income') ? 'Pemasukan' : 'Pengeluaran';
            return "<label class='label label-" . $lbl . "'>" . $txt . "</label>";
        }
        return $value;
    }

    public function save() {
        $data = array();
        $data_post = $this->input->post();
        $idpost = azarr($data_post, 'id' . $this->table);
        $data["sMessage"] = "";

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('item_name', 'Nama Item', 'required|trim|max_length[200]');
        $this->form_validation->set_rules('item_type', 'Tipe', 'required|trim');

        $err_code = 0;
        $err_message = "";

        if ($this->form_validation->run() == TRUE) {
            $iduser = $this->session->userdata('iduser');
            if ($this->is_admin && strlen($this->input->post('iduser')) > 0) {
                $iduser = $this->input->post('iduser');
            }

            $data_save = array(
                "iduser" => $iduser,
                "item_name" => azarr($data_post, 'item_name'),
                "item_type" => azarr($data_post, 'item_type'),
            );

            $response_save = az_crud_save($idpost, $this->table, $data_save);
            $err_code = azarr($response_save, 'err_code');
            $err_message = azarr($response_save, 'err_message');
        }

        $data["sMessage"] = validation_errors() . $err_message;
        echo json_encode($data);
    }

    public function edit() {
        az_crud_edit('idproduction_cash_item, item_name, item_type');
    }

    public function delete() {
        $id = $this->input->post("id");
        az_crud_delete('production_cash_item', $id);
    }
}