<?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\CwoSubscription;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Config;
use Hash;
use Auth;

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


    public function index(){
    }

    public function register(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_name' => 'required',
                'cwo_company' => 'required',
                'cwo_email' => 'required|email|unique:cwo_master,cwo_email',
                'cwo_phone' => 'required',
                // 'address_line_1' => 'required',
                // 'user_name' => 'required',
                'subscription_select' => 'required',
                'password' => [
                    'required',
                    'confirmed',
                    // 'string',
                    // 'min:8',             // must be at least 8 characters in length
                    // 'regex:/[a-z]/',      // must contain at least one lowercase letter
                    // 'regex:/[A-Z]/',      // must contain at least one uppercase letter
                    // 'regex:/[0-9]/',      // must contain at least one digit
                    // 'regex:/[!@#$%^&*()]/', // must contain a special character
                ],
            ],[
                'cwo_email.unique' => 'This email already exists.',
                'password.confirmed' => 'The password and confirm password field should be same.'
            ]);
            
        if($validate->fails()){
            $errorString = implode(",",$validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }
        // exit;

        $cwo_name = $request->input('cwo_name');
        $cwo_company = $request->input('cwo_company');
        // $cwo_business_details = $request->input('cwo_business_details');
        $cwo_email = $request->input('cwo_email');
        $cwo_isd = $request->input('cwo_isd');
        $cwo_phone = $request->input('cwo_phone');
        $password = Hash::make($request->input('password'));
        $allow_prebooking = $request->input('allow_prebooking');
        // $address_line_1 = $request->input('address_line_1');
        // $address_line_2 = $request->input('address_line_2');
        // $user_name = $request->input('user_name');
        $user_name = $request->input('cwo_name');
        $subscription_select = $request->input('subscription_select');
        $subscription_detail_id = $request->input('subscription_detail_id');
        $subscription_type = $request->input('subscription_type');
        $amount = $request->input('amount');
        $dt = Carbon::now(); 
        // $t=time();
        $account_no = generateUniqueNumberCWO();
        // echo $account_no;exit;
        // echo strtok($cwo_isd, ' ');exit;

        if($subscription_select == 1){
            $is_paid = 0;
        } elseif($subscription_select == 2){
            $is_paid = 1;
        } elseif($subscription_select == 3){
            $is_paid = 0;
        } else {
            $is_paid = 0;
        }

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

        /*insert into cwo_master table starts*/
        $data = CarWashOwner::create([
            'cwo_name'=> $cwo_name,
            'cwo_company'=> $cwo_company,
            'cwo_email'=> $cwo_email,
            'cwo_isd'=> strtok($cwo_isd, ' '),
            'cwo_phone'=> $cwo_phone,
            // 'address'=> $address_line_1 . ', ' . $address_line_2,
            // 'cwo_business_details'=> $cwo_business_details,
            'allow_prebooking'=> $allow_prebooking,
            'is_paid'=> $is_paid,
            'cwo_logo'=> $cwo_logo,
            'account_no'=> $account_no,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into cwo_master table ends*/

        /*insert into users table starts*/
        $cwo_id = $data->id;
        if($cwo_name == ''){
            $cwo_name = $cwo_company;
        }
        $dataCwo = User::create([
            'name'=> $user_name,
            'email'=> $cwo_email,
            'password'=> $password,
            'user_type'=> 2,
            'is_active'=> 1,
            'phone'=> $cwo_phone,
            'cwo_id'=> $cwo_id,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into users table ends*/

        /*insert into cwo_subscriptions table starts*/
        $t='';
        $cwo_subscription_id = 0;
        if($subscription_select == 1){
            if($subscription_detail_id != ''){
                $t=time();
                $today = date('Y-m-d');
                $time = strtotime(date("Y-m-d"));
                $transaction_id = $request->input('transaction_id');
                if($subscription_type == 1){
                    $end_date = date("Y-m-d", strtotime("+1 month", $time));
                }
                if($subscription_type == 2){
                    $end_date = date("Y-m-d", strtotime("+1 year", $time));
                }
                $dataCwoSubscription = CwoSubscription::create([
                    'subscription_detail_id'=> $subscription_detail_id,
                    'cwo_id'=> $cwo_id,
                    'start_date'=> $today,
                    'end_date'=> $end_date,
                    'type'=> $subscription_type,
                    'amount'=> $amount,
                    // 'transaction_id'=> $transaction_id,
                    'transaction_id'=> $t,
                    'status'=> 0,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]); 
                $cwo_subscription_id = $dataCwoSubscription->id;  

                $cwoDetails = CarWashOwner::findOrFail($cwo_id);
                $cwoDetails->update([
                    'cwo_subscription_id'=> $cwo_subscription_id,
                    'start_date'=> $today,
                    'end_date'=> $end_date,
                    // 'is_paid'=> 0,
                    'updated_at'=>$dt->toDayDateTimeString()
                    ]);
            }
        
            /*insert into cwo_subscriptions table ends*/

            /*send mail starts*/
            $recipient_name = $cwo_name;
            // $recipient_email = 'tamashree@karmicksolutions.com';
            $recipient_email = $cwo_email;
            // $message_body = 'test body';
            $subject = 'Autoclick - Registration Successful';
            /*$message_body = '<html>
                                <head>
                                <title>Autoclick - Registration Successful</title>
                                </head>
                                <body>
                                    <p>Good day ' . $cwo_name . '</p>
                                    <p>Walaaa, you are all set! And your account is active, and you are good to go.</p>
                                    <p style="color: red;">***IMPORTANT INFORMATION***</p>
                                    <p>As a carwash business owner, you get 7 days free trial thereafter you will pay a subscription fee either monthly, half year or yearly as per subscription packages below. No credit/debit card information needed, no contract obligations, cancel anytime you want. Subscription amount paid is non-refundable if you decide to cancel before expiry of the subscription term.
                                    </p>
                                    <p>Subscription packages are ZAR300 or US$17 / month, ZAR1700 or US$95 / 6 months and lastly ZAR3420 or US$190 per year (12 months)
                                    </p>
                                    <p>For any assistance, please call us on +2762 485 3113 or email on the details below.
                                    </p>
                                    <p>Warm Regards</p>
                                    <p><strong>AUTOCLICK</strong> | ULTIMATE CAR WASH APP FOR EVERYONE</p>
                                    <p><a href="www.autoclick.co.za">www.autoclick.co.za</a> | <a href="mailto:www.autoclick.co.za">info@autoclick.co.za</a>  </p>
                                </body>
                            </html>';*/
            $message_body = '<html>
            <head>
            <title>Welcome to Autoclick</title>
            </head>
            <body>
                <p>Dear ' . $cwo_name . '</p>
                <p>Thank you for signing up and welcome to the AUTOCLICK family as our partner, where we say leave the pen and paper and let the technology and apps do the business for you.</p>
                <p>Your account number is '.$account_no.'</p>
                <p>Your username: '.$cwo_email.'</p>
                <p>Your password: '.$request->input('password').'</p>
                <p>You have joined the smart revolution to give your business a boost and making sure you are always on top of your business whenever and wherever you are in the world. You will have access to real time activities on your business and may more on your fingertips.</p>
                <p>Clients these days are not just looking for a service that is done right, they are also looking for benefits of receiving such services. So if you participate in the loyalty program which you have control of as to how many times will a client get a free car wash… is it 5, 8, 10 or any of your choice, you will get more loyal customers as anyone wants freebies once in a while.</p>
                <p>See what you will get when you start using this app;</p>
                <p>Quick Car Details: Instantly get car details by scanning the license disk (South Africa Only) and manual entry is available is available for all other countries.</p>
                <ul>
                    <li>1. <strong>Pre-Booking: </strong>You can get clients prebooking their cars for a carwash and better for those running mobile carwash businesses can opt for a booking feature on the app.</li>
                    <li>2. <strong>Client Information: </strong>Capture and store the client’s name, picture, cell number, and email on their first visit. No need to capture this information again on subsequent visits.</li>
                    <li>3. <strong>Service Information: </strong>Clients can view all your services, business hours, address, and contact information on the app.</li>
                    <li>4. <strong>Loyalty Program: </strong>Set up your own custom loyalty points for customers to earn free washes after a certain number of washes at your business.</li>
                    <li>5. <strong>Timestamp: </strong>Generate reports that shows date and time the car came for a wash, when the carwash is completed and paid for.</li>
                    <li>6. <strong>Receipt Printing: </strong>Option to print receipts via a wireless small printer from the app and put in the car so that cleaners will know what services must be done on the car or share with the customer.</li>
                    <li>7. <strong>Real-Time Financial Dashboard: </strong>Monitor cash and card transactions in real-time on the app wherever you are in the world, so long as you have internet access.</li>
                    <li>8. <strong>Pre-Loaded & Custom Services: </strong>When setting up your business profile, you add all your carwash services and prices and can add custom services on the go if need arises.</li>
                    <li>9. <strong>Carwash Progress Updates: </strong>Update car wash progress on the app and notify car owners via push notifications or email (IN QUEUE & READY FOR COLLECTION).</li>
                    <li>10. <strong>Search Capability: </strong>On the app, once the details have been captured, you can easily search for cars by registration number or description to get information of the owner to either call or update carwash status.</li>
                    <li>11. <strong>Performance Dashboard: </strong>Track the number of cars washed daily, monthly, and yearly.</li>
                    <li>12. <strong>Revenue Tracking: </strong>View cash and card totals, and track revenue generated today, month-to-date, and year-to-date.</li>
                    <li>13. <strong>Efficiency Metrics: </strong>Track the average time taken to wash a car and encourage your team to improve but still maintaining standards.</li>
                    <li>14. <strong>Queue Management: </strong>Monitor the number of cars in the queue at any given time.</li>
                    <li>15. <strong>Client Reviews: </strong>Respond to client reviews and make service improvements as suggested by clients.</li>
                    <li>16. <strong>Comprehensive Reports:</strong>Extract sales, loyalty, and customer reports filtered by dates.</li>
                    <li>17. <strong>Client Database Access: </strong>You have access to the client’s database to notify clients of any promotions and updates via email or SMS notifications.</li>
                    <li>18. <strong>Flexible Pricing: </strong>You can add, remove or update service prices anytime through the web admin portal for your carwash business.</li>
                    <li>19. <strong>User Permissions: </strong>You have unlimited users of the app and can assign permission to team members on what they have access to on the app.</li>
                </ul>
                <p></p>
                <p>Warm Regards</p>
                <p><strong>AUTOCLICK</strong> | ULTIMATE CAR WASH APP FOR EVERYONE</p>
                <p><a href="www.autoclick.co.za">www.autoclick.co.za</a> | <a href="mailto:www.autoclick.co.za">info@autoclick.co.za</a> +27 62 485 3113  </p>
            </body>
        </html>';                
        $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
            // print_r($email);exit;
            /*send mail ends*/
        }

        if($subscription_select == 2){
            // echo "7 days free trial";exit;
            $time = strtotime(date("Y-m-d"));
            $trial_expiry_date = date("Y-m-d", strtotime("+7 day", $time));
            $data->update([
                'free_trial'=> 1,
                'trial_expiry_date'=> $trial_expiry_date,
                'updated_at'=>$dt->toDayDateTimeString()
                ]);
            // exit;    
        }

        if($subscription_select == 3){
            // echo "Offline";exit;
            /*send mail starts*/
            $recipient_name = $cwo_name;
            // $recipient_email = 'tamashree@karmicksolutions.com';
            $recipient_email = $cwo_email;
            // $message_body = 'test body';
            $subject = 'Autoclick - Registration Successful';
            $message_body = '<html>
            <head>
            <title>Welcome to Autoclick</title>
            </head>
            <body>
                <p>Dear ' . $cwo_name . '</p>
                <p>Thank you for signing up and welcome to the AUTOCLICK family as our partner, where we say leave the pen and paper and let the technology and apps do the business for you.</p>
                <p>Your account number is '.$account_no.'</p>
                <p>Your username: '.$cwo_email.'</p>
                <p>Your password: '.$request->input('password').'</p>
                <p>Amount For Monthly Subscription: R300.</p>
                <p>Amount For Yearly Subscription: R3420.</p>
                <p>Please pay in the following bank details:</p>
                <p>Name: Bank Name</p>
                <p>Account No.: Afdfggfgf</p>
                <p>IFSC Code: III000</p>
                <p>Please share your Autoclick Account Number and payment transaction ID once payment is completed.</p>
                <p>Clients these days are not just looking for a service that is done right, they are also looking for benefits of receiving such services. So if you participate in the loyalty program which you have control of as to how many times will a client get a free car wash… is it 5, 8, 10 or any of your choice, you will get more loyal customers as anyone wants freebies once in a while.</p>
                <p>See what you will get when you start using this app;</p>
                <p>Quick Car Details: Instantly get car details by scanning the license disk (South Africa Only) and manual entry is available is available for all other countries.</p>
                <ul>
                    <li>1. <strong>Pre-Booking: </strong>You can get clients prebooking their cars for a carwash and better for those running mobile carwash businesses can opt for a booking feature on the app.</li>
                    <li>2. <strong>Client Information: </strong>Capture and store the client’s name, picture, cell number, and email on their first visit. No need to capture this information again on subsequent visits.</li>
                    <li>3. <strong>Service Information: </strong>Clients can view all your services, business hours, address, and contact information on the app.</li>
                    <li>4. <strong>Loyalty Program: </strong>Set up your own custom loyalty points for customers to earn free washes after a certain number of washes at your business.</li>
                    <li>5. <strong>Timestamp: </strong>Generate reports that shows date and time the car came for a wash, when the carwash is completed and paid for.</li>
                    <li>6. <strong>Receipt Printing: </strong>Option to print receipts via a wireless small printer from the app and put in the car so that cleaners will know what services must be done on the car or share with the customer.</li>
                    <li>7. <strong>Real-Time Financial Dashboard: </strong>Monitor cash and card transactions in real-time on the app wherever you are in the world, so long as you have internet access.</li>
                    <li>8. <strong>Pre-Loaded & Custom Services: </strong>When setting up your business profile, you add all your carwash services and prices and can add custom services on the go if need arises.</li>
                    <li>9. <strong>Carwash Progress Updates: </strong>Update car wash progress on the app and notify car owners via push notifications or email (IN QUEUE & READY FOR COLLECTION).</li>
                    <li>10. <strong>Search Capability: </strong>On the app, once the details have been captured, you can easily search for cars by registration number or description to get information of the owner to either call or update carwash status.</li>
                    <li>11. <strong>Performance Dashboard: </strong>Track the number of cars washed daily, monthly, and yearly.</li>
                    <li>12. <strong>Revenue Tracking: </strong>View cash and card totals, and track revenue generated today, month-to-date, and year-to-date.</li>
                    <li>13. <strong>Efficiency Metrics: </strong>Track the average time taken to wash a car and encourage your team to improve but still maintaining standards.</li>
                    <li>14. <strong>Queue Management: </strong>Monitor the number of cars in the queue at any given time.</li>
                    <li>15. <strong>Client Reviews: </strong>Respond to client reviews and make service improvements as suggested by clients.</li>
                    <li>16. <strong>Comprehensive Reports:</strong>Extract sales, loyalty, and customer reports filtered by dates.</li>
                    <li>17. <strong>Client Database Access: </strong>You have access to the client’s database to notify clients of any promotions and updates via email or SMS notifications.</li>
                    <li>18. <strong>Flexible Pricing: </strong>You can add, remove or update service prices anytime through the web admin portal for your carwash business.</li>
                    <li>19. <strong>User Permissions: </strong>You have unlimited users of the app and can assign permission to team members on what they have access to on the app.</li>
                </ul>
                <p style="color: red;">***IMPORTANT INFORMATION***</p>
                <p>As a carwash business owner, you get 7 days free trial thereafter you will pay a subscription fee either monthly, half year or yearly as per subscription packages below. No credit/debit card information needed, no contract obligations, cancel anytime you want. Subscription amount paid is non-refundable if you decide to cancel before expiry of the subscription term.</p>
                <p>Subscription packages are ZAR300 or US$17 / month, ZAR1700 or US$95 / 6 months and lastly ZAR3420 or US$190 per year (12 months)</p>
                <p></p>
                <p>Warm Regards</p>
                <p><strong>AUTOCLICK</strong> | ULTIMATE CAR WASH APP FOR EVERYONE</p>
                <p><a href="www.autoclick.co.za">www.autoclick.co.za</a> | <a href="mailto:www.autoclick.co.za">info@autoclick.co.za</a> +27 62 485 3113  </p>
            </body>
        </html>';                
            $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
            // print_r($email);exit;
            /*send mail ends*/
        }

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'cwo_id' => $cwo_id,
            'cwo_account_no' => $account_no,
            'subscription_select' => $subscription_select,
            'user_id' => $dataCwo->id,
            'cwo_subscription_id' => $cwo_subscription_id,
            'transaction_id'=> $t
        ]);

    }

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

        $email = $request->input('email');
        $password = $request->input('password');
        $dt = Carbon::now(); 
        $credentials = [
            'email' => $email,
            'password' => $password,
            'user_type' => 2,
        ];
        if(!Auth::guard('web')->attempt($credentials)) {    
            return response()->json([
                'success' => 0,
                'message' => 'Sorry! Email & Password does not match with our record.',
            ]);
        }
        $user = User::where('email', $email)->first();
        // echo "<pre>";print_r($user->toArray());exit;
        $getCwo = CarWashOwner::where('id', $user->cwo_id)->select('is_paid', 'account_no')->first();
        // if($getCwo->is_paid == 0){
        //     return response()->json([
        //         'success' => 0,
        //         'message' => 'Sorry! The car wash owner company did not pay till now.',
        //     ]);
        // }
        $token = '';
        $token = $user->createToken('App User Token');
        $token = $token->plainTextToken;

        /*token create starts*/
        /*$token_email = 'admin@gmail.com';
        $token_password = '123456';
        $credentials = [
            'email' => $token_email,
            'password' => $token_password,
            'user_type' => 1,
        ];  
        if(Auth::attempt($credentials)){
            $tokenUser = User::where('email', $token_email)->first();
            $token = $tokenUser->createToken('App Token');
            $token = $token->plainTextToken;
        } else {
            $token = '';
        }*/
        /*token create ends*/

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'cwo_id' => $user->cwo_id,
            'cwo_account_no' => $getCwo->account_no,
            'user_id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
            'phone' => $user->phone,
            'address' => $user->address,
            'is_cwo_paid' => $getCwo->is_paid,
            'token' => $token
        ]);

    }

    public function addSubscription(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required',
                'subscription_detail_id' => 'required',
                'subscription_type' => 'required',
                'amount' => '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');
        $subscription_detail_id = $request->input('subscription_detail_id');
        $subscription_type = $request->input('subscription_type');
        $transaction_id = $request->input('transaction_id');
        $amount = $request->input('amount');
        $dt = Carbon::now(); 

        /*insert into cwo_subscriptions table starts*/
        $today = date('Y-m-d');
        $time = strtotime(date("Y-m-d"));
        if($subscription_type == 1){
            $end_date = date("Y-m-d", strtotime("+1 month", $time));
        }
        if($subscription_type == 2){
            $end_date = date("Y-m-d", strtotime("+1 year", $time));
        }
        $dataCwoSubscription = CwoSubscription::create([
            'subscription_detail_id'=> $subscription_detail_id,
            'cwo_id'=> $cwo_id,
            'start_date'=> $today,
            'end_date'=> $end_date,
            'type'=> $subscription_type,
            'amount'=> $amount,
            'transaction_id'=> $transaction_id,
            'status'=> 1,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]); 
        $cwo_subscription_id = $dataCwoSubscription->id;  

        $cwoDetails = CarWashOwner::findOrFail($cwo_id);
        $cwoDetails->update([
            'cwo_subscription_id'=> $cwo_subscription_id,
            'start_date'=> $today,
            'end_date'=> $end_date,
            'is_paid'=> 1,
            'updated_at'=>$dt->toDayDateTimeString()
            ]);
        /*insert into cwo_subscriptions table ends*/

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

    }

    public function details(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');
        $getCwo = CarWashOwner::with('currencyDetail')->where('id', $cwo_id)->first();
        if($getCwo != ''){
            return response()->json([
                'success' => 1,
                'message' => 'Success.',
                'cwo_id' => $cwo_id,
                'cwo_name' => $getCwo->cwo_name,
                'cwo_isd' => $getCwo->cwo_isd,
                'cwo_phone' => $getCwo->cwo_phone,
                'cwo_email' => $getCwo->cwo_email,
                'cwo_company' => $getCwo->cwo_company,
                // 'cwo_logo' => $getCwo->cwo_logo,
                'cwo_logo' => asset('images/cwo') . '/' . $getCwo->cwo_logo,
                'cwo_business_details' => $getCwo->cwo_business_details,
                'address' => $getCwo->address,
                'allow_prebooking' => $getCwo->allow_prebooking,
                'is_paid' => $getCwo->is_paid,
                'currency' => $getCwo->currencyDetail
            ]);    
        } else {
            return response()->json([
                'success' => 0,
                'message' => 'The CWO ID does not exist.',
            ]);
        }
    }

    public function addUser(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required',
                'name' => 'required',
                'email' => 'required|email|unique:users,email',
                'phone' => 'required',
                'address' => 'required',
                'password' => [
                    'required',
                    // 'string',
                    // 'min:8',             // must be at least 8 characters in length
                    // 'regex:/[a-z]/',      // must contain at least one lowercase letter
                    // 'regex:/[A-Z]/',      // must contain at least one uppercase letter
                    // 'regex:/[0-9]/',      // must contain at least one digit
                    // 'regex:/[!@#$%^&*()]/', // must contain a special character
                ],
            ],[
                'email.unique' => 'This email already exists.'
            ]);
            
        if($validate->fails()){
            $errorString = implode(",",$validate->messages()->all());
            return response()->json([
                'success' => 0,
                // 'message' => $validate->errors(),
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $name = $request->input('name');
        $email = $request->input('email');
        $phone = $request->input('phone');
        $address = $request->input('address');
        $password = Hash::make($request->input('password'));
        $dt = Carbon::now();

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

        /*insert into users table starts*/
        $record = User::create([
            'name'=> $name,
            'email'=> $email,
            'password'=> $password,
            'user_type'=> 2,
            'phone'=> $phone,
            'address'=> $address,
            'cwo_id'=> $cwo_id,
            'image'=> $image,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into users table ends*/

        /*send mail starts*/
        $recipient_name = $name;
        // $recipient_email = 'tamashree@karmicksolutions.com';
        $recipient_email = $email;
        $subject = 'Welcome to Autoclick';
        $message_body = '<html>
                            <head>
                            <title>Welcome to Autoclick</title>
                            </head>
                            <body>
                                <p>Dear ' . $name . '</p>
                                <p>Thank you for signing up and welcome to the AUTOCLICK family as our partner, where we say leave the pen and paper and let the technology and apps do the business for you.</p>
                                <p>Your username: '.$email.'</p>
                                <p>Your password: '.$request->input('password').'</p>
                                <p>You have joined the smart revolution to give your business a boost and making sure you are always on top of your business whenever and wherever you are in the world. You will have access to real time activities on your business and may more on your fingertips.</p>
                                <p>Clients these days are not just looking for a service that is done right, they are also looking for benefits of receiving such services. So if you participate in the loyalty program which you have control of as to how many times will a client get a free car wash… is it 5, 8, 10 or any of your choice, you will get more loyal customers as anyone wants freebies once in a while.</p>
                                <p>See what you will get when you start using this app;</p>
                                <p>Quick Car Details: Instantly get car details by scanning the license disk (South Africa Only) and manual entry is available is available for all other countries.</p>
                                <ul>
                                    <li>1. <strong>Pre-Booking: </strong>You can get clients prebooking their cars for a carwash and better for those running mobile carwash businesses can opt for a booking feature on the app.</li>
                                    <li>2. <strong>Client Information: </strong>Capture and store the client’s name, picture, cell number, and email on their first visit. No need to capture this information again on subsequent visits.</li>
                                    <li>3. <strong>Service Information: </strong>Clients can view all your services, business hours, address, and contact information on the app.</li>
                                    <li>4. <strong>Loyalty Program: </strong>Set up your own custom loyalty points for customers to earn free washes after a certain number of washes at your business.</li>
                                    <li>5. <strong>Timestamp: </strong>Generate reports that shows date and time the car came for a wash, when the carwash is completed and paid for.</li>
                                    <li>6. <strong>Receipt Printing: </strong>Option to print receipts via a wireless small printer from the app and put in the car so that cleaners will know what services must be done on the car or share with the customer.</li>
                                    <li>7. <strong>Real-Time Financial Dashboard: </strong>Monitor cash and card transactions in real-time on the app wherever you are in the world, so long as you have internet access.</li>
                                    <li>8. <strong>Pre-Loaded & Custom Services: </strong>When setting up your business profile, you add all your carwash services and prices and can add custom services on the go if need arises.</li>
                                    <li>9. <strong>Carwash Progress Updates: </strong>Update car wash progress on the app and notify car owners via push notifications or email (IN QUEUE & READY FOR COLLECTION).</li>
                                    <li>10. <strong>Search Capability: </strong>On the app, once the details have been captured, you can easily search for cars by registration number or description to get information of the owner to either call or update carwash status.</li>
                                    <li>11. <strong>Performance Dashboard: </strong>Track the number of cars washed daily, monthly, and yearly.</li>
                                    <li>12. <strong>Revenue Tracking: </strong>View cash and card totals, and track revenue generated today, month-to-date, and year-to-date.</li>
                                    <li>13. <strong>Efficiency Metrics: </strong>Track the average time taken to wash a car and encourage your team to improve but still maintaining standards.</li>
                                    <li>14. <strong>Queue Management: </strong>Monitor the number of cars in the queue at any given time.</li>
                                    <li>15. <strong>Client Reviews: </strong>Respond to client reviews and make service improvements as suggested by clients.</li>
                                    <li>16. <strong>Comprehensive Reports:</strong>Extract sales, loyalty, and customer reports filtered by dates.</li>
                                    <li>17. <strong>Client Database Access: </strong>You have access to the client’s database to notify clients of any promotions and updates via email or SMS notifications.</li>
                                    <li>18. <strong>Flexible Pricing: </strong>You can add, remove or update service prices anytime through the web admin portal for your carwash business.</li>
                                    <li>19. <strong>User Permissions: </strong>You have unlimited users of the app and can assign permission to team members on what they have access to on the app.</li>
                                </ul>
                                <p style="color: red;">***IMPORTANT INFORMATION***</p>
                                <p>As a carwash business owner, you get 7 days free trial thereafter you will pay a subscription fee either monthly, half year or yearly as per subscription packages below. No credit/debit card information needed, no contract obligations, cancel anytime you want. Subscription amount paid is non-refundable if you decide to cancel before expiry of the subscription term.</p>
                                <p>Subscription packages are ZAR300 or US$17 / month, ZAR1700 or US$95 / 6 months and lastly ZAR3420 or US$190 per year (12 months)</p>
                                <p></p>
                                <p>Warm Regards</p>
                                <p><strong>AUTOCLICK</strong> | ULTIMATE CAR WASH APP FOR EVERYONE</p>
                                <p><a href="www.autoclick.co.za">www.autoclick.co.za</a> | <a href="mailto:www.autoclick.co.za">info@autoclick.co.za</a> +27 62 485 3113  </p>
                            </body>
                        </html>';
        // $email = sendMail(array($recipient_name, $recipient_email), $message_body, $subject);
        // print_r($email);exit;
        /*send mail ends*/

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

    }

}
