<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\WashCategory;
use App\Models\WashCategoryBodyType;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;

class WashCayegoryController 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');
        $body_type_id = $request->input('body_type_id');

        $getCurrency = getCurencyOfCwo($cwo_id);

        $data = WashCategory::where('cwo_id', $cwo_id)->where('is_active', 1)->orderBy('wash_name', 'asc')->select('id', 'wash_name', 'wash_desc')->get();
        $dataDisplay=[];
        $dataArray = [];
        if($data != ''){
            foreach ($data as $d) {
                $dataDisplay['id'] = $d->id;
                $dataDisplay['wash_name'] = $d->wash_name;
                $dataDisplay['wash_desc'] = $d->wash_desc;

                // $dataDisplay['body_types'] = WashCategoryBodyType::where('wash_category_id', $d->id)->get();
                if($body_type_id == '') {
                    $dataDisplay['body_types'] = WashCategoryBodyType::select('body_types.name','wash_category_body_types.body_type_id','wash_category_body_types.price','wash_category_body_types.duration')
                    ->join('body_types','body_types.id','=','wash_category_body_types.body_type_id')
                    ->where('wash_category_body_types.wash_category_id', $d->id)
                    ->where('body_types.status', 1)
                    ->orderBy('body_types.name', 'asc')
                    ->get();
                } else {
                    $dataDisplay['body_types'] = WashCategoryBodyType::select('body_types.name','wash_category_body_types.body_type_id','wash_category_body_types.price','wash_category_body_types.duration')
                    ->join('body_types','body_types.id','=','wash_category_body_types.body_type_id')
                    ->where('wash_category_body_types.wash_category_id', $d->id)
                    ->where('wash_category_body_types.body_type_id', $body_type_id)
                    ->where('body_types.status', 1)
                    ->orderBy('body_types.name', 'asc')
                    ->get();
                }
                

                $dataArray[] = $dataDisplay;
            }
        }
        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'currency' => $getCurrency,
            'washCategories' => $dataArray
        ]);
    }


}
