<?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 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
        ]);
    }

    


}
