<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\CarWashOwner;
use App\Models\User;
use App\Models\CarOwner;
use App\Models\CarOwnerCar;
use App\Models\WashDetail;
use App\Models\BodyType;
use App\Models\WashCategoryBodyType;
use App\Models\OptionalExtra;
use App\Models\CleanerWash;
use App\Models\Loyalty;
use App\Models\WashTypeToWash;
use App\Models\WashCategory;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Config;
use Hash;
use Auth;

class CarController extends Controller
{
    public function __construct()
    {
    }


    public function index()
    {
    }

    
    public function carsInQueue(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'cwo_id' => 'required',
                // 'start_date' => 'required',
                // 'end_date' => 'required',
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        // $start_date = $request->input('start_date'); // format -> yyyy-mm-dd, today or less than today
        // $end_date = $request->input('end_date'); // format -> yyyy-mm-dd, today
        // $today = date('Y-m-d');

        $getCurrency = getCurencyOfCwo($cwo_id);

        /***************** queue starts *********************/
        /*$carsInQueueList = WashDetail::select('wash_details.id', 'wash_details.car_id', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status', 'wash_details.payment_status', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'co_car.car_registration_no', 'wash_categories.wash_name', 'wash_details.otp', 'wash_details.payment_details', 'wash_details.amount', 'wash_details.total_amount', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.is_free_wash')
        ->join('co_car', 'co_car.id', '=', 'wash_details.car_id')
        ->join('co_master', 'co_master.id', '=', 'co_car.co_id')
        ->join('users', 'users.id', '=', 'co_master.user_id')
        ->join('wash_categories', 'wash_categories.id', '=', 'wash_details.wash_type_id')
        ->join('cwo_master', 'cwo_master.id', '=', 'wash_categories.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('wash_categories.cwo_id', $cwo_id)
        // ->where('wash_details.status', 0);
        ->where('wash_details.status', '!=', 3);*/
        $carsInQueueList = WashDetail::select('wash_details.id', 'wash_details.car_id', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status', 'wash_details.payment_status', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'co_car.car_registration_no', 'wash_details.otp', 'wash_details.payment_details', 'wash_details.amount', 'wash_details.total_amount', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.is_free_wash', 'wash_details.wash_type_id', 'wash_details.keys_collected', 'wash_details.image', 'wash_details.discount', 'co_car.make', 'co_car.vehicle_color', 'co_car.car_model')
        ->join('co_car', 'co_car.id', '=', 'wash_details.car_id')
        ->join('co_master', 'co_master.id', '=', 'co_car.co_id')
        ->join('users', 'users.id', '=', 'co_master.user_id')
        ->join('cwo_master', 'cwo_master.id', '=', 'wash_details.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('wash_details.cwo_id', $cwo_id)
        ->where('wash_details.status', '!=', 4)
        ->where('wash_details.status', '!=', 3)
        ->where('wash_details.status', '!=', 5);
        // if($start_date != '') {
        //     $carsInQueueList->whereDate('wash_details.date_washed', '>=', $start_date);
        // }
        // if($end_date != '') {
        //     $carsInQueueList->whereDate('wash_details.date_washed', '<=', $end_date);
        // }
        // $carsInQueueList = $carsInQueueList->orderBy('co_car.id', 'desc');
        $carsInQueueList = $carsInQueueList->orderBy('wash_details.id', 'desc');
        $carsInQueueCount = $carsInQueueList->count();
        $carsInQueueList = $carsInQueueList->get();


        // echo "<pre>";print_r($carsInQueueList->toArray());exit;
        /***************** queue ends *********************/

        $dataDisplay=[];
        $dataArray = [];
        if($carsInQueueList != '') {
            foreach ($carsInQueueList as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['car_id'] = $d->car_id;
                if($d->driver_name != ''){
                    $dataDisplay['co_fname'] = $d->driver_name;
                    $dataDisplay['co_lname'] = '';
                } else {
                    $dataDisplay['co_fname'] = $d->co_fname;
                    $dataDisplay['co_lname'] = $d->co_lname;
                }
                if($d->driver_phone != ''){
                    $dataDisplay['isd_code'] = '+' . $d->driver_isd_code;
                    $dataDisplay['co_phone'] = $d->driver_phone;
                } else {
                    $dataDisplay['isd_code'] = '+' . $d->isd_code;
                    $dataDisplay['co_phone'] = $d->co_phone;
                }
                
                $dataDisplay['car_registration_no'] = $d->car_registration_no;
                $dataDisplay['make'] = $d->make;
                $dataDisplay['vehicle_color'] = $d->vehicle_color;
                if($d->vehicle_color == '') {
                    $dataDisplay['vehicle_color'] = '';
                }
                $dataDisplay['car_model'] = $d->car_model;
                if($d->car_model == '') {
                    $dataDisplay['car_model'] = '';
                }
                // $dataDisplay['wash_name'] = $d->wash_name;
                if($d->date_washed != '') {
                    $dataDisplay['date_washed']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['date_washed']  = '';
                }
                if($d->time_washed != '') {
                    $dataDisplay['time_washed']  = date("h:i a", strtotime($d->time_washed));
                } else {
                    $dataDisplay['time_washed']  = '';
                }
                $dataDisplay['payment_status_id'] = $d->payment_status;
                if($d->payment_status == 1) {
                    $dataDisplay['payment_status'] = 'Paid';
                } else {
                    $dataDisplay['payment_status'] = 'Not Paid';
                }
                $dataDisplay['status_id'] = $d->status;
                if($d->status == 0) {
                    $dataDisplay['status'] = 'Car in-queue';
                }
                if($d->status == 1) {
                    $dataDisplay['status'] = 'In Wash Bay';
                }
                if($d->status == 2) {
                    $dataDisplay['status'] = 'Finishing Bay';
                }
                if($d->status == 3) {
                    $dataDisplay['status'] = 'Car Wash Completed';
                }
                if($d->status == 4) {
                    $dataDisplay['status'] = 'Cancelled';
                }
                if($d->status == 5){
                    $dataDisplay['status'] = 'Keys Collected';    
                }
                if($d->is_free_wash == 1) {
                    $dataDisplay['is_free_wash'] = true;
                }
                if($d->is_free_wash == 0) {
                    $dataDisplay['is_free_wash'] = false;
                }
                $dataDisplay['otp'] = $d->otp;
                $dataDisplay['payment_mode'] = $d->payment_details;
                $dataDisplay['wash_amount'] = $d->amount;
                $dataDisplay['total_amount'] = $d->total_amount;
                $dataDisplay['discount_value_in_percentage'] = $d->discount;
                if($d->keys_collected != ''){
                    $dataDisplay['keys_collected']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['keys_collected'] = '';
                } 

                // if($d->image != ''){
                //     $dataDisplay['image'] = asset('images/wash') . '/' . $d->image;
                // } else {
                //     // $cwo_logo = '';
                //     $dataDisplay['image'] = '';
                // }
                if($d->image != '') {
                    $dataDisplay['image'] = $d->image;
                } else {
                    $dataDisplay['image'] = '';
                }
                if($dataDisplay['image'] == "null"){
                    $dataDisplay['image'] = '';
                }

                $optionalExtra = OptionalExtra::where('wash_id', $d->id)->select('optional_extra', 'price')->orderBy('id', 'asc')->get();
                $dataDisplay['extras'] = $optionalExtra;

                $cleaners = CleanerWash::select('cleaner_masters.name', 'cleaner_washes.description as cleaning_description')
                            ->join('cleaner_masters', 'cleaner_masters.id', '=', 'cleaner_washes.cleaner_id')
                            ->where('cleaner_washes.wash_id', $d->id)
                            ->orderBy('cleaner_washes.id', 'asc')
                            ->get();
                $dataDisplay['cleaners'] = $cleaners;

                $wash_types = WashTypeToWash::select('wash_categories.wash_name', 'wash_type_to_washes.price')
                            ->join('wash_categories', 'wash_categories.id', '=', 'wash_type_to_washes.wash_type_id')
                            ->where('wash_type_to_washes.wash_id', $d->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
                $dataDisplay['wash_types'] = $wash_types;

                if($d->wash_type_id > 0){
                    $getWashType = WashCategory::where('id', $d->wash_type_id)->select('wash_name')->first();
                    if($getWashType != ''){
                        $dataDisplay['wash_name'] = $getWashType->wash_name;    
                    } else {
                        $dataDisplay['wash_name'] = '';
                    }
                } else {
                    $dataDisplay['wash_name'] = '';
                }

                $dataArray[] = $dataDisplay;
            }
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'carsInQueueCount' => $carsInQueueCount,
            'carsInQueueList' => $dataArray
        ]);

    }

    public function carsInQueue_NewSplit(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'cwo_id' => 'required',
                // 'start_date' => 'required',
                // 'end_date' => 'required',
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $getCurrency = getCurencyOfCwo($cwo_id);

        /***************** queue starts *********************/
        $carsInQueueList = WashDetail::select('wash_details.id', 'wash_details.car_id', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status', 'wash_details.payment_status', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'co_car.car_registration_no', 'wash_details.otp', 'wash_details.payment_details', 'wash_details.amount', 'wash_details.total_amount', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.is_free_wash', 'wash_details.wash_type_id', 'wash_details.keys_collected', 'wash_details.image', 'wash_details.discount', 'co_car.make', 'co_car.vehicle_color', 'co_car.car_model', 'wash_details.card_amount', 'wash_details.cash_amount')
        ->join('co_car', 'co_car.id', '=', 'wash_details.car_id')
        ->join('co_master', 'co_master.id', '=', 'co_car.co_id')
        ->join('users', 'users.id', '=', 'co_master.user_id')
        ->join('cwo_master', 'cwo_master.id', '=', 'wash_details.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('wash_details.cwo_id', $cwo_id)
        ->where('wash_details.status', '!=', 4)
        ->where('wash_details.status', '!=', 3)
        ->where('wash_details.status', '!=', 5);
        // $carsInQueueList = $carsInQueueList->orderBy('co_car.id', 'desc');
        $carsInQueueList = $carsInQueueList->orderBy('wash_details.id', 'desc');
        $carsInQueueCount = $carsInQueueList->count();
        $carsInQueueList = $carsInQueueList->get();

        // echo "<pre>";print_r($carsInQueueList->toArray());exit;
        /***************** queue ends *********************/
        $dataDisplay=[];
        $dataArray = [];
        if($carsInQueueList != '') {
            foreach ($carsInQueueList as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['car_id'] = $d->car_id;
                if($d->driver_name != ''){
                    $dataDisplay['co_fname'] = $d->driver_name;
                    $dataDisplay['co_lname'] = '';
                } else {
                    $dataDisplay['co_fname'] = $d->co_fname;
                    $dataDisplay['co_lname'] = $d->co_lname;
                }
                if($d->driver_phone != ''){
                    $dataDisplay['isd_code'] = '+' . $d->driver_isd_code;
                    $dataDisplay['co_phone'] = $d->driver_phone;
                } else {
                    $dataDisplay['isd_code'] = '+' . $d->isd_code;
                    $dataDisplay['co_phone'] = $d->co_phone;
                }
                
                $dataDisplay['car_registration_no'] = $d->car_registration_no;
                $dataDisplay['make'] = $d->make;
                $dataDisplay['vehicle_color'] = $d->vehicle_color;
                if($d->vehicle_color == '') {
                    $dataDisplay['vehicle_color'] = '';
                }
                $dataDisplay['car_model'] = $d->car_model;
                if($d->car_model == '') {
                    $dataDisplay['car_model'] = '';
                }
                // $dataDisplay['wash_name'] = $d->wash_name;
                if($d->date_washed != '') {
                    $dataDisplay['date_washed']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['date_washed']  = '';
                }
                if($d->time_washed != '') {
                    $dataDisplay['time_washed']  = date("h:i a", strtotime($d->time_washed));
                } else {
                    $dataDisplay['time_washed']  = '';
                }
                $dataDisplay['payment_status_id'] = $d->payment_status;
                if($d->payment_status == 1) {
                    $dataDisplay['payment_status'] = 'Paid';
                } else {
                    $dataDisplay['payment_status'] = 'Not Paid';
                }
                $dataDisplay['status_id'] = $d->status;
                if($d->status == 0) {
                    $dataDisplay['status'] = 'Car in-queue';
                }
                if($d->status == 1) {
                    $dataDisplay['status'] = 'In Wash Bay';
                }
                if($d->status == 2) {
                    $dataDisplay['status'] = 'Finishing Bay';
                }
                if($d->status == 3) {
                    $dataDisplay['status'] = 'Car Wash Completed';
                }
                if($d->status == 4) {
                    $dataDisplay['status'] = 'Cancelled';
                }
                if($d->status == 5){
                    $dataDisplay['status'] = 'Keys Collected';    
                }
                if($d->is_free_wash == 1) {
                    $dataDisplay['is_free_wash'] = true;
                }
                if($d->is_free_wash == 0) {
                    $dataDisplay['is_free_wash'] = false;
                }
                $dataDisplay['otp'] = $d->otp;
                $dataDisplay['payment_mode'] = $d->payment_details;
                $dataDisplay['wash_amount'] = $d->amount;
                $dataDisplay['total_amount'] = $d->total_amount;
                $dataDisplay['card_amount'] = $d->card_amount;
                $dataDisplay['cash_amount'] = $d->cash_amount;
                $dataDisplay['discount_value_in_percentage'] = $d->discount;
                if($d->keys_collected != ''){
                    $dataDisplay['keys_collected']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['keys_collected'] = '';
                } 

                // if($d->image != ''){
                //     $dataDisplay['image'] = asset('images/wash') . '/' . $d->image;
                // } else {
                //     // $cwo_logo = '';
                //     $dataDisplay['image'] = '';
                // }
                if($d->image != '') {
                    $dataDisplay['image'] = $d->image;
                } else {
                    $dataDisplay['image'] = '';
                }
                if($dataDisplay['image'] == "null"){
                    $dataDisplay['image'] = '';
                }

                $optionalExtra = OptionalExtra::where('wash_id', $d->id)->select('optional_extra', 'price')->orderBy('id', 'asc')->get();
                $dataDisplay['extras'] = $optionalExtra;

                $cleaners = CleanerWash::select('cleaner_masters.name', 'cleaner_washes.description as cleaning_description')
                            ->join('cleaner_masters', 'cleaner_masters.id', '=', 'cleaner_washes.cleaner_id')
                            ->where('cleaner_washes.wash_id', $d->id)
                            ->orderBy('cleaner_washes.id', 'asc')
                            ->get();
                $dataDisplay['cleaners'] = $cleaners;

                $wash_types = WashTypeToWash::select('wash_categories.wash_name', 'wash_type_to_washes.price')
                            ->join('wash_categories', 'wash_categories.id', '=', 'wash_type_to_washes.wash_type_id')
                            ->where('wash_type_to_washes.wash_id', $d->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
                $dataDisplay['wash_types'] = $wash_types;

                if($d->wash_type_id > 0){
                    $getWashType = WashCategory::where('id', $d->wash_type_id)->select('wash_name')->first();
                    if($getWashType != ''){
                        $dataDisplay['wash_name'] = $getWashType->wash_name;    
                    } else {
                        $dataDisplay['wash_name'] = '';
                    }
                } else {
                    $dataDisplay['wash_name'] = '';
                }

                $dataArray[] = $dataDisplay;
            }
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'carsInQueueCount' => $carsInQueueCount,
            'carsInQueueList' => $dataArray
        ]);

    }

    public function getBodyTypes(Request $request)
    {
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required'
            ]);
            
        if($validate->fails()){
            $errorString = implode(",",$validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $dataDisplay=[];
        $dataArray = [];

        $bodyTypeOfOwner = BodyType::where('status', 1)->where('cwo_id', $cwo_id)->orderBy('sort_order', 'asc')->get();
        if($bodyTypeOfOwner != ''){
            foreach ($bodyTypeOfOwner as $d) {
                // $getBodyTypePriceCount = WashCategoryBodyType::where('body_type_id', $d->id)->count();
                $getBodyTypePriceCount = WashCategoryBodyType::join('wash_categories','wash_categories.id','=','wash_category_body_types.wash_category_id')
                                        ->where('wash_category_body_types.body_type_id', $d->id)
                                        ->where('wash_categories.cwo_id', $cwo_id)
                                        ->where('wash_categories.is_active', 1)
                                        ->count();
                if($getBodyTypePriceCount > 0){
                    $dataDisplay['id'] = $d->id;
                    $dataDisplay['name'] = $d->name;
                    $dataArray[] = $dataDisplay;
                }
            }
        }

        $body_types = BodyType::where('status', 1)->where('cwo_id', 0)->orderBy('sort_order', 'asc')->select('id', 'name')->get();
        if($body_types != ''){
            foreach ($body_types as $d) {
                $getBodyTypePriceCount = WashCategoryBodyType::join('wash_categories','wash_categories.id','=','wash_category_body_types.wash_category_id')
                                        ->where('wash_category_body_types.body_type_id', $d->id)
                                        ->where('wash_categories.cwo_id', $cwo_id)
                                        ->where('wash_categories.is_active', 1)
                                        ->count();
                if($getBodyTypePriceCount > 0){
                    $dataDisplay['id'] = $d->id;
                    $dataDisplay['name'] = $d->name;
                    $dataArray[] = $dataDisplay;
                }
            }
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            // 'body_types' => $body_types
            'body_types' => $dataArray
        ]);

    }

    public function manualEntry(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'car_registration_no' => 'required',
            ],
            [
                // 'email.unique' => 'This email already exists.'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }
        $car_registration_no = $request->input('car_registration_no');
        $make = $request->input('make');
        $keytag = $request->input('keytag');
        $body_type_id = $request->input('body_type_id');
        $vehicle_color = $request->input('vehicle_color');
        $car_model = $request->input('car_model');
        $dt = Carbon::now();
        if($body_type_id != '') {
            $getBodyType = BodyType::where('id', $body_type_id)->select('name')->first();
            if($getBodyType != '') {
                $body_type = $getBodyType->name;
            } else {
                $body_type_id = 0;
                $body_type = '';
            }

        } else {
            $body_type_id = 0;
            $body_type = '';
        }

        $checkCar = CarOwnerCar::where('car_registration_no', $car_registration_no)->first();
        // print_r($checkCar->toArray());exit;
        $getCo = [];
        // echo $body_type_id;exit;
        if(!empty($checkCar)) { //////////////////////////////////// update the car
            if($body_type_id != 0) {
                // echo "update";exit;
                $checkCar->update([
                    // 'make'=> $make,
                    // 'keytag'=> $keytag,
                    'vehicle_color'=> $vehicle_color,
                    'car_model'=> $car_model,
                    'updated_at'=>$dt->toDayDateTimeString()
                    ]);
            }
            // echo "do nothing";exit;
            if($checkCar->body_type_id == 0){
                $checkCar->update([
                    'body_type'=> $body_type,
                    'body_type_id'=> $body_type_id,
                    'updated_at'=>$dt->toDayDateTimeString()
                    ]);    
            }

            $getWashDetail = WashDetail::where('car_id', $checkCar->id)->select('driver_name', 'driver_isd_code', 'driver_phone', 'driver_email', 'dob')->orderBy('id', 'desc')->take(1)->get();
            // print_r($getWashDetail->toArray());exit;
            // dd($getWashDetail);
            if(!empty($getWashDetail[0])) {
                $getCoArray = [
                    'id' => 0,
                    'account_no' => '',
                    'co_fname' => $getWashDetail[0]->driver_name,
                    'co_lname' => '',
                    'co_phone' => $getWashDetail[0]->driver_phone,
                    'email' => $getWashDetail[0]->driver_email,
                    'co_image' => '',
                    'is_active' => '',
                    'created_at' => '',
                    'updated_at' => '',
                    'user_id' => 0,
                    'make' => $checkCar->make,
                    'keytag' => $checkCar->keytag,
                    'isd_code' => $getWashDetail[0]->driver_isd_code,
                    'dob' => $getWashDetail[0]->dob,
                ];
                $getCo = $getCoArray;
            } else {
                $getCo = CarOwner::where('id', $checkCar->co_id)->first();
                if($getCo !=''){
                    $getCoArray = [
                        'id' => $getCo->id,
                        'account_no' => $getCo->account_no,
                        'co_fname' => $getCo->co_fname,
                        'co_lname' => $getCo->co_lname,
                        'co_phone' => $getCo->co_phone,
                        'email' => $getCo->email,
                        'co_image' => '',
                        'is_active' => '',
                        'created_at' => '',
                        'updated_at' => '',
                        'user_id' => $getCo->user_id,
                        'make' => $checkCar->make,
                        'keytag' => $checkCar->keytag,
                        'isd_code' => $getCo->isd_code,
                        'dob' => $getCo->dob,
                    ];
                    $getCo = $getCoArray;
                } else {
                    $getCoArray = [
                        'id' => 0,
                        'account_no' => '',
                        'co_fname' => '',
                        'co_lname' => '',
                        'co_phone' => '',
                        'email' => '',
                        'co_image' => '',
                        'is_active' => '',
                        'created_at' => '',
                        'updated_at' => '',
                        'user_id' => 0,
                        'make' => $checkCar->make,
                        'keytag' => $checkCar->keytag,
                        'isd_code' => '',
                        'dob' => '',
                    ];
                    $getCo = $getCoArray;
                }
            }

            // $getCo = CarOwner::where('id', $checkCar->co_id)->first();
            $success = 1;
            $message = 'Success';
        } else { //////////////////////////////////////////////////// insert the car
            /*$body_type_id = $request->input('body_type_id');
            if($body_type_id != '') {
                $getBodyType = BodyType::where('id', $body_type_id)->select('name')->first();
                if($getBodyType != '') {
                    $body_type = $getBodyType->name;
                } else {
                    $body_type_id = 0;
                    $body_type = '';
                }

            } else {
                $body_type_id = 0;
                $body_type = '';
            }*/
            /*insert into co_car table starts*/
            $insertCar = CarOwnerCar::create([
                'car_registration_no'=> $car_registration_no,
                'body_type'=> $body_type,
                'body_type_id'=> $body_type_id,
                'make'=> $make,
                'keytag'=> $keytag,
                'vehicle_color'=> $vehicle_color,
                'car_model'=> $car_model,
                'created_at'=>$dt->toDayDateTimeString(),
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
            /*insert into co_car table ends*/
            $checkCar = CarOwnerCar::where('id', $insertCar->id)->first();
            $success = 1;
            // $message = 'Car registration no. does not exist.';
            $message = 'Success';
            $getCoArray = [
                'id' => 0,
                'account_no' => '',
                'co_fname' => '',
                'co_lname' => '',
                'co_phone' => '',
                'email' => '',
                'co_image' => '',
                'is_active' => '',
                'created_at' => '',
                'updated_at' => '',
                'user_id' => 0,
                'make' => $checkCar->make,
                'keytag' => $checkCar->keytag,
                'isd_code' => '',
                'dob' => '',
            ];
            $getCo = $getCoArray;
        }


        if($checkCar->image) {
            $checkCar->image=asset('images/CarOwner') . '/' . $checkCar->image;
        }
        return response()->json([
            'success' => $success,
            'message' => $message,
            'car_details' => $checkCar,
            'car_owner_details' => $getCo,
        ]);

    }


    public function updateCarImage(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'id' => 'required',
            ],
            [
                 'id.required' => 'Please provide car detail  id.'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }


        // co_car
        $id = $request->input('id');

        $checkCar = CarOwnerCar::where('id', $id)->first();

        $getCo = CarOwner::where('id', $checkCar->co_id)->first();




        $image = $checkCar->image;

        if ($request->hasFile('image') && $id) {
            $image = time() . '.' . $request->file('image')->extension();

            /***:- save new file -:***/
            $path = $request->file('image')->move(public_path('images/CarOwner/'), $image);


            /***:- delete old file -:***/

            // $oldFile=public_path('images/CarOwner/') . $checkCar->image;
            // if(file_exists($oldFile) && $checkCar->image!='') {
            //     unlink($oldFile);
            // }

            /***:- update data -:***/
            $checkCar->update([
            'image'=>$image
            ]);

        }

        $checkCar = CarOwnerCar::where('id', $id)->first();
        $asset=asset('images/CarOwner') . '/' . $checkCar->image;



        if($checkCar->image) {
            $checkCar->image=$asset;
        }


        return response()->json([
            'success' => 1,
            'message' =>'Success',
            'car_details' => $checkCar,
            'car_owner_details' => $getCo,
        ]);

    }

    public function addDriverDetails(Request $request)
    {

        $validate = Validator::make(
            $request->all(),
            [
                // 'name' => 'required',
                'car_id' => 'required',
            ],
            [
                // 'email.unique' => 'This email already exists.'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }

        $name = $request->input('name');
        $isd_code = $request->input('isd_code');
        $phone = $request->input('phone');
        $email = $request->input('email');
        $car_id = $request->input('car_id');
        $cwo_id = $request->input('cwo_id');
        $is_phone_check = $request->input('is_phone_check');
        $dob = $request->input('dob');
        if($dob != '') {
            $dob = date("Y-m-d", strtotime($dob)); 
        } else {
            $dob = NULL;
        }
        $dt = Carbon::now();
        $account_no = generateUniqueNumberCo();
        if($name == '') {
            $name = "";
        }

        /*update car starts*/
        $itemCar = CarOwnerCar::findOrFail($car_id);
        $itemCar->update([
            'make'=> $request->input('make'),
            'keytag'=> $request->input('keytag'),
            'updated_at'=>$dt->toDayDateTimeString()
            ]);
        /*update car ends*/

        /* Loyalty Starts */
        $is_free_wash = false;
        $loyalty_name = '';
        $loyalty_type = '';
        $no_of_paid_wash = '';
        $loyalty_type_text = '';
        $discount_value_in_percentage = '';
        $getLoyalty = Loyalty::where('cwo_id', $cwo_id)->where('is_active', 1)->first();
        // print_r($getLoyalty);exit;
        if($getLoyalty != ''){
            $no_of_paid_wash = $getLoyalty->no_of_paid_wash;
            // echo $no_of_paid_wash;exit;
            $loyalty_name = $getLoyalty->loyalty_name;
            $loyalty_type = $getLoyalty->loyalty_type;
            if($loyalty_type == 1){
                $loyalty_type_text = "Free Wash";
            }
            if($loyalty_type == 2){
                $loyalty_type_text = "Price Discount";
            }
            $discount_value_in_percentage = $getLoyalty->discount_value;

            if($is_phone_check != '') { // if is_phone_check exists in new version
                // echo "new version";exit;
                if($phone != '' && $isd_code != ''){ // check by phone no and isd
                    // echo "check by phone";exit;
                    $getWash = WashDetail::where('driver_isd_code', $isd_code)
                                            ->where('driver_phone', $phone)
                                            ->where('cwo_id', $cwo_id)
                                            // ->where('is_free_wash', 0)
                                            // ->where('status', 3)
                                            ->where('is_loyalty_utilized', 0)
                                            ->where(function($query) {
                                                $query->where('status', 3)
                                                    ->orWhere('status', 5);
                                            });
                    $getWashCount = $getWash->count();
                    if($getWashCount >= $no_of_paid_wash){
                        $is_free_wash = true;    
                    }
                } else { // check by registration no
                    // echo "check by reg no";exit;
                    $getLastFreeWash = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 1)->orderBy('id', 'desc')->take(1)->get();
                    // print_r($getLastFreeWash->toArray());exit;
                    // echo count($getLastFreeWash);exit;
                    if(count($getLastFreeWash) > 0){
                        // echo 'first wash->';exit;
                        $getWashCount = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 0)->where('status', 3)->where('id', '>', $getLastFreeWash[0]->id)->count();
                        
                    } else {
                        // echo 'second wash->';exit;
                        $getWashCount = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 0)->where('status', 3)->count();
                        
                    }
                    // echo $getWashCount;exit;
                    if($getWashCount >= $no_of_paid_wash){
                        $is_free_wash = true;    
                    }
                }
                // echo $getWashCount;exit;
            } else { // if is_phone_check does not exist in new version 
                // echo "old version";exit;
                $getLastFreeWash = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 1)->orderBy('id', 'desc')->take(1)->get();
                // print_r($getLastFreeWash->toArray());exit;
                // echo count($getLastFreeWash);exit;
                if(count($getLastFreeWash) > 0){
                    // echo 'first wash->';exit;
                    $getWashCount = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 0)->where('status', 3)->where('id', '>', $getLastFreeWash[0]->id)->count();
                    
                } else {
                    // echo 'second wash->';exit;
                    $getWashCount = WashDetail::where('car_id', $car_id)->where('cwo_id', $cwo_id)->where('is_free_wash', 0)->where('status', 3)->count();
                    
                }
                // echo $getWashCount;exit;
                // if($getWashCount == $no_of_paid_wash){
                //     $is_free_wash = true;    
                // }
                if($getWashCount >= $no_of_paid_wash){
                    $is_free_wash = true;    
                }
            } // if is_phone_check exists ends
        }
        // echo $is_free_wash;exit;
        // echo "no loyalty";exit;
        /* Loyalty ends */

