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

class Ride extends CI_Controller {

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

	function import_ride(){
		$template['page'] = 'Ride/add_vehicle';

		$template['menu'] = "Ride Management";
		$template['sub_menu'] = "Import Rides";

        $template['page_desc'] = "Import Broker Ride data through CSV File";
        $template['page_title'] = "Import Ride";

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

	function import(){
		$header = 0;
		$insertArr = array(); 
		$headerArr = array();
		$insertData = array();
		if (($handle = fopen($_FILES['csv_file']['tmp_name'], "r")) !== FALSE) {
		    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
		    	$colCnt = 0;
		    	$rowArr = array();
		    	foreach($row as $col){
		    		if($header == 0){
			    		$headerArr[] = $col;
			    	}else{
			    		$rowArr[][$headerArr[$colCnt]] = $col;
			    		$colCnt++;
			    	}
		    	}
		    	$insertData = array('medical_no'=>$row[0],
			    							'patient_name'=>$row[2].' '.$col[1],
			    							'age'=>$row[4],
			    							'phone'=>$row[5],
			    						    'trip_no'=>$row[7],
			    						    'appointment_time'=>$row[8].' '.$col[8],
											'reason_code'=>$row[11],
											'trip_cost'=>$row[22],
											'pickup_location'=>$row[23],
											'drop_location'=>$row[28],
											'trip_bid_status'=>$row[37],
											'trip_status'=>$row[12],
											'vehicle_type'=>$row[13],
											'trip_type'=>$row[14]);

		    	$insertData['data'] = json_encode($rowArr);
		    	$insertArr[] = $insertData;
		    	$header = 1;
		    }
		    pr($insertArr);
		    fclose($handle);
		}
		pr($json_data);
	}
}