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

class Rassist_booking extends CI_Controller {

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

         
    }



	public function index(){
       
		$template['page'] = 'Road_assist/booking_list';
        $template['menu'] = 'Roadassist Requet Management';
        $template['smenu'] = 'View Roadassist bookings';
        $template['pTitle'] = "View Roadassist bookings";
        $template['pDescription'] = "View and Manage Roadassist bookings";
        $template['page_head'] = "Roadassist booking Management";
        $template['datatable'] = 1;
		$this->load->view('template',$template);
	}

public function get_all_booking() {
	$data = $_GET;

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

	 $columns = array("customer.name","vehicle_model.vehicle_name","vehicle_model.make","customer.email_id");
	
	
	$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->Rassist_booking_model->get_all_Assist_booking($value);

    $all_list = $this->Rassist_booking_model->get_all_Assist_booking();

	$recordsTotal = count($all_list);

    $filtered = count($all_list);

    if($value['where']!='') {
        $value['length'] = '-1';
        $all_list = $this->Rassist_booking_model->get_all_Assist_booking($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>";
        $customer_dtl = "Name:".$r->customer_name."<br>
                Phone:".$r->customer_phone."<br>
                Email:".$r->email_id."<br>";
 if($r->status != 1){                
        $assist_dtl = "Name:".$r->road_assist_name."<br>
                Phone:".$r->road_assist_phone."<br>
                Email:".$r->road_assist_email."<br>";          
                    } else{

         $assist_dtl ='Waiting....';

                    }
$action = " <a class='btn btn-sm btn-info' onclick='viewBookingDetails(".$r->id.")'>
                       <i class='fa fa-cog'></i> VIEW
                                </a>";
                         
                       
                                        
if($r->status == 1){ 
$status =   "<button type='button' class='btn btn-primary'>Requested</button>";
                         } else if ($r->status == 2) {
$status =  "<button type='button' class='btn btn-info'>Accept</button>";
                         } else if ($r->status == 3) {
   $status =  "<button type='button' class='btn btn-warning'>Completed</button>";
                         }else if ($r->status == 0) {
$status =  "<button type='button' class='btn btn-danger'>Cancelled</button>";
                         }
                     


                array_push($data, array(
                    $r->cust_id,
                    $r->booking_id,
                    $customer_dtl,
                    $vehicle_dtl,
                    $assist_dtl,
                    $r->location,
                    $r->date,
                    $status,
                    $action));
                    }


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



















	public function getAssist_bookingData(){
		$resArr = array('status'=>0);
		if(!isset($_POST)||empty($_POST)||!isset($_POST['booking_id'])||empty($_POST['booking_id']) || 
		   !is_numeric($booking_id = $_POST['booking_id'])){
			echo json_encode($resArr);exit;
		}
		 //$view_all = (isset($_POST['view_all']) && !empty($_POST['view_all']))?$_POST['view_all']:'';
        $booking_id = $_POST['booking_id'];
       
		$bokData = $this->Rassist_booking_model->getAssist_bookingData($booking_id);
//print_r($bokData);exit;
		if(empty($bokData)){
			echo json_encode($resArr);exit;
		}

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

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

	
	

	


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

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




	
}
?>