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

class Api_model extends CI_Model{
	
	public function add_postion($postarray) { 
		return	$this->db->insert('positions',$postarray);
	} 
    public function checkgps_auth($v_id) { 
        $this->db->where('v_api_username', $v_id);
        $query = $this->db->get("vehicles");
		if ($query->num_rows() >= 1) {
			return $query->result_array();
		} 
	}
	public function  get_driver_info($driver_id){
		$this->db->select("*");
		$this->db->from('drivers');
		$this->db->join('organization', 'organization.org_id=drivers.organization_id');
		$this->db->join('vehicles', 'vehicles.v_driver_id=drivers.d_id');
		$this->db->join('vehicle_group', 'vehicle_group.gr_id=vehicles.v_group');
		$this->db->where('drivers.d_id',$driver_id);
		$query = $this->db->get();
	   	return $query->result_array();
	} 

	public function file_upload($file_data,$driver_id,$driver_name,$document_name,$org_code){

		$file_ext = pathinfo($file_data['name'],PATHINFO_EXTENSION);
		$driver_name_trim= preg_replace('/\s\s+/', ' ', $driver_name);
	    $driver_name = str_replace(' ', '_',$driver_name_trim);  

		$file_name= $driver_id.'-'.$driver_name.'-'.$document_name.'.'.$file_ext;

		//check file exist on db
		$d_where=array("driver_id"=>$driver_id,"document_name"=>$document_name);
		$this->db->select('*');
		$this->db->from('drivers_documents');
		$this->db->where($d_where); 
		$query = $this->db->get();
		//return $this->db->last_query();
		$document_data= $query->row_array();
		if($document_data){
			$file_name_data=$document_data['document_file'];
			$file_ifo='assets/uploads/'.$org_code.'/driver_documents/'.$file_name_data;
			if(file_exists($file_ifo)){  
				$delete_file='assets/uploads/'.$org_code.'/driver_documents/'.$file_name_data;
				unlink($delete_file);
			}
		}
		
		$_FILES['file']['name']     = $driver_id.'-'.$driver_name.'-'.$document_name.'.'.$file_ext;
		$_FILES['file']['type']     = $file_data['type'];
		$_FILES['file']['tmp_name'] = $file_data['tmp_name'];
                $_FILES['file']['size']     = $file_data['size'];
		$_FILES['file']['error']     = $file_data['error'];  

               //size check
                if($_FILES['file']['size']>2097152)
		{
	           $result = ["status"=>0,"message"=>"below 2mb"];
	          echo json_encode($result);
	         exit();
		}else{                                     
		//File upload configuration photographs
       // echo $file_data['name'];

		if (!is_dir('assets/uploads/'.$org_code.'/driver_documents')) {
			mkdir('./assets/uploads/' . $org_code.'/driver_documents', 0777, TRUE);  
		}
		$uploadPath = 'assets/uploads/'.$org_code.'/driver_documents';
		$config['upload_path'] = $uploadPath;
		$config['allowed_types'] = 'jpg|jpeg|png'; 
		// $config['encrypt_name'] = TRUE;
		// Load and initialize upload library 
		$this->load->library('upload', $config);
		$this->upload->initialize($config); 
		// Upload file to server
		if($this->upload->do_upload('file'))
		{	
			//echo 'Successfully update';
			$fileData = $this->upload->data();

			$data_insert=array(
					'driver_id'=>$driver_id,
					'document_name'=>$document_name,
					'document_file'=>$file_name,
					'doc'=> date("Y-m-d H:i:s")					
				); 
				if($document_data){
					$update_data=array("document_file"=>$file_name); 
					return $insert_data=$this->comman_model->update('drivers_documents', $d_where, $update_data);
				}else{
					return $insert_data=$this->comman_model->insert_record('drivers_documents', $data_insert);
				}
			
		}
          }
		
	} 


} 