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

class Vehicle extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set("Asia/Kolkata");
		$this->load->model('Vehicle_model');
		
		if(!$this->session->userdata('logged_in')) {
			redirect(base_url('Login'));
		}
 	}
 	
	public function add_vehicle_type() {
		$template['page'] = 'Vehicle/add_vehicle_type';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "Manage Vehicle Types";

        $template['page_desc'] = "Add New Vehicle Type";
        $template['page_title'] = "Vehicle Type Management";

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

	public function view_vehicle_types() {
		$template['page'] = 'Vehicle/view_vehicle_types';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "Manage Vehicle Types";

        $template['page_desc'] = "Manage Vehicles Types";
        $template['page_title'] = "Vehicle Types";
        
        $template['vehicles_data'] = $this->Vehicle_model->getVehicleType();

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

	public function addVehicle(){
		$err = 0;
		$errMsg = '';
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(!isset($_POST) || empty($_POST)){
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle_type'));
		}
		if($err == 0 && (!isset($_POST['vehicle_id']) || empty($_POST['vehicle_id']))){
			$err = 1;
			$errMsg = 'Provide a Vehicle Type Code';
		}else if($err == 0 && (!isset($_POST['vehicle_type']) || empty($_POST['vehicle_type']))){
			$err = 1;
			$errMsg = 'Provide a Vehicle Type';
		}
		if($err == 1){
			$flashMsg['message'] = $errMsg;
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle_type'));
		}
		if($err == 0){
	        $config = set_upload_service("assets/uploads/services");
	        $this->load->library('upload');
	        $config['file_name'] = time()."_".$_FILES['vehicle_photo']['name'];
	        $this->upload->initialize($config);
	        if(!$this->upload->do_upload('vehicle_photo')){
	        	$err = 1;
				$errMsg = $this->upload->display_errors();
	        }else{
	            $upload_data = $this->upload->data();
	            $_POST['vehicle_photo'] = $config['upload_path']."/".$upload_data['file_name'];
	        }
	    }
		$status = $this->Vehicle_model->addVehicleType($_POST);
		if($status == 1){
			$flashMsg['class'] = 'success';
			$flashMsg['message'] = 'Vehicle Created';
			$this->session->set_flashdata('message',$flashMsg);
	        redirect(base_url('Vehicle/view_vehicle_types'));
		}else if($status == 2){
			$flashMsg['message'] = 'Vehicle Type already exist.';
		}
		$this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Vehicle/add_vehicle_type'));
	}
	
	function changeVehicleTypeStatus($vehicle_id = '',$status = '1'){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id)){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicle_types'));
		}
		$vehicle_id = decode_param($vehicle_id);
		$status = $this->Vehicle_model->changeVehicleTypeStatus($vehicle_id,$status);
		if(!$status){
			$this->session->set_flashdata('message',$flashMsg);
		}
		redirect(base_url('Vehicle/view_vehicle_types'));
	}

	function edit($vehicle_id = ''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id)){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicle_types'));
		}
		$vehicle_id = decode_param($vehicle_id);

		$template['page'] = 'Vehicle/edit_vehicle_type';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "Manage Vehicle Types";

        $template['page_desc'] = "Modify Vehicle Type";
        $template['page_title'] = "Edit Vehicle Type";
        $template['vehicle_id'] = $vehicle_id;
        $template['vehicle_data'] = $this->Vehicle_model->getVehicleType($vehicle_id);

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

	function update($vehicle_id = ''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id)){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicle_types'));
		}
		$vehicle_id = decode_param($vehicle_id);

		$err = 0;
		$errMsg = '';
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(!isset($_POST) || empty($_POST)){
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/edit/'.encode_param($vehicle_id)));
		}
		if($err == 0 && (!isset($_POST['vehicle_id']) || empty($_POST['vehicle_id']))){
			$err = 1;
			$errMsg = 'Provide a Vehicle Code';
		} else if($err == 0 && (!isset($_POST['vehicle_type']) || empty($_POST['vehicle_type']))){
			$err = 1;
			$errMsg = 'Provide a Vehicle Type';
		}
		if($err == 1){
			$flashMsg['message'] = $errMsg;
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/edit/'.encode_param($vehicle_id)));
		}
		$_POST['vehicle_photo'] = '';
		if(isset($_FILES['vehicle_photo']) && !empty($_FILES['vehicle_photo'])){
	        $config = set_upload_service("assets/uploads/services");
	        $this->load->library('upload');
	        $config['file_name'] = time()."_".$_FILES['vehicle_photo']['name'];
	        $this->upload->initialize($config);
	        if($this->upload->do_upload('vehicle_photo')){
	        	$upload_data = $this->upload->data();
	            $_POST['vehicle_photo'] = $config['upload_path']."/".$upload_data['file_name'];
	        }
	    }

		$status = $this->Vehicle_model->updateVehicleType($vehicle_id,$_POST);
		if($status == 1){
			$flashMsg['class'] = 'success';
			$flashMsg['message'] = 'Vehicle Details Updated';
			$this->session->set_flashdata('message',$flashMsg);
			redirect(base_url('Vehicle/view_vehicle_types'));
		}else if($status == 2){
			$flashMsg['message'] = 'Vehicle Type already in use.';
		}
		$flashMsg['message'] = 'Something went wrong, please try again with different Vehicle Code..!';
		$this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Vehicle/edit/'.encode_param($vehicle_id)));
	}

	public function add_vehicle() {
		$template['page'] = 'Vehicle/add_vehicle';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "Add Vehicle";

        $template['page_desc'] = "Create New Vehicle";
        $template['page_title'] = "Add Vehicle";

        $template['vehicle_types'] = $this->Vehicle_model->getVehicleType();

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

	public function createVehicle() {
		$err = 0;
        $errMsg = '';
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(!isset($_POST) || empty($_POST) || !isset($_FILES) || empty($_FILES)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle'));
        }
        if($err == 0 && (!isset($_POST['vehicle_model']) || empty($_POST['vehicle_model']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Model';
        }
        else if($err == 0 && (!isset($_POST['vehicle_type']) || empty($_POST['vehicle_type']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Type';
        }
        else if($err == 0 && (!isset($_POST['vehicle_reg_no']) || empty($_POST['vehicle_reg_no']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Registration Number';
        }
        else if($err == 0 && (!isset($_POST['model']) || empty($_POST['model']))){
            $err = 1;
            $errMsg = 'Provide Model';
        }
        else if($err == 0 && (!isset($_FILES['vehicle_reg_image']) || empty($_FILES['vehicle_reg_image']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Registration Image';
        }
        $_POST['vehicle_reg_image'] = '';
        if($err == 0){
            $config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['vehicle_reg_image']['name'];
            $this->upload->initialize($config);
            if(!$this->upload->do_upload('vehicle_reg_image')){
                $err = 1;
                $errMsg = $this->upload->display_errors();
            }else{
                $upload_data = $this->upload->data();
                $_POST['vehicle_reg_image'] = $config['upload_path']."/".$upload_data['file_name'];
            }
        }
        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle'));
        }
        $status = $this->Vehicle_model->createVehicle($_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Vechile Added';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/view_vehicles'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Vehicle/add_vehicle'));
	}

	public function view_vehicles(){
		$template['page'] = 'Vehicle/view_vehicles';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "View Vehicles";

        $template['page_desc'] = "View and Manage Vehicles";
        $template['page_title'] = "View Vehicle";

        $template['vehicle_data'] = $this->Vehicle_model->getVehiclesData();

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

	public function changeVehicleStatus($vehicle_id = '', $status = ''){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id) || $status == ''){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicles'));
		}
		$vehicle_id = decode_param($vehicle_id);
		$status = $this->Vehicle_model->changeVehicleStatus($vehicle_id,$status);
		if(!$status){
			$this->session->set_flashdata('message',$flashMsg);
		}
		redirect(base_url('Vehicle/view_vehicles'));
	}

	public function editVehicle($vehicle_id = ''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id)){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicles'));
		}
		$template['page'] = 'Vehicle/add_vehicle';

		$template['menu'] = "Vehicle Management";
		$template['sub_menu'] = "Edit Vehicle Details";

        $template['page_desc'] = "Edit Vehicle Details";
        $template['page_title'] = "Edit Vehicle";

        $template['vehicle_id'] = $vehicle_id;
        $template['vehicle_data'] = $this->Vehicle_model->getVehiclesData(decode_param($vehicle_id));
        $template['vehicle_types'] = $this->Vehicle_model->getVehicleType();

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

	public function updateVehicle($vehicle_id = ''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($vehicle_id)){
			$this->session->set_flashdata('message',$flashMsg);
        	redirect(base_url('Vehicle/view_vehicles'));
		}
		$err = 0;
        $errMsg = '';
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(!isset($_POST) || empty($_POST)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle/'.$vehicle_id));
        }
        if($err == 0 && (!isset($_POST['vehicle_model']) || empty($_POST['vehicle_model']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Model';
        }
        else if($err == 0 && (!isset($_POST['vehicle_type']) || empty($_POST['vehicle_type']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Type';
        }
        else if($err == 0 && (!isset($_POST['vehicle_reg_no']) || empty($_POST['vehicle_reg_no']))){
            $err = 1;
            $errMsg = 'Provide Vehicle Registration Number';
        }
        else if($err == 0 && (!isset($_POST['model']) || empty($_POST['model']))){
            $err = 1;
            $errMsg = 'Provide Model';
        }

        if($err == 0 && isset($_FILES) && !empty($_FILES) && isset($_FILES['vehicle_reg_image']) && 
    	   !empty($_FILES['vehicle_reg_image'])){
        	$config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['vehicle_reg_image']['name'];
            $this->upload->initialize($config);
            if($this->upload->do_upload('vehicle_reg_image')){
                $upload_data = $this->upload->data();
                $_POST['vehicle_reg_image'] = $config['upload_path']."/".$upload_data['file_name'];
            }
        }
        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/add_vehicle/'.$vehicle_id));
        }
        $status = $this->Vehicle_model->updateVehicle(decode_param($vehicle_id),$_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Vechile Details Updated';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Vehicle/view_vehicles'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Vehicle/add_vehicle'));
	}

	function getVehicleData(){
		$retStatus = array('status'=>'0');
		if(!isset($_POST) || empty($_POST) || 
		   !isset($_POST['vehicle_type']) || empty($_POST['vehicle_type'])){
			echo json_encode($retStatus);exit;
		}
        $retStatus['data'] = $this->Vehicle_model->getVehiclesData('',$_POST['vehicle_type']);
        $retStatus['status'] = 1;
        echo json_encode($retStatus);exit;
	}
}
?>