<?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 DeliveryCompany 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,$isActive=9, $currentPage = 0, $name = '')
	{
	    $offsetRecords = 0;
	    if($currentPage > 0)
	        $offsetRecords = 10*($currentPage-1);
	    
	    $sql="SELECT * FROM DeliveryCompany WHERE (DeliId = ".$id." OR 0=".$id.") AND (IsActive = ".$isActive." OR ".$isActive."=9) AND (DeliName LIKE '%".rawurldecode($name)."%' OR ''='".rawurldecode($name)."') AND IsDeleted=0 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('DeliveryCompany',$input);
            
            $del = array(
                    'DeliId' => $input['DeliId'],
                    'DeliName' => $input['DeliName'],
                    'DeliPhone' => $input['DeliPhone'],
                    'DeliAddress' => $input['DeliAddress'],
                    'IsActive' => intval($input['IsActive']),
                    'CreatedDateTime' => date("Y-m-d H:i:s"),
                    'CreatedBy' =>  intval($input['CreatedBy']),
                    'IsDeleted' => 0,
                    'DeliRemark' => $input['DeliRemark']
            );
            $this->db->insert('DeliveryCompany',$del);
         
            $this->response(['A delivery company has been created successfully.'], REST_Controller::HTTP_OK);
        }
        else if ($type==='update'){
            $input = $this->input->post();
             $del = array(
                    'DeliId' => $input['DeliId'],
                    'DeliName' => $input['DeliName'],
                    'DeliPhone' => $input['DeliPhone'],
                    'DeliAddress' => $input['DeliAddress'],
                    'IsActive' => intval($input['IsActive']),
                    'CreatedDateTime' => date("Y-m-d H:i:s"),
                    'CreatedBy' =>  intval($input['CreatedBy']),
                    'IsDeleted' => 0,
                    'DeliRemark' => $input['DeliRemark']
            );
            $this->db->update('DeliveryCompany', $del, array('DeliId'=>$input['DeliId']));
         
            $this->response(['The delivery company has been updated successfully.'], REST_Controller::HTTP_OK);
        }else if ($type==='delete') {
            $input = $this->input->post();
            $del = array(
                    'IsDeleted' => 1,
                    'CreatedDateTime' => date("Y-m-d H:i:s")
            );
            $this->db->update('DelveryCompany', $del, array('DeliId'=>$input['DeliId']));
         
            $this->response(['The delivery company has been deleted 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);
    }
    	
}