        $getCheckCar = CarOwnerCar::where('id', $car_id)->select('co_id')->first();
        if($getCheckCar->co_id == 0){
            if($phone == '' && $email =='') {
                // echo "hello1";exit;
                /*insert into users table starts*/
                $insertUser = User::create([
                    'name'=> $name,
                    'isd_code'=> $isd_code,
                    'phone'=> $phone,
                    'email'=> $email,
                    'user_type'=> 3,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                $user_id = $insertUser->id;
                /*insert into co_master table starts*/
                $insertCo = CarOwner::create([
                    'co_fname'=> $name,
                    'co_lname'=> '',
                    'isd_code'=> $isd_code,
                    'co_phone'=> $phone,
                    'email'=> $email,
                    'user_id'=> $user_id,
                    'account_no'=> $account_no,
                    'dob'=> $dob,
                    'make'=> $request->has('make')?$request->make:null,
                    'keytag'=> $request->has('keytag')?$request->keytag:null,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                /*insert into co_master table ends*/
                $co_id = $insertCo->id;
            }
            if($phone != '' && $email =='') {
                // echo "hello2";exit;
                $getCoUser = User::where('phone', $phone)->where('user_type', 3)->first();
                if(!empty($getCoUser)) { // if co phone no already exists
                    // if($getCoUser->user_type == 3) { // if co phone no already exists for CO (user_type=3) then update the user
                        $co_user_id = $getCoUser->id;
                        $co = CarOwner::where('user_id', $co_user_id)->select('id')->first();
                        $co_id = $co->id;
                        $user_id = $co_user_id;
                        $userDetails = User::findOrFail($user_id);
                        $co->update([
                            // 'co_fname'=> $name,
                            // 'co_lname'=> '',
                            // 'isd_code'=> $isd_code,
                            // 'phone'=> $phone,
                            'dob'=> $dob,
                            'make'=> $request->has('make')?$request->make:null,
                            'keytag'=> $request->has('keytag')?$request->keytag:null,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
                        $userDetails->update([
                            // 'name'=> $name,
                            // 'isd_code'=> $isd_code,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
    
    
                    // } else {
                    //     return response()->json([
                    //         'success' => 0,
                    //         'message' => 'This phone already exists for another user type'
                    //     ]);
                    // }
                } else { // if co phone no does not exists
                    /*insert into users table starts*/
                    $insertUser = User::create([
                        'name'=> $name,
                        'isd_code'=> $isd_code,
                        'phone'=> $phone,
                        'user_type'=> 3,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into users table ends*/
                    $user_id = $insertUser->id;
    
                    /*insert into co_master table starts*/
                    $insertCo = CarOwner::create([
                        'co_fname'=> $name,
                        'co_lname'=> '',
                        'isd_code'=> $isd_code,
                        'co_phone'=> $phone,
                        'user_id'=> $user_id,
                        'account_no'=> $account_no,
                        'dob'=> $dob,
                        'make'=> $request->has('make')?$request->make:null,
                        'keytag'=> $request->has('keytag')?$request->keytag:null,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into co_master table ends*/
                    $co_id = $insertCo->id;
    
                }
            }
            if($phone == '' && $email !='') {
                // echo "hello3";exit;
                $getCoUser = User::where('email', $email)->where('user_type', 3)->first();
                if(!empty($getCoUser)) { // if co email already exists
                    // if($getCoUser->user_type == 3) { // if co email already exists for CO (user_type=3) then update the user
                        $co_user_id = $getCoUser->id;
                        $co = CarOwner::where('user_id', $co_user_id)->select('id')->first();
                        $co_id = $co->id;
                        $user_id = $co_user_id;
                        $userDetails = User::findOrFail($user_id);
                        $co->update([
                            // 'co_fname'=> $name,
                            // 'co_lname'=> '',
                            // 'isd_code'=> $isd_code,
                            'dob'=> $dob,
                            'make'=> $request->has('make')?$request->make:null,
                            'keytag'=> $request->has('keytag')?$request->keytag:null,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
                        $userDetails->update([
                            // 'name'=> $name,
                            // 'isd_code'=> $isd_code,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
    
    
                    // } else {
                    //     return response()->json([
                    //         'success' => 0,
                    //         'message' => 'This email already exists for another user type'
                    //     ]);
                    // }
                } else { // if co email does not exists
                    /*insert into users table starts*/
                    $insertUser = User::create([
                        'name'=> $name,
                        'email'=> $email,
                        // 'isd_code'=> $isd_code,
                        'user_type'=> 3,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into users table ends*/
                    $user_id = $insertUser->id;
    
                    /*insert into co_master table starts*/
                    $insertCo = CarOwner::create([
                        'co_fname'=> $name,
                        'co_lname'=> '',
                        'email'=> $email,
                        // 'isd_code'=> $isd_code,
                        'user_id'=> $user_id,
                        'account_no'=> $account_no,
                        'dob'=> $dob,
                        'make'=> $request->has('make')?$request->make:null,
                        'keytag'=> $request->has('keytag')?$request->keytag:null,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into co_master table ends*/
                    $co_id = $insertCo->id;
    
                }
            }
            if($phone != '' && $email !='') {
                // echo "hello4";exit;
                $getCoUser = User::where('email', $email)->where('user_type', 3)->first();
                if(!empty($getCoUser)) { // if co email already exists
                    $co_user_id = $getCoUser->id;
                    $co = CarOwner::where('user_id', $co_user_id)->select('id', 'email')->first();
                    $co_id = $co->id;
                    $user_id = $co_user_id;
                    $userDetails = User::findOrFail($user_id);
                    $co->update([
                        // 'co_fname'=> $name,
                        // 'co_lname'=> '',
                        // 'email'=> $email,
                        // 'isd_code'=> $isd_code,
                        // 'make'=> $request->has('make')?$request->make:null,
                        // 'keytag'=> $request->has('keytag')?$request->keytag:null,
                        // 'phone'=> $phone,
                        'dob'=> $dob,
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);
                    /*$userDetails->update([
                        // 'name'=> $name,
                        // 'email'=> $email,
                        // 'isd_code'=> $isd_code,
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);*/
                    /*if($co->co_phone == '') {
                        $co->update([
                            'isd_code'=> $isd_code,
                            'co_phone'=> $phone,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
                        $userDetails->update([
                            'isd_code'=> $isd_code,
                            'phone'=> $phone,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);    
                    }*/
                } else {
                    /*insert into users table starts*/
                    $insertUser = User::create([
                        'name'=> $name,
                        'email'=> $email,
                        'isd_code'=> $isd_code,
                        'phone'=> $phone,
                        'user_type'=> 3,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into users table ends*/
                    $user_id = $insertUser->id;
                    /*insert into co_master table starts*/
                    $insertCo = CarOwner::create([
                        'co_fname'=> $name,
                        'co_lname'=> '',
                        'isd_code'=> $isd_code,
                        'co_phone'=> $phone,
                        'email'=> $email,
                        'user_id'=> $user_id,
                        'account_no'=> $account_no,
                        'dob'=> $dob,
                        'make'=> $request->has('make')?$request->make:null,
                        'keytag'=> $request->has('keytag')?$request->keytag:null,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into co_master table ends*/
                    $co_id = $insertCo->id;

                }
            }
        } else {
            //do nothing
            $co_id = $getCheckCar->co_id;
            $co_user = CarOwner::findOrFail($co_id);
            $user_id = $co_user->user_id;
            $userDetails = User::findOrFail($user_id);
            // if($co_user->email == '') {
            //     $co_user->update([
            //         'email'=> $email,
            //         'updated_at'=>$dt->toDayDateTimeString()
            //         ]);
            //     $userDetails->update([
            //         'email'=> $email,
            //         'updated_at'=>$dt->toDayDateTimeString()
            //         ]);    
            // }
            // if($co_user->co_phone == '') {
            //     $co_user->update([
            //         'isd_code'=> $isd_code,
            //         'co_phone'=> $phone,
            //         'updated_at'=>$dt->toDayDateTimeString()
            //         ]);
            //     $userDetails->update([
            //         'isd_code'=> $isd_code,
            //         'phone'=> $phone,
            //         'updated_at'=>$dt->toDayDateTimeString()
            //         ]);    
            // }
            
        }

        

        /*update co_car table starts*/
        $carDetails = CarOwnerCar::findOrFail($car_id);
        $carDetails->update([
            'co_id'=> $co_id,
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*update co_car table ends*/

        

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'co_id' => $co_id,
            'user_id' => $user_id,
            'is_free_wash' => $is_free_wash,
            'loyalty_name' => $loyalty_name,
            'loyalty_type' => $loyalty_type,
            'loyalty_type_text' => $loyalty_type_text,
            'no_of_paid_wash' => $no_of_paid_wash,
            'discount_value_in_percentage' => $discount_value_in_percentage
        ]);

    }


    public function scan(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'car_registration_no' => 'required',
                // 'car_model' => 'required',
                'body_type_id' => 'required',
                // 'control_no' => 'required',
                // 'make' => 'required',
                // 'engine_no' => 'required',
                // 'vin' => 'required',
                // 'license_expiry_date' => 'required',
            ],
            [
                // 'email.unique' => 'This email already exists.'
                'body_type_id.required' => 'Please select a body type'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }
        $car_registration_no = $request->input('car_registration_no');
        $car_model = $request->input('car_model');
        $body_type_id = $request->input('body_type_id');
        $control_no = $request->input('control_no');
        $make = $request->input('make');
        $keytag = $request->input('keytag');
        $engine_no = $request->input('engine_no');
        $vin = $request->input('vin');
        $license_expiry_date = $request->input('license_expiry_date');
        $vehicle_color = $request->input('vehicle_color');
        $co_id = 0;
        $dt = Carbon::now();

        if($body_type_id != '') {
            $getBodyType = BodyType::where('id', $body_type_id)->select('name')->first();
            $body_type = $getBodyType->name;
        } else {
            $body_type_id = 0;
            $body_type = '';
        }
        if($license_expiry_date != '') {
            $license_expiry_date = date("Y-m-d", strtotime($license_expiry_date));
        }

        $checkCar = CarOwnerCar::where('car_registration_no', $car_registration_no)->first();
        // print_r($checkCar->toArray());exit;
        $getCo = [];
        if(!empty($checkCar)) {
            if($checkCar->body_type_id == 0){
                $checkCar->update([
                    'body_type'=> $body_type,
                    'body_type_id'=> $body_type_id,
                    'updated_at'=>$dt->toDayDateTimeString()
                    ]);    
            }
            $checkCar->update([
                'vehicle_color'=> $vehicle_color,
                'car_model'=> $car_model,
                'updated_at'=>$dt->toDayDateTimeString()
                ]);
            $image = '';
            if ($request->hasFile('image')) {
                $image = time() . '.' . $request->file('image')->extension();
                $path = $request->file('image')->move(public_path('images/CarOwner/'), $image);
            }
            $checkCar->update([
                    'image'=> $image,
                    'updated_at'=>$dt->toDayDateTimeString()
                    ]); 
            if($request->hasFile('image')){
                $image1 = asset('images/CarOwner') . '/' . $image;
            } else {
                $image1 = '';
            }
            $getWashDetail = WashDetail::where('car_id', $checkCar->id)->select('driver_name', 'driver_isd_code', 'driver_phone', 'driver_email')->orderBy('id', 'desc')->take(1)->get();
            // print_r($getWashDetail->toArray());exit;
            // dd($getWashDetail);
            if(!empty($getWashDetail[0])) {
                $getCoArray = [
                    'id' => 0,
                    'account_no' => '',
                    'co_fname' => $getWashDetail[0]->driver_name,
                    'co_lname' => '',
                    'co_phone' => $getWashDetail[0]->driver_phone,
                    'email' => $getWashDetail[0]->driver_email,
                    'co_image' => '',
                    'is_active' => '',
                    'created_at' => '',
                    'updated_at' => '',
                    'user_id' => 0,
                    'make' => '',
                    'keytag' => '',
                    'isd_code' => $getWashDetail[0]->driver_isd_code,
                ];
                $getCo = $getCoArray;
            } else {
                $getCo = CarOwner::where('id', $checkCar->co_id)->first();
            }
            // $getCo = CarOwner::where('id', $checkCar->co_id)->first();
            return response()->json([
                'success' => 1,
                'message' => 'Car registration no. already exists.',
                'car_id' => $checkCar->id,
                'car_registration_no'=> $car_registration_no,
                'car_details' => $checkCar,
                'car_owner_details' => $getCo,
                'image'=>$image1
            ]);
        } else {


            $image = '';

            if ($request->hasFile('image')) {
                $image = time() . '.' . $request->file('image')->extension();
                $path = $request->file('image')->move(public_path('images/CarOwner/'), $image);
            }

            /*insert into co_car table starts*/

            $carData=[
                'co_id'=> $co_id,
                'car_model'=> $car_model,
                'car_registration_no'=> $car_registration_no,
                'body_type'=> $body_type,
                'body_type_id'=> $body_type_id,
                'control_no'=> $control_no,
                'make'=> $make,
                'keytag'=> $keytag,
                'engine_no'=> $engine_no,
                'vin'=> $vin,
                'license_expiry_date'=> $license_expiry_date,
                'created_at'=>$dt->toDayDateTimeString(),
                'updated_at'=>$dt->toDayDateTimeString(),
                'image'=>$image,
                'vehicle_color'=>$vehicle_color
            ];


            $insertCar = CarOwnerCar::create($carData);
            /*insert into co_car table ends*/
            $checkCar = CarOwnerCar::where('id', $insertCar->id)->first();
            if($checkCar->image != ''){
                $image = asset('images/CarOwner') . '/' . $checkCar->image;
            } else {
                $image = '';
            }
            return response()->json([
                'success' => 1,
                'message' => 'Success',
                'car_id' => $insertCar->id,
                'car_registration_no'=> $car_registration_no,
                'car_details' => $checkCar,
                'car_owner_details' => $getCo,
                'image'=>$image
            ]);
        }
    }

    public function addBodyType(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'body_type_name' => 'required|unique:body_types,name',
                'wash_type_id' => 'required',
                'price' => 'required',
                'duration' => 'required'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $body_type_name = $request->input('body_type_name');
        $wash_type_id = $request->input('wash_type_id');
        $price = $request->input('price');
        $duration = $request->input('duration');
        $dt = Carbon::now();

        /*insert into body_types table starts*/
        $data = BodyType::create([
            'name'=> $body_type_name,
            'status'=> 1,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into body_types table ends*/

        /*insert into wash_category_body_types table starts*/
        $body_type_id = $data->id;
        $dataCwo = WashCategoryBodyType::create([
            'wash_category_id'=> $wash_type_id,
            'body_type_id'=> $body_type_id,
            'price'=> $price,
            'duration'=> $duration,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into wash_category_body_types table ends*/

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'body_type_id' => $body_type_id
        ]);

    }


    public function updateDriverDetails(Request $request)
    {

        $validate = Validator::make(
            $request->all(),
            [
                'name' => 'required',
                'car_id' => 'required',
            ],
            [
                // 'email.unique' => 'This email already exists.'
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }

        $name = $request->input('name');
        $phone = $request->input('phone');
        $email = $request->input('email');
        $car_id = $request->input('car_id');
        $dt = Carbon::now();

        if($phone == '' && $email =='') {
            /*insert into users table starts*/
            $insertUser = User::create([
                'name'=> $name,
                'user_type'=> 3,
                'created_at'=>$dt->toDayDateTimeString(),
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
            $user_id = $insertUser->id;
            /*insert into co_master table starts*/
            $insertCo = CarOwner::create([
                'co_fname'=> $name,
                'user_id'=> $user_id,
                'created_at'=>$dt->toDayDateTimeString(),
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
            /*insert into co_master table ends*/
            $co_id = $insertCo->id;
        }
        if($phone != '' && $email =='') {
            $getCoUser = User::where('phone', $phone)->first();
            if(!empty($getCoUser)) { // if co phone no already exists
                if($getCoUser->user_type == 3) { // if co phone no already exists for CO (user_type=3) then update the user
                    $co_user_id = $getCoUser->id;
                    $co = CarOwner::where('user_id', $co_user_id)->select('id')->first();
                    $co_id = $co->id;
                    $user_id = $co_user_id;
                    $userDetails = User::findOrFail($user_id);
                    $co->update([
                        'co_fname'=> $name,
                        'co_lname'=> '',
                        // 'phone'=> $phone,
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);
                    $userDetails->update([
                        'name'=> $name,
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);


                } else {
                    return response()->json([
                        'success' => 0,
                        'message' => 'This phone already exists for another user type'
                    ]);
                }
            } else { // if co phone no does not exists
                /*insert into users table starts*/
                $insertUser = User::create([
                    'name'=> $name,
                    'phone'=> $phone,
                    'user_type'=> 3,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                /*insert into users table ends*/
                $user_id = $insertUser->id;

                /*insert into co_master table starts*/
                $insertCo = CarOwner::create([
                    'co_fname'=> $name,
                    'co_phone'=> $phone,
                    'user_id'=> $user_id,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                /*insert into co_master table ends*/
                $co_id = $insertCo->id;

            }
        }
        if($phone == '' && $email !='') {
            $getCoUser = User::where('email', $email)->first();
            if(!empty($getCoUser)) { // if co email already exists
                if($getCoUser->user_type == 3) { // if co email already exists for CO (user_type=3) then update the user
                    $co_user_id = $getCoUser->id;
                    $co = CarOwner::where('user_id', $co_user_id)->select('id')->first();
                    $co_id = $co->id;
                    $user_id = $co_user_id;
                    $userDetails = User::findOrFail($user_id);
                    $co->update([
                        'co_fname'=> $name,
                        'co_lname'=> '',
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);
                    $userDetails->update([
                        'name'=> $name,
                        'updated_at'=>$dt->toDayDateTimeString()
                        ]);


                } else {
                    return response()->json([
                        'success' => 0,
                        'message' => 'This email already exists for another user type'
                    ]);
                }
            } else { // if co email does not exists
                /*insert into users table starts*/
                $insertUser = User::create([
                    'name'=> $name,
                    'email'=> $email,
                    'user_type'=> 3,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                /*insert into users table ends*/
                $user_id = $insertUser->id;

                /*insert into co_master table starts*/
                $insertCo = CarOwner::create([
                    'co_fname'=> $name,
                    'email'=> $email,
                    'user_id'=> $user_id,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
                /*insert into co_master table ends*/
                $co_id = $insertCo->id;

            }
        }
        if($phone != '' && $email !='') {
            $getCoUser = User::where('phone', $phone)->first();
            // print_r($getCoUser->toArray());exit;
            if(!empty($getCoUser)) { // if co phone no already exists
                if($getCoUser->user_type == 3) { // if co phone no already exists for CO (user_type=3) then update the user
                    $co_user_id = $getCoUser->id;
                    $co = CarOwner::where('user_id', $co_user_id)->select('id')->first();
                    $co_id = $co->id;
                    $user_id = $co_user_id;
                    $emailCheck = User::where('email', $email)->where('id', '!=', $user_id)->first();
                    if(!empty($emailCheck)) {
                        return response()->json([
                            'success' => 0,
                            'message' => 'This email already exists for another user'
                        ]);
                    } else {
                        $userDetails = User::findOrFail($user_id);
                        $co->update([
                            'co_fname'=> $name,
                            'co_lname'=> '',
                            'email'=> $email,
                            // 'phone'=> $phone,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
                        $userDetails->update([
                            'name'=> $name,
                            'email'=> $email,
                            'updated_at'=>$dt->toDayDateTimeString()
                            ]);
                    }

                } else {
                    return response()->json([
                        'success' => 0,
                        'message' => 'This phone already exists for another user type'
                    ]);
                }
            } else { // if co phone no does not exists
                $emailCheck = User::where('email', $email)->first();
                if(!empty($emailCheck)) {
                    return response()->json([
                        'success' => 0,
                        'message' => 'This email already exists for another user'
                    ]);
                } else {
                    /*insert into users table starts*/
                    $insertUser = User::create([
                        'name'=> $name,
                        'email'=> $email,
                        'phone'=> $phone,
                        'user_type'=> 3,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into users table ends*/
                    $user_id = $insertUser->id;

                    /*insert into co_master table starts*/
                    $insertCo = CarOwner::create([
                        'co_fname'=> $name,
                        'co_phone'=> $phone,
                        'email'=> $email,
                        'user_id'=> $user_id,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);
                    /*insert into co_master table ends*/
                    $co_id = $insertCo->id;
                }

            }
        }

        /*update co_car table starts*/
        $carDetails = CarOwnerCar::findOrFail($car_id);
        $carDetails->update([
            'co_id'=> $co_id,
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*update co_car table ends*/

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'co_id' => $co_id,
            'user_id' => $user_id
        ]);

    }

    public function carsCompleted(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'cwo_id' => 'required',
                // 'start_date' => 'required',
                // 'end_date' => 'required',
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        // $start_date = $request->input('start_date'); // format -> yyyy-mm-dd, today or less than today
        // $end_date = $request->input('end_date'); // format -> yyyy-mm-dd, today
        $today = date('Y-m-d');
        $start_date = date('Y-m-d', strtotime('-2 days', strtotime($today)));
        $end_date = $today;
        // echo $start_date . ' - ' . $end_date;exit;

        $getCurrency = getCurencyOfCwo($cwo_id);

        /***************** queue starts *********************/
        $carsCompleted = WashDetail::select('wash_details.id', 'wash_details.car_id', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status', 'wash_details.payment_status', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'co_car.car_registration_no', 'wash_details.otp', 'wash_details.payment_details', 'wash_details.amount', 'wash_details.total_amount', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.is_free_wash', 'wash_details.wash_type_id', 'wash_details.keys_collected', 'wash_details.image', 'wash_details.discount', 'co_car.make', 'co_car.vehicle_color', 'co_car.car_model')
        ->join('co_car', 'co_car.id', '=', 'wash_details.car_id')
        ->join('co_master', 'co_master.id', '=', 'co_car.co_id')
        ->join('users', 'users.id', '=', 'co_master.user_id')
        ->join('cwo_master', 'cwo_master.id', '=', 'wash_details.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('wash_details.cwo_id', $cwo_id)
        // ->where('wash_details.status', '=', 3)
        ->where(function($query) {
            $query->where('wash_details.status', 3)
                ->orWhere('wash_details.status', 5);
        });
        if($start_date != '') {
            $carsCompleted->whereDate('wash_details.date_washed', '>=', $start_date);
        }
        if($end_date != '') {
            $carsCompleted->whereDate('wash_details.date_washed', '<=', $end_date);
        }
        // $carsCompleted = $carsCompleted->orderBy('co_car.id', 'desc');
        $carsCompleted = $carsCompleted->orderBy('wash_details.id', 'desc');
        $carsCompletedCount = $carsCompleted->count();
        $carsCompleted = $carsCompleted->get();


        // echo "<pre>";print_r($carsCompleted->toArray());exit;
        /***************** queue ends *********************/

        $dataDisplay=[];
        $dataArray = [];
        if($carsCompleted != '') {
            foreach ($carsCompleted as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['car_id'] = $d->car_id;
                if($d->driver_name != ''){
                    $dataDisplay['co_fname'] = $d->driver_name;
                    $dataDisplay['co_lname'] = '';
                } else {
                    $dataDisplay['co_fname'] = $d->co_fname;
                    $dataDisplay['co_lname'] = $d->co_lname;
                }
                if($d->driver_phone != ''){
                    $dataDisplay['isd_code'] = '+' . $d->driver_isd_code;
                    $dataDisplay['co_phone'] = $d->driver_phone;
                } else {
                    $dataDisplay['isd_code'] = '+' . $d->isd_code;
                    $dataDisplay['co_phone'] = $d->co_phone;
                }
                
                $dataDisplay['car_registration_no'] = $d->car_registration_no;
                $dataDisplay['make'] = $d->make;
                $dataDisplay['vehicle_color'] = $d->vehicle_color;
                if($d->vehicle_color == '') {
                    $dataDisplay['vehicle_color'] = '';
                }
                $dataDisplay['car_model'] = $d->car_model;
                if($d->car_model == '') {
                    $dataDisplay['car_model'] = '';
                }
                // $dataDisplay['wash_name'] = $d->wash_name;
                if($d->date_washed != '') {
                    $dataDisplay['date_washed']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['date_washed']  = '';
                }
                if($d->time_washed != '') {
                    $dataDisplay['time_washed']  = date("h:i a", strtotime($d->time_washed));
                } else {
                    $dataDisplay['time_washed']  = '';
                }
                $dataDisplay['payment_status_id'] = $d->payment_status;
                if($d->payment_status == 1) {
                    $dataDisplay['payment_status'] = 'Paid';
                } else {
                    $dataDisplay['payment_status'] = 'Not Paid';
                }
                $dataDisplay['status_id'] = $d->status;
                if($d->status == 0) {
                    $dataDisplay['status'] = 'Car in-queue';
                }
                if($d->status == 1) {
                    $dataDisplay['status'] = 'In Wash Bay';
                }
                if($d->status == 2) {
                    $dataDisplay['status'] = 'Finishing Bay';
                }
                if($d->status == 3) {
                    $dataDisplay['status'] = 'Car Wash Completed';
                }
                if($d->status == 4) {
                    $dataDisplay['status'] = 'Cancelled';
                }
                if($d->status == 5){
                    $dataDisplay['status'] = 'Keys Collected';    
                }
                if($d->is_free_wash == 1) {
                    $dataDisplay['is_free_wash'] = true;
                }
                if($d->is_free_wash == 0) {
                    $dataDisplay['is_free_wash'] = false;
                }
                $dataDisplay['otp'] = $d->otp;
                $dataDisplay['payment_mode'] = $d->payment_details;
                $dataDisplay['wash_amount'] = $d->amount;
                $dataDisplay['total_amount'] = $d->total_amount;
                $dataDisplay['discount_value_in_percentage'] = $d->discount;
                if($d->keys_collected != ''){
                    $dataDisplay['keys_collected']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['keys_collected'] = '';
                } 

                // if($d->image != ''){
                //     $dataDisplay['image'] = asset('images/wash') . '/' . $d->image;
                // } else {
                //     // $cwo_logo = '';
                //     $dataDisplay['image'] = '';
                // }
                if($d->image != '') {
                    $dataDisplay['image'] = $d->image;
                } else {
                    $dataDisplay['image'] = '';
                }
                if($dataDisplay['image'] == "null"){
                    $dataDisplay['image'] = '';
                }
                

                $optionalExtra = OptionalExtra::where('wash_id', $d->id)->select('optional_extra', 'price')->orderBy('id', 'asc')->get();
                $dataDisplay['extras'] = $optionalExtra;

                $cleaners = CleanerWash::select('cleaner_masters.name', 'cleaner_washes.description as cleaning_description')
                            ->join('cleaner_masters', 'cleaner_masters.id', '=', 'cleaner_washes.cleaner_id')
                            ->where('cleaner_washes.wash_id', $d->id)
                            ->orderBy('cleaner_washes.id', 'asc')
                            ->get();
                $dataDisplay['cleaners'] = $cleaners;

                $wash_types = WashTypeToWash::select('wash_categories.wash_name', 'wash_type_to_washes.price')
                            ->join('wash_categories', 'wash_categories.id', '=', 'wash_type_to_washes.wash_type_id')
                            ->where('wash_type_to_washes.wash_id', $d->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
                $dataDisplay['wash_types'] = $wash_types;

                if($d->wash_type_id > 0){
                    $getWashType = WashCategory::where('id', $d->wash_type_id)->select('wash_name')->first();
                    if($getWashType != ''){
                        $dataDisplay['wash_name'] = $getWashType->wash_name;    
                    } else {
                        $dataDisplay['wash_name'] = '';
                    }
                } else {
                    $dataDisplay['wash_name'] = '';
                }

                $dataArray[] = $dataDisplay;
            }
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'carsCompletedCount' => $carsCompletedCount,
            'carsCompleted' => $dataArray
        ]);

    }

    public function carsCompleted_NewSplit(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'cwo_id' => 'required',
                // 'start_date' => 'required',
                // 'end_date' => 'required',
            ]
        );

        if($validate->fails()) {
            $errorString = implode(",", $validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $today = date('Y-m-d');
        $start_date = date('Y-m-d', strtotime('-2 days', strtotime($today)));
        $end_date = $today;
        // echo $start_date . ' - ' . $end_date;exit;

        $getCurrency = getCurencyOfCwo($cwo_id);

        /***************** queue starts *********************/
        $carsCompleted = WashDetail::select('wash_details.id', 'wash_details.car_id', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status', 'wash_details.payment_status', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'co_car.car_registration_no', 'wash_details.otp', 'wash_details.payment_details', 'wash_details.amount', 'wash_details.total_amount', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.is_free_wash', 'wash_details.wash_type_id', 'wash_details.keys_collected', 'wash_details.image', 'wash_details.discount', 'co_car.make', 'co_car.vehicle_color', 'co_car.car_model', 'wash_details.card_amount', 'wash_details.cash_amount')
        ->join('co_car', 'co_car.id', '=', 'wash_details.car_id')
        ->join('co_master', 'co_master.id', '=', 'co_car.co_id')
        ->join('users', 'users.id', '=', 'co_master.user_id')
        ->join('cwo_master', 'cwo_master.id', '=', 'wash_details.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('wash_details.cwo_id', $cwo_id)
        // ->where('wash_details.status', '=', 3)
        ->where(function($query) {
            $query->where('wash_details.status', 3)
                ->orWhere('wash_details.status', 5);
        });
        if($start_date != '') {
            $carsCompleted->whereDate('wash_details.date_washed', '>=', $start_date);
        }
        if($end_date != '') {
            $carsCompleted->whereDate('wash_details.date_washed', '<=', $end_date);
        }
        // $carsCompleted = $carsCompleted->orderBy('co_car.id', 'desc');
        $carsCompleted = $carsCompleted->orderBy('wash_details.id', 'desc');
        $carsCompletedCount = $carsCompleted->count();
        $carsCompleted = $carsCompleted->get();


        // echo "<pre>";print_r($carsCompleted->toArray());exit;
        /***************** queue ends *********************/

        $dataDisplay=[];
        $dataArray = [];
        if($carsCompleted != '') {
            foreach ($carsCompleted as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['car_id'] = $d->car_id;
                if($d->driver_name != ''){
                    $dataDisplay['co_fname'] = $d->driver_name;
                    $dataDisplay['co_lname'] = '';
                } else {
                    $dataDisplay['co_fname'] = $d->co_fname;
                    $dataDisplay['co_lname'] = $d->co_lname;
                }
                if($d->driver_phone != ''){
                    $dataDisplay['isd_code'] = '+' . $d->driver_isd_code;
                    $dataDisplay['co_phone'] = $d->driver_phone;
                } else {
                    $dataDisplay['isd_code'] = '+' . $d->isd_code;
                    $dataDisplay['co_phone'] = $d->co_phone;
                }
                
                $dataDisplay['car_registration_no'] = $d->car_registration_no;
                $dataDisplay['make'] = $d->make;
                $dataDisplay['vehicle_color'] = $d->vehicle_color;
                if($d->vehicle_color == '') {
                    $dataDisplay['vehicle_color'] = '';
                }
                $dataDisplay['car_model'] = $d->car_model;
                if($d->car_model == '') {
                    $dataDisplay['car_model'] = '';
                }
                // $dataDisplay['wash_name'] = $d->wash_name;
                if($d->date_washed != '') {
                    $dataDisplay['date_washed']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['date_washed']  = '';
                }
                if($d->time_washed != '') {
                    $dataDisplay['time_washed']  = date("h:i a", strtotime($d->time_washed));
                } else {
                    $dataDisplay['time_washed']  = '';
                }
                $dataDisplay['payment_status_id'] = $d->payment_status;
                if($d->payment_status == 1) {
                    $dataDisplay['payment_status'] = 'Paid';
                } else {
                    $dataDisplay['payment_status'] = 'Not Paid';
                }
                $dataDisplay['status_id'] = $d->status;
                if($d->status == 0) {
                    $dataDisplay['status'] = 'Car in-queue';
                }
                if($d->status == 1) {
                    $dataDisplay['status'] = 'In Wash Bay';
                }
                if($d->status == 2) {
                    $dataDisplay['status'] = 'Finishing Bay';
                }
                if($d->status == 3) {
                    $dataDisplay['status'] = 'Car Wash Completed';
                }
                if($d->status == 4) {
                    $dataDisplay['status'] = 'Cancelled';
                }
                if($d->status == 5){
                    $dataDisplay['status'] = 'Keys Collected';    
                }
                if($d->is_free_wash == 1) {
                    $dataDisplay['is_free_wash'] = true;
                }
                if($d->is_free_wash == 0) {
                    $dataDisplay['is_free_wash'] = false;
                }
                $dataDisplay['otp'] = $d->otp;
                $dataDisplay['payment_mode'] = $d->payment_details;
                $dataDisplay['wash_amount'] = $d->amount;
                $dataDisplay['total_amount'] = $d->total_amount;
                $dataDisplay['card_amount'] = $d->card_amount;
                $dataDisplay['cash_amount'] = $d->cash_amount;
                $dataDisplay['discount_value_in_percentage'] = $d->discount;
                if($d->keys_collected != ''){
                    $dataDisplay['keys_collected']  = date("d/m/Y", strtotime($d->date_washed));
                } else {
                    $dataDisplay['keys_collected'] = '';
                } 

                // if($d->image != ''){
                //     $dataDisplay['image'] = asset('images/wash') . '/' . $d->image;
                // } else {
                //     // $cwo_logo = '';
                //     $dataDisplay['image'] = '';
                // }
                if($d->image != '') {
                    $dataDisplay['image'] = $d->image;
                } else {
                    $dataDisplay['image'] = '';
                }
                if($dataDisplay['image'] == "null"){
                    $dataDisplay['image'] = '';
                }
                

                $optionalExtra = OptionalExtra::where('wash_id', $d->id)->select('optional_extra', 'price')->orderBy('id', 'asc')->get();
                $dataDisplay['extras'] = $optionalExtra;

                $cleaners = CleanerWash::select('cleaner_masters.name', 'cleaner_washes.description as cleaning_description')
                            ->join('cleaner_masters', 'cleaner_masters.id', '=', 'cleaner_washes.cleaner_id')
                            ->where('cleaner_washes.wash_id', $d->id)
                            ->orderBy('cleaner_washes.id', 'asc')
                            ->get();
                $dataDisplay['cleaners'] = $cleaners;

                $wash_types = WashTypeToWash::select('wash_categories.wash_name', 'wash_type_to_washes.price')
                            ->join('wash_categories', 'wash_categories.id', '=', 'wash_type_to_washes.wash_type_id')
                            ->where('wash_type_to_washes.wash_id', $d->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
                $dataDisplay['wash_types'] = $wash_types;

                if($d->wash_type_id > 0){
                    $getWashType = WashCategory::where('id', $d->wash_type_id)->select('wash_name')->first();
                    if($getWashType != ''){
                        $dataDisplay['wash_name'] = $getWashType->wash_name;    
                    } else {
                        $dataDisplay['wash_name'] = '';
                    }
                } else {
                    $dataDisplay['wash_name'] = '';
                }

                $dataArray[] = $dataDisplay;
            }
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'carsCompletedCount' => $carsCompletedCount,
            'carsCompleted' => $dataArray
        ]);

    }


}
