<?php
   header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
header("Content-Security-Policy: default-src 'self';");
   require APPPATH . '/libraries/REST_Controller.php';
   use Restserver\Libraries\REST_Controller;
     
class Customer extends REST_Controller {
    
	  /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function __construct() {
       parent::__construct();
       $this->load->database();
    }
       
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
	public function index_get($id = 0,$currentPage = 0, $name = '')
	{
	    $offsetRecords = 0;
	    if($currentPage > 0)
	        $offsetRecords = 10*($currentPage-1);
	        
	    $sql="SELECT * FROM Customer WHERE (CustomerId = ".$id." OR 0=".$id.") AND (CustomerName LIKE '%".rawurldecode($name)."%' OR ''='".rawurldecode($name)."') ORDER BY CustomerId DESC LIMIT ".$offsetRecords.", 10 ";
        
        $data = $this->db->query($sql)->result();
     
        $this->response($data, REST_Controller::HTTP_OK);
	}
      
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_post($type)
    {
        if($type==='new'){
            $input = $this->input->post();
            $this->db->insert('Customer',$input);
         
            $this->response(['Customer created successfully.'], REST_Controller::HTTP_OK);
        }
        else if ($type==='update'){
            $input = $this->input->post();
            $this->db->update('Customer', $input, array('CustomerId'=>$input['CustomerId']));
         
            $this->response(['Customer updated successfully.'], REST_Controller::HTTP_OK);
        }else if ($type==='delete') {
            $input = $this->input->post();
            $this->db->delete('Customer',  array('CustomerId'=>$input['CustomerId']));
         
            $this->response(['Customer delete successfully.'], REST_Controller::HTTP_OK);
        }
    } 
     
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_put($id)
    {
        $input = $this->put();
        $this->db->update('Customer', $input, array('CustomerId'=>$id));
     
        $this->response(['Customer updated successfully.'], REST_Controller::HTTP_OK);
    }
     
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_delete($id)
    {
        //$this->db->delete('Customer', array('CustomerId'=>$id));
       
        //$this->response(['Customer deleted successfully.'], REST_Controller::HTTP_OK);
         $this->response([$id], REST_Controller::HTTP_OK);
    }
    	
}