<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\driver_model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\vehicles\vehicles_model;

class drivers extends Controller
{
    public function index(){
        $data['drivers']=DB::table('drivers')
            ->join('vehicles', 'vehicles.vehicle_owner_id','drivers.driver_id')
            ->get();

        //$data['drivers']=DB::select('select * from drivers');
        return view('drivers.drivers',$data);
    }

    public function maintain_driver(){

        return view('drivers.maintain_drivers');
    }
    public function autocomplete_driver_vehicle(Request $request){
        $query = $request->get('query');
        //$driv=new driver_model();
          $filterResult =vehicles_model::select('vehcile_id')->where('vehcile_id', 'LIKE', '%'. $query. '%')->pluck('vehcile_id');
          return response()->json($filterResult);

    }
    public function get_vehicle_info(Request $request){
        $vehicle_id=$request->post('vehicle_id');
        $did=DB::table('vehicles')
                ->where('vehcile_id', $vehicle_id)
                ->get();
        
        foreach($did as $d){
          return $d->vehicle_make;   
        }
    }
    public function save_drivers(Request $request)
    {
        $rules = [
            'driver_id'=>'string|min:3|unique:drivers',
            'driver_pass'=>'string|min:3',
            'device_phone'=>'required|numeric|min:11',
            'sort_code'=>'required|numeric|min:6',
            'account_number'=>'required|numeric|min:8'
        ];
        $validator = Validator::make($request->all(),$rules);
        if ($validator->fails()) {
        
            return redirect('maintain_driver')
            ->withInput()
            ->withErrors($validator);
        }
        else{
        $data=$request->input();
        $driver = new driver_model;
        $driver->company_name=$data['company_name'];
        $driver->record_id=rand(10000000,99999999);
        $driver->driver_id=$data['driver_id'];
        $driver->driver_pass=md5($data['driver_pass']);
        $driver->siid=$data['siid'];
        $driver->driver_vehicle=$data['driver_vehicle'];
        $driver->device_phone=$data['device_phone'];
        $driver->driver_status=$data['driver_status'];
        $driver->last_active=$data['last_active'];
        $driver->driver_type=$data['driver_type'];
        $driver->first_name=$data['first_name'];
        $driver->last_name=$data['last_name'];
        $driver->aka=$data['aka'];
        $driver->driver_address=$data['driver_address'];
        $driver->driver_email=$data['driver_email'];
        $driver->mobile_phone=$data['mobile_phone'];
        $driver->other_phone=$data['other_phone'];
        $driver->driver_sex=$data['driver_sex'];
        $driver->ethnicity=$data['ethnicity'];
        $driver->payment_card=$data['payment_card'];
        $driver->card_expiry_month=$data['card_expiry_month'];
        $driver->card_expiry_year=$data['card_expiry_year'];
        $driver->license_start_date=$data['license_start_date'];
        $driver->license_verb_date=$data['license_verb_date'];
        $driver->badge=$data['badge'];
        $driver->badge_expiry=$data['badge_expiry'];
        $driver->badge_expiry_verb=$data['badge_expiry_verb'];
        $driver->badge_expiry_time=$data['badge_expiry_time'];
        $driver->badge_type=$data['badge_type'];
        $driver->driver_license_nu=$data['driver_license_nu'];
        $driver->license_expiry=$data['license_expiry'];
        $driver->license_expiry_verb=$data['license_expiry_verb'];
        $driver->license_expiry_time=$data['license_expiry_time'];
        $driver->school_badge_expiry=$data['school_badge_expiry'];
        $driver->school_badge_expiry_verb=$data['school_badge_expiry_verb'];
        $driver->school_badge_expiry_time=$data['school_badge_expiry_time'];
        $driver->id_number=$data['id_number'];
        $driver->pvgdisclosure=$data['pvgdisclosure'];
        $driver->uiformed=$data['uiformed'];
        $driver->topman=$data['topman'];
        $driver->acceptdiscount=$data['acceptdiscount'];
        $driver->acceptaccount=$data['acceptaccount'];
        $driver->acceptfixedfare=$data['acceptfixedfare'];
        $driver->device_imei=$data['device_imei'];
        $driver->device_locked=$data['device_locked'];
        $driver->device_os=$data['device_os'];
        $driver->phone_assist=$data['phone_assist'];
        $driver->invoice_frequency=$data['invoice_frequency'];
        $driver->payment_terms=$data['payment_terms'];
        $driver->output_options=$data['output_options'];
        $driver->invoice_footer=$data['invoice_footer'];
        if($data['shifts']==''){
        $driver->shifts='';
        }
        else{
            $driver->shifts=json_encode($data['shifts']);
        }
        $driver->payment_day=$data['payment_day'];
        $driver->payment_type=$data['payment_type'];
        $driver->last_payment_date=$data['last_payment_date'];
        $driver->last_payment_verb=$data['last_payment_verb'];
        $driver->last_payment_time=$data['last_payment_time'];
        $driver->distribution=$data['distribution'];
        $driver->apply_vat=$data['apply_vat'];
        if($data['vat_rate']==''){
         $driver->vat_rate='0.00';   
        }
        else{
            $driver->vat_rate=$data['vat_rate'];
        }
        if($data['balance']==''){
         $driver->balance='0.00';
        }
        else{
           $driver->balance=$data['balance']; 
        }
        
        $driver->exclude_booking=$data['exclude_booking'];
        if($data['commission']==''){
            $driver->commission='0.00';
        }
        else{
         $driver->commission=$data['commission'];   
        }
        
        $driver->auto_post=$data['auto_post'];
        $driver->payment_vat_card=$data['payment_vat_card'];
        $driver->payment_vat_card_expiry_month=$data['payment_vat_card_expiry_month'];
        $driver->payment_vat_card_expiry_year=$data['payment_vat_card_expiry_year'];
        $driver->bank_payment_ref=$data['bank_payment_ref'];
        $driver->use_sepa=$data['use_sepa'];
        $driver->bank_name=$data['bank_name'];
        $driver->account_name=$data['account_name'];
        $driver->sort_code=$data['sort_code'];
        $driver->account_number=$data['account_number'];

        $driver->save();
        return redirect('maintain_driver')->with('status',"Driver record inserted successfully");
    }
  }

  //driver login

  public function driverlogin(){
    return view('drivers.driverlogin');
  }

  








}
