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

class Product extends MY_Controller
{
    
     public $data = array(
        'breadcrumb'    => 'Produk',
        'modul'         => 'Produk',
        'pesan'         => '',
        'pagination'    => '',
        'datatable'    => '',
        'main_view'     => 'product/product', ///This View Form or Table is want to the load
        'form_action'   => '',
        'form_value'    => '',
        );
	
    public function __construct()
    {	
        parent::__construct();		
        $this->load->model('Product_model', 'product', TRUE);
        $this->load->library('session');
        $this->load->helper('thumb');
    }
	
    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 InsertProduct()
    {
        $date = date('Y-m-d G:i:s');
        $lokasi_file    = $_FILES['fupload']['tmp_name'];
        $tipe_file      = $_FILES['fupload']['type'];
        $nama_file      = $_FILES['fupload']['name'];
        $nama_file_unik = $nama_file; 

        if (empty($lokasi_file)==false) 
        {
           UploadImage($nama_file_unik);
           $data = array(
                            'product_code' => $this->input->post('txtAddProductCode'), 
                            'product_name' => $this->input->post('txtAddProductName'), 
                            'description'  => $this->input->post('txtAddProductDeskripsi'),
                            'create_date'  => $date,
                            'isactive'     => 'Y',
                            'file_name'    => $nama_file,
                            'create_by'    => $this->session->userdata('username')
                    );
     
            $simpan = $this->product->InsertProduct($data);
            echo $simpan;
        }else {
             $data = array(
                            'product_code' => $this->input->post('txtAddProductCode'),
                            'product_name' => $this->input->post('txtAddProductName'), 
                            'description'  => $this->input->post('txtAddProductDeskripsi'),
                            'create_date'  => $date,
                            'isactive'     => 'Y',
                            'create_by'    => $this->session->userdata('username')
                    );
     
            $simpan = $this->product->InsertProduct($data);
            echo $simpan;
        }
    }

    public function UpdateProduct()
    {
        $date = date('Y-m-d G:i:s');
        $id = $this->input->post('txtIdProduct');
        
        $lokasi_file    = $_FILES['fupload']['tmp_name'];
        $tipe_file      = $_FILES['fupload']['type'];
        $nama_file      = $_FILES['fupload']['name'];
        $nama_file_unik = $nama_file; 
        
        if (empty($lokasi_file)==false) 
        {
            UploadImage($nama_file_unik);
            $data = array(
                            'product_name' => $this->input->post('txtEditProductName'), 
                            'description'  => $this->input->post('txtEditProductDeskripsi'),
                            'update_date'  => $date,
                            'isactive' => $this->input->post('rbactive'), 
                            'file_name'    => $nama_file,
                            'update_by'    => $this->session->userdata('username')
                        );
         
            $simpan = $this->product->UpdateProduct($id,$data);
            echo $simpan;
        }else {
             $data = array(
                        'product_name' => $this->input->post('txtEditProductName'), 
                        'description'  => $this->input->post('txtEditProductDeskripsi'),
                        'update_date'  => $date,
                        'isactive' => $this->input->post('rbactive'), 
                        'update_by'    => $this->session->userdata('username')
                    );
     
            $simpan = $this->product->UpdateProduct($id,$data);
             echo $simpan;
        }   
    }

    public function GetDataProductById()
    {
        $id = $_POST['id'];
        $data = $this->product->GetProductById($id);   
        echo json_encode($data); 
    }

    public function GetDataProduct()
    {
       $columns = array( 
                            0 =>'id', 
                            1 =>'product_code',
                            2 =>'product_name',
                            3 =>'isactive',
                            4 => '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->product->AllProductCount();
        $totalFiltered = $totalData; 
            
        if(empty($this->input->post('search')['value']))
        {            
            $posts = $this->product->AllProduct($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value']; 

            $posts =  $this->product->ProductSearch($limit,$start,$search,$order,$dir);

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

                $nestedData['id'] = $post->id;
                $nestedData['product_code'] = $post->product_code;
                $nestedData['product_name'] = $post->product_name;
                $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 checkAvailability()
    {
        $code = $_POST['code'];
        $data = $this->product->checkAvailability($code);

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

}

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