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

class Garage_request extends CI_Controller {

 public function __construct() {
        parent::__construct();
        date_default_timezone_set("Asia/Kolkata");
        $this->load->model('Garage_request_model');
         $this->load->helper('generals_helper');
        if(!$this->session->userdata('logged_garage')) {
            redirect(base_url());
        }


         
    }



	public function index(){


    $template['page'] = 'Garage_request/list_garage';
        $template['menu'] = 'Garage Request Management';
        $template['smenu'] = 'View Garage Requests';
        $template['pTitle'] = "View Garage Requests";
        $template['pDescription'] = "View and Manage Garage Requests";
        $template['page_head'] = "Garage Request Management";
        $template['datatable'] = 1;


        //$template['Garage_request_data'] = $this->Garage_request_model->getGarage_requestData();

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

	function changeStatus($request_id = ''){
	
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'danger');
		if(empty($request_id) || !is_numeric($request_id = decode_param($request_id))){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Garage_request'));
		}
		$status = $this->Garage_request_model->changeStatus($request_id);
		if(!$status){
			$this->session->set_flashdata('message',$flashMsg);
		}
		redirect(base_url('Garage_request'));
	}

	
	

	


// public function check_email_availability() {
   			
// 		$email=$this->input->post('email');

//         $result = $this->Garage_request_model->email_availability_check($email);
// 		echo json_encode($result);
		
		
// 	}


public function get_all_Garage_request() {
	$data = $_GET;
$garage_id = $this->session->userdata('garage_id');
       

	$col = $data['order'][0]['column'];
	$col_type = $data['order'][0]['dir'];

	 $columns = array("garage_request.id","sub_Garages.sub_Garages","Garages.Garage_name","garage_request.booking_date");
	
	
	$value['order_by'] = $columns[$col];
	$value['order_type'] = $col_type;
	$value['search'] = $data['search']['value'];
	$value['start'] = $data['start'];
	$value['length'] = $data['length'];
	$value['where'] = '';

	if(!empty($value['search'])) {
            $where = array();
            foreach($columns as $c) {
                $where_data[] = $c." like '%".$value['search']."%' ";
            }
            $where = implode(" OR ", $where_data);
            $where = "(".$where.")";
            $value['where'] = $where;
        }


	$prod_list = $this->Garage_request_model->get_all_Garage_request($garage_id,$value);

	//print_r($prod_list);exit;
   
    $all_list = $this->Garage_request_model->get_all_Garage_request($garage_id);

	$recordsTotal = count($all_list);

    $filtered = count($all_list);
    if($value['where']!='') {
        $value['length'] = '-1';
        $all_list = $this->Garage_request_model->get_all_Garage_request($garage_id,$value);
        $filtered = count($all_list);
    }

	$data = array();
	foreach ($prod_list  as $r) {
$vehicle_dtl = "Model:".$r->model."<br>
                Make:".$r->make."<br>
                Year:".$r->year."<br>";  

// $action = " <a class='btn btn-sm btn-info' onclick='viewRequestDetails(".$r->cust_id.",".$r->cust_id.")'>
//                        <i class='fa fa-cog'></i> VIEW
//                                 </a>";

// $action .= "  <a class='btn btn-info btn-sm' href='".base_url()."Garage_request/editGarage_requests/".encode_param($r->cust_id)."'>
//                                              <i class='fa fa-pencil'></i>Edit
//                                          </a>";
                                  
                                      
                                               
// $action =   "  <a class='btn btn-sm btn-danger' href='".base_url()."Garage_request/changeStatus/".encode_param($r->id)."'
//                             onClick='return doconfirm()'>
//                           <i class='fa fa-fw fa-trash'></i>Delete
//                         </a>";   
                         
   $action ='';                    
   $status = '';                                 
// if($r->status == 2){ 
 if ($r->status == 2) {
                                          
$status =  "<button type='button' class='btn btn-info'>Requested</button>";
                        } else if ($r->status == 3) { 
   $status =  "<button type='button' class='btn btn-warning'>Respond</button>";
                         } else if ($r->status == 4) {

$status =  "<button type='button' class='btn'>Quote Send</button>";
                        } else if ($r->status == 5) {
$status =  "<button type='button' class='btn btn-success'>Approved</button>";

                   }else if ($r->status == 0) {
$status =  "<button type='button' class='btn btn-danger'>Cancelled</button>";

                   }






 if ($r->status != 5)  { 

  $action =  " <a class='btn btn-sm btn-success' onclick='getRequestDetails(".$r->id.",".$garage_id.")'>
                       <i class='fa fa-cog'></i> Quote
                                </a>";
}
     if ($r->status == 3)  { 
 $action .=  " <a class='btn btn-sm btn-info' onclick='viewRequestDetails(".$r->id.",".$garage_id.")'>
                       <i class='fa fa-cog'></i> VIEW
                                </a>";
           } 
if ($r->status == 5) {

$action .= "<a class='btn btn-sm btn-danger' href='".base_url()."Garage_request/changeStatus/".encode_param($r->id)."' onClick='return doconfirm()'><i class='fa fa-fw fa-remove'></i>Reject
</a>";   

} 


                        
$assign_dtl = "<a class='btn btn-info btn-sm' href='".base_url()."Garage_request/Garage_assign/".encode_param($r->id)."'>
                                             <i class='fa fa-pencil'></i>Assign
                                         </a>";

                array_push($data, array(

                    $r->cust_id,
                    $r->request_code,
                    $r->customer_name.'<br>'.'<b>'.'Phone:'.'</b>'.($r->phone_no),
                    $vehicle_dtl,
                    $r->service_name,
                    $r->booking_date,
                    $status,
                    $action
                     ));
            }


	echo json_encode(array('recordsFiltered' => $filtered, 'recordsTotal' => $recordsTotal, 'data' => $data));
  
}

// public function Garage_assign($request_id=''){
       
// 		    $template['page'] = 'Garage_request/assign';
//         $template['menu'] = 'Garage Assign Management';
//         $template['smenu'] = 'View Garage Assign';
//         $template['pTitle'] = "View Garage Assign";
//         $template['pDescription'] = "View and Manage Garage Assign";
//         $template['page_head'] = "Garage Assign Management";
//         $template['datatable'] = 1;
//         $template['request_id'] = $request_id;
//         $template['requestData'] = $this->Garage_request_model->getGarage_requestData(decode_param($request_id));
//         $template['garageData'] = $this->Garage_request_model->getGarageData();
//         $template['assign_garage'] = $this->Garage_request_model->getAssignGarage();
//  // echo json_encode($template['assign_garage']);exit;
// 		$this->load->view('template',$template);
// 	}

	

public function approve_garage(){

    if(!isset($_POST)||empty($_POST)){
      $resArr = array('status'=>0);
      echo json_encode($resArr);exit;

    }
   
   $setting = getSettings();
   
    
    $mechData = $this->Garage_request_model->approve_garages($_POST,$setting['VAT']);
   
    if(empty($mechData)){
      $resArr = array('status'=>0);
      echo json_encode($resArr);exit;
    }

    $resArr['status'] = 1;
    $resArr['data'] = $mechData;
    echo json_encode($resArr);exit;
  }

public function getQuoteData(){
   $resArr = array('status'=>0);
   if(!isset($_POST)||empty($_POST)||!isset($_POST['request_id'])||empty($_POST['request_id']) || 
      !is_numeric($request_id = $_POST['request_id'])){
     echo json_encode($resArr);exit;
   }
    //$view_all = (isset($_POST['view_all']) && !empty($_POST['view_all']))?$_POST['view_all']:'';
        //$view_all = (isset($_POST['view_all']) && $_POST['view_all'] == 1)?1:0;
   $mechData = $this->Garage_request_model->getGarage_QuoteData($request_id);
      //echo json_encode($mechData);exit;
   if(empty($mechData)){
     echo json_encode($resArr);exit;
   }

   $resArr['status'] = 1;
   $resArr['data'] = $mechData;
   echo json_encode($resArr);exit;
  }


}
?>