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

class Garage_response extends CI_Controller {

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

         
    }



	public function responce($request_id){
       
		$template['page'] = 'Garage_response/list';
        $template['menu'] = 'Garage Response Management';
        $template['smenu'] = 'View Garage Responses';
        $template['pTitle'] = "View Garage Responses";
        $template['pDescription'] = "View and Manage Garage Responses";
        $template['page_head'] = "Garage Response Management";
        $template['datatable'] = 1;
        $template['request_id'] = $request_id;
        $template['response'] = $this->Garage_response_model->getGarage_responseData(decode_param($request_id));
        $template['prod_list'] = $this->Garage_response_model->get_all_Garage_response(decode_param($request_id));
//print_r($template['prod_list']);exit;
		$this->load->view('template',$template);
	}

	public function getGarage_responseData(){
		$resArr = array('status'=>0);
		if(!isset($_POST)||empty($_POST)||!isset($_POST['Garage_response_id'])||empty($_POST['Garage_response_id']) || 
		   !is_numeric($Garage_response_id = $_POST['Garage_response_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_response_model->getGarage_responseData($Garage_response_id,$view_all);
      //echo json_encode($mechData);exit;
		if(empty($mechData)){
			echo json_encode($resArr);exit;
		}

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

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

	
	

	


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

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


public function get_all_Garage_response($request) {
	$data = $_GET;
    $request_id = decode_param($request);
//print_r($data);exit;
	$col = $data['order'][0]['column'];
	$col_type = $data['order'][0]['dir'];

	 $columns = array("garage.garage_code","garage.name","garage.phone_no","customer.name","vehicle_model.vehicle_name,garage_response.quote_type,garage_response.labour_cost,garage_response.parts_cost");
	
	
	$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_response_model->get_all_Garage_response($request_id,$value);

	//print_r($prod_list);exit;
   
    $all_list = $this->Garage_response_model->get_all_Garage_response($request_id);

	$recordsTotal = count($all_list);

    $filtered = count($all_list);
    if($value['where']!='') {
        $value['length'] = '-1';
        $all_list = $this->Garage_response_model->get_all_Garage_response($request_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>";
if($r->quote_type ='1') { $quote_types = "BASE";}  
if($r->quote_type ='2') { $quote_types = "MEDIUM";}   
if($r->quote_type ='3') { $quote_types = "PREMIUM";}
// $action = " <a class='btn btn-sm btn-info' onclick='viewResponseDetails(".$request_id.",".'1'.")'>
//                        <i class='fa fa-cog'></i> VIEW
//                                 </a>";
// $action .= "  <a class='btn btn-info btn-sm' href='".base_url()."Garage_response/editGarage_responses/".encode_param($r->id)."'>
//                                              <i class='fa fa-pencil'></i>Edit
//                                          </a>";
                                  
                                      
                                               
// $action .=   "  <a class='btn btn-sm btn-danger' href='".base_url()."Garage_response/changeStatus/".encode_param($r->id).'/2' ."'
//                             onClick='return doconfirm()'>
//                           <i class='fa fa-fw fa-trash'></i>Delete
//                         </a>";   
                         
                       
                                        
if($r->status == 1){ 
$action =   " <a class='btn btn-sm btn-success' style='background-color:#ac2925' href='".base_url()."Garage_response/changeStatus/".encode_param($r->id).'/0' ."'>
                                             <i class='fa fa-cog'></i> Approved
                                           </a>";
                                         } else { 
$action =  " <a class='btn btn-sm btn-success' href='".base_url()."Garage_response/changeStatus/".encode_param($r->id).'/1' ."'>
                       <i class='fa fa-cog'></i> Pending
                                </a>";
                        } 
                        


                array_push($data, array(
                	$r->id,
                	// $r->request_code,
                    $r->garage_code,
                    $r->garage_name.'</br>'.'<b>'.'Phone:'.'</b>'. $r->garage_phone1,
                   
                    // $r->customer_name.'</br>'.'<b>'.'Phone:'.'</b>'. $r->phone_no,
                    // $vehicle_dtl,
                    // $r->sub_services,
                    $quote_types,
                    $r->labour_cost,
                    $r->parts_cost,
                   
                    $action
                     ));
            }


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


public function getGarage_respData(){
   $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;
   }
  
   $mechData = $this->Garage_response_model->Garage_ResponseData($request_id);
    
   if(empty($mechData)){
     echo json_encode($resArr);exit;
   }

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



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

    }
    
    
    $mechData = $this->Garage_response_model->approve_quote($_POST);
   
    if(empty($mechData)){
      $resArr = array('status'=>0);
      echo json_encode($resArr);exit;
    }

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










	
}
?>