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

class Product_category extends CI_Controller {
	protected $table;

	public function __construct() {
        parent::__construct();
        $this->table = "product_category";
        $this->load->helper("az_core");
        az_check_login();
    }

	public function index(){
		$this->load->library('AZApp');
		$azapp = $this->azapp;

		$this->load->library('AZAppCRUD');
		$crud = $this->azappcrud;

		$column = array('#', azlang('Category'), azlang('Action'));
		$crud->set_column($column);
		$crud->set_width("10px, , 120px");
		$crud->set_th_class("no-sort, , no-sort");
		$crud->set_id($this->table);
		$crud->set_default_url(true);
		$crud->set_form('form');
		$v_modal = $this->load->view($this->table.'/v_'.$this->table, '', true);
		$crud->set_modal($v_modal);
		$crud->set_modal_title(azlang('Product Category'));

		$v_view = $crud->render();
		$v_modal = $crud->generate_modal();
		$v_view .= $v_modal;
		$azapp->add_content($v_view);

		$data_header['title'] = azlang('PRODUCT');
		$data_header['subtitle'] = azlang('PRODUCT CATEGORY');
		$azapp->set_data_header($data_header);

		echo $azapp->render();
	}

	public function get() {
		$this->load->library("AZAppCRUD");
		$crud = $this->azappcrud;

    	$crud->set_select("id".$this->table.", name");
    	$crud->set_filter('name');
    	$crud->set_table($this->table);
    	$crud->set_sorting('name');
    	$crud->set_order_by('name');
    	$crud->set_id($this->table);

		echo $crud->get_table();
	}

	public function edit() {
		$id = $this->input->post("id");
		$this->db->select("id".$this->table.", name");
		$this->db->where("id".$this->table, $id);

		$rdata = $this->db->get($this->table)->result_array();
		echo json_encode($rdata);
	}

	public function save(){
		$data = array();
		$data["sMessage"] = "";
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('', '');

		$this->form_validation->set_rules('name', azlang('Category'), 'required|trim|max_length[20]');

		$data_post = $this->input->post();
		$err_code = "";
		$err_message = "";

		if($this->form_validation->run() == TRUE){
			$idpost = $data_post['id'.$this->table];

			$data_save = array(
				"name" => $data_post["name"],
				"updated" => Date("Y-m-d H:i:s"),
				"updatedby" => $this->session->userdata("username")
			);

			if($idpost == ""){
				$data_save["created"] = Date("Y-m-d H:i:s");
				$data_save["createdby"] = $this->session->userdata("username");
				if(!$this->db->insert($this->table, $data_save)){
					$err = $this->db->error();
					$err_code = $err["code"];
					$err_message = $err["message"];
				}
			}
			else {
				$this->db->where("id".$this->table, $idpost);
				if (!$this->db->update($this->table, $data_save)) {
					$err = $this->db->error();
					$err_code = $err["code"];
					$err_message = $err["message"];
				}
			}
		}

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

	public function delete() {
		$id = $this->input->post("id");

		if (is_array($id)) {
			$this->db->where_in("id".$this->table, $id);
		}
		else {
			$this->db->where("id".$this->table, $id);
		}

		$this->db->delete($this->table);

		echo json_encode(array("SUCCESS"));
	}

	public function get_data(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("name");
		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->select("idproduct_category as id, name as text");

		$data = $this->db->get("product_category", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$cdata = $this->db->get("product_category");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}
}