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

class mstr_pro_type extends CI_Controller {

    var $main_menu_name = "settings";
    var $sub_menu_name = "mstr_pro_type";

    public function __construct()
    {
        parent::__construct();

        $this->load->model('mstr_pro_type_model');
        $this->load->model('Common_Model');
        $this->load->model('Sequerty_Model');
    }

    public function index()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('mstr_pro_type',$data);
    }


    public function save_department()
    {
        
       // echo "test"; die();
        $department_id=$this->input->post('department_id');
        $type=$this->input->post('type');
        $department_name=$this->input->post('department_name');
        //$department_code=$this->input->post('department_code');

        $data=array(
            'ptype_name'=>$department_name,
           // 'department_code'=>$department_code,
        );

        $this->load->library('form_validation'); //form validation lib
        if($type=='A')
        {
            $this->form_validation->set_rules('ptype_name', 'Name', 'required[mstr_department.ptype_name]');
        }
        else if($type=='E')
        {
            $this->form_validation->set_rules('ptype_name', 'Name', 'required');
        }

       // if ($this->form_validation->run() == FALSE)
      //  {
        //    $st = array('status' =>0,'validation' => validation_errors());
         //   echo json_encode($st);
      //  }
        //else
        {

            $_insert=$this->mstr_pro_type_model->save_department($data,$department_id);
            $lastid=$this->db->insert_id();

            if($type=='A'){
                if ($lastid) {
                    echo json_encode(array('id'=>$lastid,'type'=>$type));
                } else {
                    echo json_encode(array('status'=>'error'));
                }
            }
            if($type=='E'){
                echo json_encode(array('type'=>$type));
            }
        }
    }

    public function add_department()
    {
        if (isset($_GET['department_id'])) {
            $department_id=$_GET['department_id'];
        }
        else {
            $department_id='';
        }
        if($department_id){
            $data['department_id']=$department_id;
            $data['type']='E';
            $data['pageName']='UPDATE ';
            $data['btnText']='Update ';
            $data['suppliyer']= $this->mstr_pro_type_model->get_department_info($department_id);
        }
        else {
            $data['department_id']='';
            $data['type']='A';
            $data['pageName']='ADD ';
            $data['btnText']='Add ';
            $data['suppliyer']=array();
        }
        $this->load->view('models/create_mstr_pro_type',$data);
    }

    public function list_department()
    {
        $requestData= $_REQUEST;

        $columns = array(
            0 =>'department_code',
            1 => 'department_name'
        );

        $data = array();
        $department = $this->mstr_pro_type_model->get_all_department();
        $totalData = count($department);
        $totalFiltered = $totalData;

        foreach ($department as $row){
            $nestedData=array();
            $nestedData[] =$row['ptype_id'];
            $nestedData[] = $row['ptype_name'];
            $actionTxtDisble='';
            $actionTxtEnable='';
            $actionTxtUpdate='';
            $actionTxtDelete='';
            $actionTxtUpdate='<a onClick="click_department_update_btn('.$row['ptype_id'].')" data-toggle="modal" href="#" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="glyphicon fa fa-edit"></i></a> &nbsp;';
            if($row['ptype_status']==1){
                $actionTxtDisble = '<a class="btn btn-xs btn-green tooltips" data-placement="top" data-original-title="Disable supplier" onClick="disableDepartmentData('.$row['ptype_id'].')"><i class="glyphicon fa fa-check"></i></a> &nbsp;';
            }
            if($row['ptype_status']==0){
                $actionTxtEnable = '<a class="btn btn-xs btn-warning tooltips" data-placement="top" data-original-title="Disable supplier" onClick="enableDepartmentData('.$row['ptype_id'].')"><i class="glyphicon fa fa-minus-circle"></i></a> &nbsp;';
            }
            //$actionTxtDelete='<a class="btn btn-xs btn-bricky tooltips" data-placement="top" data-original-title="Delete supplier" onClick="deleteDepartmentData('.$row['ptype_id'].')">
										//					<i class="glyphicon fa fa-trash-o"></i></a>';

            $nestedData[]=$actionTxtUpdate.$actionTxtDisble.$actionTxtEnable.$actionTxtDelete;
            $data[] = $nestedData;
        }

        $json_data = array(
            //"draw"            => intval( $requestData['draw'] ),
            "recordsTotal"    => intval( $totalData ),
            "recordsFiltered" => intval( $totalFiltered ),
            "data"            => $data
        );

        echo json_encode($json_data);
    }


    function delete_department() {
        $department_id	= $this->input->post('department_id');
        $this->mstr_pro_type_model->delete_department($department_id);
        if ($department_id) {
            echo json_encode(array('id'=>$department_id));
        } else {
            echo json_encode(array('status'=>'error'));
        }
    }

    function disable_department() {

        $department_id	= $this->input->post('department_id');
        $this->mstr_pro_type_model->disable_department($department_id);
        if ($department_id) {
            echo json_encode(array('id'=>$department_id));
        } else {
            echo json_encode(array('status'=>'error'));
        }
    }

    function enable_department() {
        $department_id	= $this->input->post('department_id');
        $this->mstr_pro_type_model->enable_department($department_id);
        if ($department_id) {
            echo json_encode(array('id'=>$department_id));
        } else {
            echo json_encode(array('status'=>'error'));
        }
    }
}