<?php

namespace App\Http\Controllers\Api;

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

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

    public function index()
    {
    }

    public function list(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');

        $data = PreBooking::select('pre_bookings.id', 'pre_bookings.name', 'pre_bookings.isd_code', 'pre_bookings.phone_no', 'pre_bookings.date', 'pre_bookings.time', 'pre_bookings.date_time', 'pre_bookings.address', 'pre_bookings.body_type_id', 'pre_bookings.body_type', 'pre_bookings.amount', 'pre_bookings.total_amount', 'pre_bookings.status', 'pre_bookings.car_id', 'co_car.car_registration_no', 'pre_bookings.co_id', 'currency_masters.symbol', 'pre_booking_types.type', 'pre_bookings.discount', 'co_car.make', 'co_car.car_model', 'co_car.vehicle_color', 'pre_bookings.is_free_wash')
            ->join('co_car', 'co_car.id', '=', 'pre_bookings.car_id')
            ->join('co_master', 'co_master.id', '=', 'pre_bookings.co_id')
            ->join('pre_booking_types', 'pre_booking_types.id', '=', 'pre_bookings.prebooking_type')
            ->join('cwo_master', 'cwo_master.id', '=', 'pre_bookings.cwo_id')
            ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
            ->where('pre_bookings.cwo_id', $cwo_id);
        $data = $data->orderBy('pre_bookings.id', 'desc');
        $dataCount = $data->count();
        $data = $data->get(); 
        // echo "<pre>";print_r($data->toArray());exit;   

        $dataDisplay=[];
        $dataArray = [];
        if($data != '') {
            foreach ($data as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['prebooking_type'] = $d->type;
                $dataDisplay['name'] = $d->name;
                $dataDisplay['isd_code'] = $d->isd_code;
                $dataDisplay['phone_no'] = $d->phone_no;
                if($d->date != '') {
                    $dataDisplay['date']  = date("d/m/Y", strtotime($d->date));
                } else {
                    $dataDisplay['date']  = '';
                }
                if($d->time != '') {
                    $dataDisplay['time']  = date("h:i a", strtotime($d->time));
                } else {
                    $dataDisplay['time']  = '';
                }
                $dataDisplay['date_time'] = $d->date_time;
                $dataDisplay['address'] = $d->address;
                $dataDisplay['body_type_id'] = $d->body_type_id;
                $dataDisplay['body_type'] = $d->body_type;
                $dataDisplay['amount'] = $d->amount;
                $dataDisplay['total_amount'] = $d->total_amount;
                $dataDisplay['discount_value_in_percentage'] = $d->discount;
                if($d->is_free_wash == 1) {
                    $dataDisplay['is_free_wash'] = true;
                }
                if($d->is_free_wash == 0) {
                    $dataDisplay['is_free_wash'] = false;
                }
                $dataDisplay['status_id'] = $d->status;
                if($d->status == 1) {
                    $dataDisplay['status'] = 'Pending';
                }
                if($d->status == 2) {
                    $dataDisplay['status'] = 'Confirmed';
                }
                if($d->status == 3) {
                    $dataDisplay['status'] = 'Declined';
                }
                if($d->status == 4) {
                    $dataDisplay['status'] = 'Wash Started';
                }
                $dataDisplay['car_id'] = $d->car_id;
                $dataDisplay['car_registration_no'] = $d->car_registration_no;
                $dataDisplay['make'] = $d->make;
                $dataDisplay['car_model'] = $d->car_model;
                $dataDisplay['vehicle_color'] = $d->vehicle_color;
                $dataDisplay['co_id'] = $d->co_id;
                if($d->symbol != '') {
                    $dataDisplay['currency'] = $d->symbol;
                } else {
                    $dataDisplay['currency'] = 'R';
                }

                $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.prebooking_id', $d->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
                $dataDisplay['wash_types'] = $wash_types;

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

                $dataArray[] = $dataDisplay;
            }
        }
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'preBookingCount' => $dataCount,
            'preBookingList' => $dataArray
        ]);
    }

    public function preBookingAllStatus(Request $request){
        $preBookingAllStatus = [
            [
                'id' => 2,
                'name' => 'Confirm',
            ],
            [
                'id' => 3,
                'name' => 'Decline',
            ],
            [
                'id' => 4,
                'name' => 'Start Wash',
            ]

        ];

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

    public function preBookingChangeStatus(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'prebooking_id' => 'required',
                'status' => 'required',
            ]
        );

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

        $prebooking_id = $request->input('prebooking_id');
        $status = $request->input('status');

        $record = PreBooking::find($prebooking_id);
        $record->status = $status;
        $record->save();

        $getCwo = CarWashOwner::where('id', $record->cwo_id)->select('cwo_company')->first();
        $cwo_company = $getCwo->cwo_company;
        $getCar = CarOwnerCar::where('id', $record->car_id)->select('car_registration_no')->first();
        $car_registration_no = $getCar->car_registration_no;
        $getCo = CarOwner::where('id', $record->co_id)->select('co_fname', 'co_lname', 'email', 'fcm_token')->first();
        if($record->name != '') {
            $recipient_name = $record->name;
        } else {
            $recipient_name = $getCo->co_fname . ' ' . $getCo->co_lname;
        }
        $email = $getCo->email;
        $fcm_token = $getCo->fcm_token;

        if($status == 2) {
            $status_text = 'confirmed';
        }
        if($status == 3) {
            $status_text = 'declined';
        }

        /*send mail starts*/
        if($email != ''){
            $recipient_email = $email;
            // echo $recipient_email;exit;
            $subject = 'Autoclick Car Pre Booking ' . ucfirst($status_text);
            $message_body = '<html>
                                <head>
                                <title>Autoclick Car Pre Booking ' . ucfirst($status_text) . '</title>
                                </head>
                                <body>
                                    <p>Hi ' . $recipient_name . '</p>
                                    <p>Pre booking for your car ' . $car_registration_no . ' has been ' . $status_text . ' at  ' . $cwo_company . '. Thank you!</p>
                                    <p>Regards</p>
                                    <p>AUTOCLICK</p>
                                    <p><a href="www.autoclickcarwashapp.com">www.autoclickcarwashapp.com</a> | <a href="mailto:info@autoclick.co.za">info@autoclick.co.za</a></p>
                                </body>
                            </html>';
            // echo $message_body;exit;
            $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
            // print_r($email);exit;
        }
        /*send mail ends*/

        /*push starts*/
        // $notification_title = 'Autoclick Car Pre Booking ' . ucfirst($status_text);
        // $notification_body = 'Pre booking for your car ' . $car_registration_no . ' has been ' . $status_text . ' at ' . $cwo_company;
        // // echo $notification_title . ' ->' . $notification_body;exit;
        // $data=array("request_type" => "notification");
        // sendFcmNotification($fcm_token, $notification_title, $notification_body, $data);
        /*push ends*/

        return response()->json([
            'success' => 1,
            'message' => 'Pre booking has been ' . $status_text . ' successfully'
        ]);
    }

    public function detail(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'prebooking_id' => 'required'
            ]
        );

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

        $prebooking_id = $request->input('prebooking_id');

        $data = PreBooking::select('pre_bookings.id', 'pre_bookings.name', 'pre_bookings.isd_code', 'pre_bookings.phone_no', 'pre_bookings.date', 'pre_bookings.time', 'pre_bookings.date_time', 'pre_bookings.address', 'pre_bookings.body_type_id', 'pre_bookings.body_type', 'pre_bookings.amount', 'pre_bookings.total_amount', 'pre_bookings.status', 'pre_bookings.car_id', 'co_car.car_registration_no', 'pre_bookings.co_id', 'currency_masters.symbol', 'pre_booking_types.type', 'pre_bookings.discount', 'co_car.make', 'co_car.car_model', 'co_car.vehicle_color', 'pre_bookings.is_free_wash')
            ->join('co_car', 'co_car.id', '=', 'pre_bookings.car_id')
            ->join('co_master', 'co_master.id', '=', 'pre_bookings.co_id')
            ->join('pre_booking_types', 'pre_booking_types.id', '=', 'pre_bookings.prebooking_type')
            ->join('cwo_master', 'cwo_master.id', '=', 'pre_bookings.cwo_id')
            ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
            ->where('pre_bookings.id', $prebooking_id)
            ->first();
        // echo "<pre>";print_r($data->toArray());exit;   

        $dataDisplay=[];
        if($data != '') {
            $dataDisplay['id'] = $data->id;
            $dataDisplay['prebooking_type'] = $data->type;
            $dataDisplay['name'] = $data->name;
            $dataDisplay['isd_code'] = $data->isd_code;
            $dataDisplay['phone_no'] = $data->phone_no;
            if($data->date != '') {
                $dataDisplay['date']  = date("d/m/Y", strtotime($data->date));
            } else {
                $dataDisplay['date']  = '';
            }
            if($data->time != '') {
                $dataDisplay['time']  = date("h:i a", strtotime($data->time));
            } else {
                $dataDisplay['time']  = '';
            }
            $dataDisplay['date_time'] = $data->date_time;
            $dataDisplay['address'] = $data->address;
            $dataDisplay['body_type_id'] = $data->body_type_id;
            $dataDisplay['body_type'] = $data->body_type;
            $dataDisplay['amount'] = $data->amount;
            $dataDisplay['total_amount'] = $data->total_amount;
            $dataDisplay['discount_value_in_percentage'] = $data->discount;
            if($data->is_free_wash == 1) {
                $dataDisplay['is_free_wash'] = true;
            }
            if($data->is_free_wash == 0) {
                $dataDisplay['is_free_wash'] = false;
            }
            $dataDisplay['status_id'] = $data->status;
            if($data->status == 1) {
                $dataDisplay['status'] = 'Pending';
            }
            if($data->status == 2) {
                $dataDisplay['status'] = 'Confirmed';
            }
            if($data->status == 3) {
                $dataDisplay['status'] = 'Declined';
            }
            if($data->status == 4) {
                $dataDisplay['status'] = 'Wash Started';
            }
            $dataDisplay['car_id'] = $data->car_id;
            $dataDisplay['car_registration_no'] = $data->car_registration_no;
            $dataDisplay['make'] = $data->make;
            $dataDisplay['car_model'] = $data->car_model;
            $dataDisplay['vehicle_color'] = $data->vehicle_color;
            $dataDisplay['co_id'] = $data->co_id;
            if($data->symbol != '') {
                $dataDisplay['currency'] = $data->symbol;
            } else {
                $dataDisplay['currency'] = 'R';
            }
            $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.prebooking_id', $data->id)
                            ->orderBy('wash_type_to_washes.id', 'asc')
                            ->get();
            $dataDisplay['wash_types'] = $wash_types;

            $optionalExtra = OptionalExtra::where('prebooking_id', $data->id)->select('optional_extra', 'price')->orderBy('id', 'asc')->get();
            $dataDisplay['extras'] = $optionalExtra;
        }
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'detail' => $dataDisplay
        ]);
    }

    public function accept(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'prebooking_id' => 'required'
            ]
        );

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

        $prebooking_id = $request->input('prebooking_id');

        $record = PreBooking::find($prebooking_id);
        $record->status = 2;
        $record->save();

        $getCwo = CarWashOwner::where('id', $record->cwo_id)->select('cwo_company')->first();
        $cwo_company = $getCwo->cwo_company;
        $getCar = CarOwnerCar::where('id', $record->car_id)->select('car_registration_no')->first();
        $car_registration_no = $getCar->car_registration_no;
        $getCo = CarOwner::where('id', $record->co_id)->select('co_fname', 'co_lname', 'email', 'fcm_token')->first();
        if($record->name != '') {
            $recipient_name = $record->name;
        } else {
            $recipient_name = $getCo->co_fname . ' ' . $getCo->co_lname;
        }
        $email = $getCo->email;
        $fcm_token = $getCo->fcm_token;

        // $status_text = 'confirmed';

        /*send mail starts*/
        if($email != ''){
            $recipient_email = $email;
            // echo $recipient_email;exit;
            $subject = 'Autoclick Car Pre Booking Confirmed';
            $message_body = '<html>
                                <head>
                                <title>Autoclick Car Pre Booking Confirmed</title>
                                </head>
                                <body>
                                    <p>Hi ' . $recipient_name . '</p>
                                    <p>Pre booking for your car ' . $car_registration_no . ' has been confirmed at  ' . $cwo_company . '. Thank you!</p>
                                    <p>Regards</p>
                                    <p>AUTOCLICK</p>
                                    <p><a href="www.autoclickcarwashapp.com">www.autoclickcarwashapp.com</a> | <a href="mailto:info@autoclick.co.za">info@autoclick.co.za</a></p>
                                </body>
                            </html>';
            // echo $message_body;exit;
            $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
            // print_r($email);exit;
        }
        /*send mail ends*/

        /*push starts*/
        $notification_title = 'Autoclick Car Pre Booking Confirmed';
        // $notification_body = 'Pre booking for your car ' . $car_registration_no . ' has been confirmed at ' . $cwo_company;
        // // echo $notification_title . ' ->' . $notification_body;exit;
        // $data=array("request_type" => "notification");
        // sendFcmNotification($fcm_token, $notification_title, $notification_body, $data);
        /*push ends*/

        return response()->json([
            'success' => 1,
            'message' => 'Pre booking has been confirmed'
        ]);
    }

    public function decline(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'prebooking_id' => 'required',
                'code' => 'required'
            ]
        );

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

        $prebooking_id = $request->input('prebooking_id');
        $code = $request->input('code');

        $record = PreBooking::find($prebooking_id);

        $getPasscode = CwoPasscode::where('cwo_id', $record->cwo_id)->first();
        if($getPasscode != ''){
            if($getPasscode->code == $code){
                // $record = PreBooking::find($prebooking_id);
                $record->status = 3;
                $record->save();

                $getCwo = CarWashOwner::where('id', $record->cwo_id)->select('cwo_company')->first();
                $cwo_company = $getCwo->cwo_company;
                $getCar = CarOwnerCar::where('id', $record->car_id)->select('car_registration_no')->first();
                $car_registration_no = $getCar->car_registration_no;
                $getCo = CarOwner::where('id', $record->co_id)->select('co_fname', 'co_lname', 'email', 'fcm_token')->first();
                if($record->name != '') {
                    $recipient_name = $record->name;
                } else {
                    $recipient_name = $getCo->co_fname . ' ' . $getCo->co_lname;
                }
                $email = $getCo->email;
                $fcm_token = $getCo->fcm_token;

                /*send mail starts*/
                if($email != ''){
                    $recipient_email = $email;
                    // echo $recipient_email;exit;
                    $subject = 'Autoclick Car Pre Booking Declined';
                    $message_body = '<html>
                                        <head>
                                        <title>Autoclick Car Pre Booking Declined</title>
                                        </head>
                                        <body>
                                            <p>Hi ' . $recipient_name . '</p>
                                            <p>Pre booking for your car ' . $car_registration_no . ' has been declined at  ' . $cwo_company . '. Thank you!</p>
                                            <p>Regards</p>
                                            <p>AUTOCLICK</p>
                                            <p><a href="www.autoclickcarwashapp.com">www.autoclickcarwashapp.com</a> | <a href="mailto:info@autoclick.co.za">info@autoclick.co.za</a></p>
                                        </body>
                                    </html>';
                    // echo $message_body;exit;
                    $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
                    // print_r($email);exit;
                }
                /*send mail ends*/

                /*push starts*/
                // $notification_title = 'Autoclick Car Pre Booking Declined';
                // $notification_body = 'Pre booking for your car ' . $car_registration_no . ' has been confirmed at ' . $cwo_company;
                // // echo $notification_title . ' ->' . $notification_body;exit;
                // $data=array("request_type" => "notification");
                // sendFcmNotification($fcm_token, $notification_title, $notification_body, $data);
                /*push ends*/

                return response()->json([
                    'success' => 1,
                    'message' => 'Pre booking has been declined.'
                ]);
            } else {
                return response()->json([
                    'success' => 0,
                    'message' => 'Passcode to decline pre-booking did not match.',
                ]); 
            }
        } else {
            return response()->json([
                'success' => 0,
                'message' => 'Passcode to decline pre-booking did not match.',
            ]);    
        }
    }

    public function start(Request $request)
    {
        $validate = Validator::make(
            $request->all(),
            [
                'prebooking_id' => 'required'
            ]
        );

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

        $prebooking_id = $request->input('prebooking_id');
        $record = PreBooking::find($prebooking_id);

        $co_id = $record->co_id;
        $name = $record->name;
        $isd_code = $record->isd_code;
        $phone_no = $record->phone_no;
        $dob = $record->dob;
        $car_id = $record->car_id;
        $body_type_id = $record->body_type_id;
        $body_type = $record->body_type;
        $cwo_id = $record->cwo_id;
        $amount = $record->amount;
        $total_amount = $record->total_amount;
        $discount = $record->discount;
        $is_free_wash = $record->is_free_wash;
        $entry_date = date('Y-m-d');
        $today_date = date('Y-m-d'); 
        $otp = generateNumericOTP(4);
        $getWashCount = WashDetail::select('wash_count')->where('cwo_id', $cwo_id)->orderBy('id', 'desc')->take(1)->get();
        // echo $getWashCount->count();exit;
        // print_r($getWashCount->toarray());exit;
        if ($getWashCount->count() > 0){
            $wash_count = $getWashCount[0]->wash_count + 1;
        } else {
            $wash_count = 1;
        }
        // echo $wash_count;exit;
        $dt = Carbon::now(); 

        $getCwo = CarWashOwner::where('id', $cwo_id)->select('cwo_name', 'cwo_company', 'cwo_isd', 'cwo_phone')->first();
        $cwo_name = $getCwo->cwo_name;
        $cwo_company = $getCwo->cwo_company;
        $cwo_contact_no = '+' . $getCwo->cwo_isd . $getCwo->cwo_phone;

        $getCar = CarOwnerCar::where('id', $car_id)->select('car_registration_no')->first();
        $car_registration_no = $getCar->car_registration_no;
        $getCo = CarOwner::where('id', $co_id)->select('co_fname', 'co_lname', 'email','isd_code','co_phone', 'fcm_token')->first();
        $co_fname = $getCo->co_fname;
        $co_lname = $getCo->co_lname;
        $co_email = $getCo->email;
        $co_isd_code = $getCo->isd_code;
        $co_phone = $getCo->co_phone;

        /*insert into wash_details table starts*/
        $insertCarWash = WashDetail::create([
            'prebooking_id'=> $prebooking_id,
            'car_id'=> $car_id,
            'entry_date'=> $entry_date,
            'entry_time'=> date('H:i:s'),
            'body_type'=> $body_type,
            'body_type_id'=> $body_type_id,
            'amount'=> $amount,
            'total_amount'=> $total_amount,
            'status'=> 0,
            'payment_status'=> 0,
            'otp'=> $otp,
            // 'image'=> $image,
            'driver_name'=> $name,
            'driver_isd_code'=> $isd_code,
            'driver_phone'=> $phone_no,
            'driver_email'=> $co_email,
            'dob'=> $dob,
            'cwo_id'=> $cwo_id,
            'is_free_wash'=> $is_free_wash,
            'wash_count'=> $wash_count,
            'discount'=> $discount,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into wash_details table ends*/     
        $wash_id = $insertCarWash->id;  

        /*update wash_status_update_datetime starts*/
        $carRecordDetails = CarOwnerCar::findorfail($car_id);
        $carRecordDetails->wash_status_update_datetime = date('Y-m-d H:i:s');
        $carRecordDetails->save();
        /*update wash_status_update_datetime ends*/

        $record_wash = WashDetail::find($wash_id);
        /*update is_loyalty_utilized starts*/
        if($is_free_wash == 1){
            $record_wash->is_loyalty_utilized = 1;
            $record_wash->save();
            $getLoyalty = Loyalty::where('cwo_id', $cwo_id)->where('is_active', 1)->first();
            $no_of_paid_wash = $getLoyalty->no_of_paid_wash; 
            // $record_wash->is_loyalty_utilized = 1;
            $getPrevious = WashDetail::where('id', '<', $wash_id)
                                        ->where('cwo_id', $cwo_id)
                                        ->where('is_loyalty_utilized', 0)
                                        ->where(function($query) {
                                                $query->where('status', 3)
                                                    ->orWhere('status', 5);
                                            })
                                        ->take($no_of_paid_wash)
                                        ->orderBy('id', 'asc')
                                        ->get();
                                        // print_r($getPrevious->toArray());exit;
            if($getPrevious != ''){
                foreach($getPrevious as $d){
                    $record_prev = WashDetail::find($d->id);
                    $record_prev->is_loyalty_utilized = 1;
                    $record_prev->save();
                }
            }
        }
        /*update is_loyalty_utilized ends*/

        /*update wash_type_to_washes table starts*/
        DB::statement("UPDATE wash_type_to_washes SET wash_id = $wash_id where prebooking_id = $prebooking_id");
        /*update wash_type_to_washes table ends*/

        /*update optional_extras table starts*/
        DB::statement("UPDATE optional_extras SET wash_id = $wash_id where prebooking_id = $prebooking_id");
        /*update optional_extras table ends*/

        /*update cleaner_washes table starts*/
        // DB::statement("UPDATE cleaner_washes SET wash_id = $wash_id where prebooking_id = $prebooking_id");
        /*update cleaner_washes table ends*/

        /*update pre_bookings table starts*/
        $record->status = 4;
        $record->save();
        /*update pre_bookings table ends*/

        /*send mail starts*/
        // $recipient_name = $co_fname . ' ' . $co_lname;
        /*whatsApp driver name starts*/
        if($record_wash->driver_name != ''){
            $recipient_name = $record_wash->driver_name;   
        } else {
            $recipient_name = $co_fname . ' ' . $co_lname;
        }
        //  echo $recipient_name;exit;
        /*whatsApp driver name ends*/
        // $recipient_email = 'tamashree@karmicksolutions.com';
        if($record_wash->driver_email != ''){
            if($entry_date >= $today_date){
                // $recipient_email = $co_email;
                $recipient_email = $record_wash->driver_email;
                $subject = 'Autoclick  Car In-Queue';
                $message_body = '<html>
                                    <head>
                                    <title>Autoclick  Car In-Queue</title>
                                    </head>
                                    <body>
                                        <p>Hi ' . $recipient_name . '</p>
                                        <p>Your car ' . $car_registration_no . ' is in the queue at ' . $cwo_company . ' and it will not be long.</p>
                                        <p>Your OTP to collect is  ' . $otp . '</p>
                                        <p>Regards</p>
                                        <p>AUTOCLICK</p>
                                        <p><a href="www.autoclickcarwashapp.com">www.autoclickcarwashapp.com</a> | <a href="mailto:info@autoclick.co.za">info@autoclick.co.za</a></p>
                                    </body>
                                </html>';
                // $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
                // print_r($email);exit;
            }
        }
        /*send mail ends*/

        /*whatsApp starts*/
        if($record_wash->driver_phone != ''){
            $whats_message_body = array($recipient_name,$car_registration_no,$cwo_company,$otp,$cwo_contact_no);
            $phone = '+'.$record_wash->driver_isd_code.$record_wash->driver_phone;
            if($cwo_id == 151){
                if($entry_date >= $today_date){
                    // sendWhatsappInQueueOtp($phone,$whats_message_body);
                }
            }
        }
        /*whatsApp ends*/

        /*push starts*/
        /*$fcm_token = $getCo->fcm_token;
        // $fcm_token = "eL409rTNQ1CiR4Y5wOUGh7:APA91bGLnCrYpQPlqwPQa_TnCvkZrT-kkmRmTiXyNFDhu7sJ3los_EZXOji2jBTcAc3haxJbCFq0Yb1iYa8l_ifAtCnK36Ut5Lw_ctTB-WV5LUfNnO8IQZFG4BEuohI1MYJuYdt_lnPo";
        $notification_title = "Car in queue";
        $notification_body = 'Your car ' . $car_registration_no . ' is in queue in ' . $cwo_company . '. Collection OTP: ' . $otp;
        $data=array("request_type" => "notification");
        sendFcmNotification($fcm_token, $notification_title, $notification_body, $data);*/
        /*push ends*/

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

    }


}
