<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProfileUpdateRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

use Illuminate\Support\Facades\Log;
use App\Models\WashDetail;
use App\Models\CurrencyMaster;
use App\Models\CarWashOwner;
use App\Models\BodyType;
use App\Models\WashCategoryBodyType;
use App\Models\WashCategory;
use App\Models\CarOwnerCar;
use App\Models\CarOwner;
use App\Models\OptionalExtra;
use App\Models\CwoPasscode;
use App\Models\CleanerWash;
use App\Models\WashTypeToWash;
use App\Models\Loyalty;
use App\Models\OtherServiceReport; // to show total_amount including other products
use App\Models\PreBooking;
use App\Models\LoyaltyScore;
use App\Models\LoyaltyScoreUsageLog;
use App\Models\LoyaltyScoreAddLog;
use App\Models\WashCategoryLoyaltyPoint;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;

class ReceiptController extends Controller
{

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

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

        $wash_id = $request->input('wash_id');
        $encoded_wash_id = encodeC($wash_id);
        $record = WashDetail::find($wash_id);
        $getCwo = CarWashOwner::where('id', $record->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', $record->car_id)->select('co_id', 'car_registration_no')->first();
        $car_registration_no = $getCar->car_registration_no;
        // $getCo = CarOwner::where('id', $getCar->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;
        // if($record->driver_name != ''){
        //     $recipient_name = $record->driver_name;   
        // } else {
        //     $recipient_name = $co_fname . ' ' . $co_lname;
        // }
        /*send mail starts*/
        if($record->driver_email != ''){
            $recipient_name = $record->driver_name;
            $recipient_email = $record->driver_email;
            $subject = 'Autoclick Car Wash Receipt';
            $download_link = route('downloadreceipt') . '?wash_id=' . $encoded_wash_id;
            // echo $download_link;exit;
            $message_body = '<html>
                                <head>
                                <title>Autoclick Car Wash Receipt</title>
                                </head>
                                <body>
                                    <p>Hi ' . $recipient_name . '</p>
                                    <p>Your car ' . $car_registration_no . ' wash receipt in the company  ' . $cwo_company . ' can be downloaded from the following link.</p>
                                    <p><a href="' . $download_link . '" target="_blank">Click Here to Download Receipt</a></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);
        }
        /*send mail ends*/
        return response()->json([
            'success' => 1,
            'message' => 'Please check email to download the wash receipt.'
        ]);
    }
  



}
