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

class Customer extends MY_Controller
{
    
     public $data = array(
        'breadcrumb'    => 'Customer',
        'modul'         => 'Customer',
        'pesan'         => '',
        'pagination'    => '',
        'datatable'    => '',
        'main_view'     => 'customer/customer', ///This View Form or Table is want to the load
        'form_action'   => '',
        'form_value'    => '',
        );
	
    public function __construct()
    {	
        parent::__construct();		
        $this->load->model('Customer_model', 'customer', TRUE);
        $this->load->model('Login_model', 'userSession', TRUE);
        $this->load->library('session');
        $this->load->helper('encryption');
    }
	
    public function index()
    {

        $session_data = $this->session->userdata('full_name');
        $session_role = $this->session->userdata('role_id');

        $this->data['full_name'] = $session_data;   
        $this->data['role_id'] = $session_role;     

        $this->load->view('template', $this->data);
    }

    public function getDataCustomerById()
    {
        $id = $_POST['id'];
        $data = $this->customer->GetCustomerById($id);
           
        echo json_encode($data); 
    }

    public function getDataCustomer()
    {
       $columns = array( 
                            0 =>'id', 
                            1 => 'customer_code',
                            2 =>'customer_name',
                            3=> 'email',
                            4=> 'phone',
                            5=> 'isactive',
                            6=> 'id',
                        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir = $this->input->post('order')[0]['dir'];

        $totalData = $this->customer->allcustomer_count();
        $totalFiltered = $totalData; 
            
        if(empty($this->input->post('search')['value']))
        {            
            $posts = $this->customer->allcustomer($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value']; 

            $posts =  $this->customer->customer_search($limit,$start,$search,$order,$dir);

            $totalFiltered = $this->customer->customer_search_count($search);
        }
        
        $data = array();
        if(!empty($posts))
        {
            foreach ($posts as $post)
            {

                $nestedData['id'] = $post->id;
                $nestedData['customer_code'] = $post->customer_code;
                $nestedData['customer_name'] = $post->customer_name;
                $nestedData['email'] = $post->email;
                $nestedData['phone'] = $post->phone;
                $nestedData['isactive'] = $post->isactive;
                $data[] = $nestedData;

            }
        }

      
        $json_data = array(
                "draw"            => intval($this->input->post('draw')),  
                "recordsTotal"    => intval($totalData),  
                "recordsFiltered" => intval($totalFiltered), 
                "data"            => $data   
                );

        echo json_encode($json_data); 
       
    }

    public function InsertCustomer()
    {
        $date = date('Y-m-d G:i:s');
        //$birthDate = date('Y-m-d',strtotime($this->input->post('txtAddCustomerBirthDate')));
        //$password = random_password(8);
        $data = array(
                            'customer_code' => $this->input->post('txtAddCustomerCode'), 
                            'customer_name' => $this->input->post('txtAddCustomerName'), 
                            'email'         => $this->input->post('txtAddCustomerMail'), 
                            'phone'         => $this->input->post('txtAddCustomerPhone'), 
                            'create_date'      => $date,
                            'address'       => $this->input->post('txtAddCustomerAddrees'), 
                            'isactive'      => 'Y',
                            'create_by'     => $this->session->userdata('username')
                    );

        $simpan = $this->customer->InsertCustomer($data);
      echo 'ok';
    }

    public function UpdateCustomer()
    {
        $date = date('Y-m-d G:i:s');
        $id = $this->input->post('txtIdCustomer');
        $data = array(
                        'customer_name' => $this->input->post('txtEditCustomerName'), 
                        'email'         => $this->input->post('txtEditCustomerMail'), 
                        'phone'         => $this->input->post('txtEditCustomerPhone'), 
                        'address'       => $this->input->post('txtEditCustomerAddrees'), 
                        'isactive' => $this->input->post('rbactive'), 
                        'update_date'   => $date,
                        'update_by'     => $this->session->userdata('username')
                    );

      $simpan = $this->customer->UpdateCustomer($id,$data);
      echo $simpan;
    }

    public function checkAvailability()
    {
        $code = $_POST['code'];
        $data = $this->customer->checkAvailability($code);

        if($data > 0)
        { 
            echo "<span class='status-not-available'> Kode Customer ".$code." Sudah Ada Pada Database, Gunakan Kode Customer Yang Lain.</span>";
        } else{
            echo "";
        }
    }
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */