<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\CarWashOwner;
use App\Models\OtherService;
use App\Models\CarOwnerCar;
use App\Models\OtherServiceReport;
use App\Models\OtherServiceReportToProduct;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;

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


    public function index(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');
        $getCurrency = getCurencyOfCwo($cwo_id);
        $data = OtherService::where('cwo_id', $cwo_id)->where('is_active', 1)->orderBy('id', 'asc')->select('id', 'description', 'price', 'qty_in_stock_applicable', 'quantity')->get();
        // echo "<pre>";print_r($data->toArray());exit;
        $otherServiceStatus = getOtherServicesCount($cwo_id);
        // echo $otherServiceStatus;exit;
        $dataDisplay=[];
        $dataArray = [];
        if($data != ''){
            foreach ($data as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['service'] = $d->description;
                $dataDisplay['price'] = $d->price;
                $dataDisplay['qty_in_stock_applicable'] = $d->qty_in_stock_applicable;
                if($d->quantity != ''){
                    $dataDisplay['quantity_in_stock'] = $d->quantity;
                } else {
                    $dataDisplay['quantity_in_stock'] = '';
                }
                
                $dataArray[] = $dataDisplay;
            }
        }
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'otherServiceStatus' => $otherServiceStatus,
            'otherServices' => $dataArray
        ]);
    }

    public function addOtherServices(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required',
                // 'car_id' => 'required',
                // 'name' => 'required',
                // 'isd_code' => 'required',
                // 'phone' => 'required',
                // 'email' => 'required',
                // 'wash_id' => 'required',
                'other_services' => 'required',
                'total_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');
        $car_id = $request->input('car_id');
        $name = $request->input('name');
        $isd_code = $request->input('isd_code');
        $phone = $request->input('phone');
        $email = $request->input('email');
        $wash_id = $request->input('wash_id');
        $other_services = $request->input('other_services');
        $total_amount = $request->input('total_amount');

        if($name == '') {
            $name = 'Annonymous';
        }
        if($wash_id == '') {
            $wash_id = 0;
        }
        if($car_id != '') {
            $getCar = CarOwnerCar::where('id', $car_id)->select('co_id')->first();
            $co_id = $getCar->co_id;
        } else {
            $co_id = 0;
        }
        // echo 'name- ' . $name . ', wash_id-  '. $wash_id . ', co_id - ' . $co_id;exit;
        $dt = Carbon::now(); 

        /*insert into other_service_reports table starts*/
        $insertOtherService = OtherServiceReport::create([
            'co_id'=> $co_id,
            'name'=> $name,
            'isd_code'=> $isd_code,
            'phone'=> $phone,
            'email'=> $email,
            'total_amount'=> $total_amount,
            'payment_status'=> 0,
            'date'=> date('Y-m-d'),
            'time'=> date('H:i:s'),
            'cwo_id'=> $cwo_id,
            'wash_id'=> $wash_id,
            'created_at'=>$dt->toDayDateTimeString(),
            'updated_at'=>$dt->toDayDateTimeString()
        ]);
        /*insert into other_service_reports table ends*/     
        $other_service_report_id = $insertOtherService->id;

        if($other_services != ''){
            $other_services_id_array=json_decode($other_services, true);
            if($other_services_id_array != ""){  // if starts
                foreach ($other_services_id_array as $e) { // foreach starts
                    $assignWashType = OtherServiceReportToProduct::create([
                        'other_service_report_id'=> $other_service_report_id,
                        'other_service_id'=> $e['other_service_id'],
                        'price'=> $e['unit_price'],
                        'quantity'=> $e['quantity'],
                        'total_price'=> $e['total_price'],
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]);

                    $getOtherService = OtherService::findorfail($e['other_service_id']);
                    // echo $getOtherService->qty_in_stock_applicable . ' - ' . $getOtherService->quantity . "<br>";
                    if($getOtherService->qty_in_stock_applicable == 1) {
                        $remaining_quantity = $getOtherService->quantity - $e['quantity'];
                        $getOtherService->quantity = $remaining_quantity;
                        $getOtherService->save();
                    }


                } // foreach starts
            } // if ends
        }
        // exit;
        
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'other_service_report_id' => $other_service_report_id,
        ]);
    }

    public function getStatus(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');
        $otherServiceStatus = getOtherServicesCount($cwo_id);
        // echo $otherServiceStatus;exit;
        
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'otherServiceStatus' => $otherServiceStatus,
        ]);
    }


}
