<?php

namespace App\Http\Controllers\OwnerPanel;

use App\Http\Controllers\Controller;
// use App\Services\CarWashClientService;
use Illuminate\Http\Request;
use App\Models\CarWashOwner;
use App\Models\WashCategory;
use App\Models\BodyType;
use App\Models\WashCategoryBodyType;
use App\Models\WashDetail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Auth;
use DB;

class OwnerWashCategoryController extends Controller
{
    // protected $carwashclientService;

    // public function __construct(CarWashClientService $carwashclientService)
    public function __construct()
    {
        // $this->carwashclientService = $carwashclientService;
    }

    public function index(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        // echo $cwoid;exit;
        $dataBag['chMenu'] = '';
        // $data = WashCategory::with('cwoDetail')->where('cwo_id', $cwo_id)->where('is_active', '!=' , 3)->orderBy('id', 'desc');
        $wash_name = $request->input('wash_name');

        if ($wash_name == '') {
            $data = WashCategory::with('cwoDetail')->where('cwo_id', $cwo_id)->where('is_active', '!=', 3)->orderBy('id', 'desc');
        } else {
            $data = WashCategory::with('cwoDetail')->where('cwo_id', $cwo_id)->where('is_active', '!=', 3)->orderBy('id', 'desc');
            if ($wash_name != '') {
                $data->where('wash_name', 'like', '%' . $wash_name . '%');
            }
        }
        $dataCnt = $data->count();
        $data = $data->paginate(10);
        $dataBag['allRecords'] = $dataCnt;
        $dataBag['records'] = $data;
        // echo "<pre>";print_r($dataBag['records']->toArray());exit;
        $dataBag['cwo'] = CarWashOwner::where('is_active', 1)->orderBy('id', 'desc')->get();
        return view('owner.washcategory.list', $dataBag);
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
        $dataBag = array();
        $dataBag['chMenu'] = '';
        // $dataBag['bodyType'] = BodyType::where('status', 1)->orderBy('sort_order', 'asc')->get();
        $dataBag['bodyType'] = BodyType::where('status', 1)->where('cwo_id', 0)->orderBy('sort_order', 'asc')->get();
        $cwo_id = Auth::user()->cwo_id;
        $dataBag['bodyTypeOfOwner'] = BodyType::where('status', 1)->where('cwo_id', $cwo_id)->orderBy('sort_order', 'asc')->get();
        return view('owner.washcategory.create', $dataBag);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        $request->validate([
            // 'wash_name' => 'required|unique:wash_categories,wash_name',
            'wash_name' => 'required',
            'wash_desc' => 'required',
            // 'price_sedan_hatchback' => 'numeric|nullable',
            // 'price_suv_cab' => 'numeric|nullable',
            // 'price_van_mini' => 'numeric|nullable',
            // 'price_tucks_busses' => 'numeric|nullable',
            'is_active' => 'required'
        ], [
            'wash_name.required' => 'Please enter wash name.',
            'wash_name.unique' => 'This Wash name already exists.',
            'wash_desc.required' => 'Please enter wash description.',
            // 'price_sedan_hatchback.numeric' => 'Price sedan hatchback should be numeric.',
            // 'price_suv_cab.numeric' => 'Price suv cab should be numeric.',
            // 'price_van_mini.numeric' => 'Price van mini should be numeric.',
            // 'price_tucks_busses.numeric' => 'Price trucks busses should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $wash_name = $request->input('wash_name');
        $wash_desc = $request->input('wash_desc');
        // $price_sedan_hatchback = $request->input('price_sedan_hatchback');
        // $price_suv_cab = $request->input('price_suv_cab');
        // $price_van_mini = $request->input('price_van_mini');
        // $price_tucks_busses = $request->input('price_tucks_busses');
        $is_active = $request->input('is_active');
        $body_type_id = $request->input('body_type_id');
        $body_type_name = $request->input('body_type_name');
        $price = $request->input('price');
        // $duration = $request->input('duration');
        $dt = Carbon::now();
        /*insert into wash_categories table starts*/
        $record = WashCategory::create([
            'wash_name' => $wash_name,
            'wash_desc' => $wash_desc,
            'cwo_id' => $cwo_id,
            // 'price_sedan_hatchback'=> $price_sedan_hatchback,
            // 'price_suv_cab'=> $price_suv_cab,
            // 'price_van_mini'=> $price_van_mini,
            // 'price_tucks_busses'=> $price_tucks_busses,
            'is_active' => $is_active,
            'created_at' => $dt->toDayDateTimeString(),
            'updated_at' => $dt->toDayDateTimeString()
        ]);
        $wash_category_id = $record->id;
        /*insert into wash_categories table ends*/

        if (count($body_type_id) > 0) {
            for ($i = 0; $i < count($body_type_id); $i++) {
                // echo $body_type_id[$i] . ' - ' . $body_type_name[$i] . ' - ' . $price[$i] . ' - ' . $duration[$i] . '<br>';
                if ($price[$i] != '') {
                    /*insert into wash_category_body_types table starts*/
                    $recordBodyType = WashCategoryBodyType::create([
                        'wash_category_id' => $wash_category_id,
                        'body_type_id' => $body_type_id[$i],
                        'price' => $price[$i],
                        // 'duration'=> $duration[$i],
                        'created_at' => $dt->toDayDateTimeString(),
                        'updated_at' => $dt->toDayDateTimeString()
                    ]);
                    /*insert into wash_category_body_types table ends*/
                }
            }
        }

        return back()->with('success', 'Carwash Services/Pricelist has been added successfully.')->with('msg_class', 'alert alert-success');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $dataBag = array();
        $dataBag['chMenu'] = '';
        $record = WashCategory::findorfail($id);
        $dataBag['details'] = $record;
        // $dataBag['bodyType'] = BodyType::where('status', 1)->orderBy('sort_order', 'asc')->get();
        $dataBag['bodyType'] = BodyType::where('status', 1)->where('cwo_id', 0)->orderBy('sort_order', 'asc')->get();
        $cwo_id = Auth::user()->cwo_id;
        $dataBag['bodyTypeOfOwner'] = BodyType::where('status', 1)->where('cwo_id', $cwo_id)->orderBy('sort_order', 'asc')->get();
        return view('owner.washcategory.edit', $dataBag);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        // echo $id;exit;
        $request->validate([
            // 'wash_name' => 'required|unique:wash_categories,wash_name,' . $id,
            'wash_name' => 'required',
            'wash_desc' => 'required',
            // 'price_sedan_hatchback' => 'numeric|nullable',
            // 'price_suv_cab' => 'numeric|nullable',
            // 'price_van_mini' => 'numeric|nullable',
            // 'price_tucks_busses' => 'numeric|nullable',
            'is_active' => 'required'
        ], [
            'wash_name.required' => 'Please enter wash name.',
            'wash_name.unique' => 'This Wash name already exists.',
            'wash_desc.required' => 'Please enter wash description.',
            // 'price_sedan_hatchback.numeric' => 'Price sedan hatchback should be numeric.',
            // 'price_suv_cab.numeric' => 'Price suv cab should be numeric.',
            // 'price_van_mini.numeric' => 'Price van mini should be numeric.',
            // 'price_tucks_busses.numeric' => 'Price trucks busses should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $wash_name = $request->input('wash_name');
        $wash_desc = $request->input('wash_desc');
        // $price_sedan_hatchback = $request->input('price_sedan_hatchback');
        // $price_suv_cab = $request->input('price_suv_cab');
        // $price_van_mini = $request->input('price_van_mini');
        // $price_tucks_busses = $request->input('price_tucks_busses');
        $is_active = $request->input('is_active');
        $body_type_id = $request->input('body_type_id');
        $body_type_name = $request->input('body_type_name');
        $price = $request->input('price');
        // $duration = $request->input('duration');
        $dt = Carbon::now();
        /*update wash_categories table starts*/
        $data = WashCategory::findOrFail($id);
        $record = $data->update([
            'wash_name' => $wash_name,
            'wash_desc' => $wash_desc,
            // 'price_sedan_hatchback'=> $price_sedan_hatchback,
            // 'price_suv_cab'=> $price_suv_cab,
            // 'price_van_mini'=> $price_van_mini,
            // 'price_tucks_busses'=> $price_tucks_busses,
            'is_active' => $is_active,
            'updated_at' => $dt->toDayDateTimeString()
        ]);
        /*update wash_categories table ends*/
        DB::delete("DELETE FROM `wash_category_body_types` WHERE `wash_category_id` = " . $id . "");
        if (count($body_type_id) > 0) {
            for ($i = 0; $i < count($body_type_id); $i++) {
                // echo $body_type_id[$i] . ' - ' . $body_type_name[$i] . ' - ' . $price[$i] . ' - ' . $duration[$i] . '<br>';
                if ($price[$i] != '') {
                    /*insert into wash_category_body_types table starts*/
                    $recordBodyType = WashCategoryBodyType::create([
                        'wash_category_id' => $id,
                        'body_type_id' => $body_type_id[$i],
                        'price' => $price[$i],
                        // 'duration'=> $duration[$i],
                        'created_at' => $dt->toDayDateTimeString(),
                        'updated_at' => $dt->toDayDateTimeString()
                    ]);
                    /*insert into wash_category_body_types table ends*/
                }
            }
        }
        return back()->with('success', 'Carwash Services/Pricelist has been updated successfully.')->with('msg_class', 'alert alert-success');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }

    public function delete($id)
    {
        $record_count = WashDetail::where('wash_type_id', $id)->count();
        if ($record_count > 0) {
            if ($record_count > 1) {
                $data = 'washes';
            } else {
                $data = 'wash';
            }
            return back()->with('msg', 'This Carwash Services/Pricelist is associated with ' . $record_count . ' ' . $data . '. You can not delete it.')->with('msg_class', 'alert alert-danger');
        } else {
            $record = WashCategory::findorfail($id); // fetch the record
            $record->is_active = "3";
            $record->save();
            return back()->with('msg', 'Carwash Services/Pricelist has been deleted successfully.')
                ->with('msg_class', 'alert alert-success');
        }
    }
}
