<?php

namespace App\Http\Controllers\OwnerPanel;

use App\Http\Controllers\Controller;
// use App\Services\CarWashClientService;
use Illuminate\Http\Request;
use App\Models\Loyalty;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Auth;

class OwnerLoyaltyController 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 = Loyalty::where('cwo_id', $cwo_id)->where('is_active', '!=' , 3)->orderBy('id', 'desc');
        $dataCnt = $data->count();
        $data = $data->paginate(10);
        $dataBag['allRecords'] = $dataCnt;
        $dataBag['records'] = $data;
        // echo "<pre>";print_r($dataBag['records']->toArray());exit;
        return view('owner.loyalty.list', $dataBag);
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
        $dataBag = array();
        $dataBag['chMenu'] = '';
        return view('owner.loyalty.create', $dataBag);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name',
            'loyalty_name' => 'required',
            'no_of_paid_wash' => 'required|numeric',
            'no_of_free_wash' => 'required|numeric',
            'is_active' => 'required'
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            // 'loyalty_name.unique' => 'This Loyalty name already exists.',
            'no_of_paid_wash.required' => 'Please enter no. of paid washes.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.required' => 'Please enter no. of free washes.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();
        /*insert into loyalties table starts*/
        $record = Loyalty::create([
                'loyalty_name'=> $loyalty_name,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'cwo_id'=> $cwo_id,
                'is_active'=> $is_active,
                'created_at'=>$dt->toDayDateTimeString(),
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
        /*insert into loyalties table ends*/

            // return back()->with('success', 'Loyalty has been added successfully.')->with('msg_class', 'alert alert-success');
            return redirect()->route('owner.loyalty.list')->with('success', 'Loyalty 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 = Loyalty::findorfail($id);
        $dataBag['details'] = $record;
        return view('owner.loyalty.edit', $dataBag);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        // echo $id;exit;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name,' . $id,
            'loyalty_name' => 'required',
            'no_of_paid_wash' => 'required|numeric',
            'no_of_free_wash' => 'required|numeric',
            'is_active' => 'required'
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            // 'loyalty_name.unique' => 'This Loyalty name already exists.',
            'no_of_paid_wash.required' => 'Please enter no. of paid washes.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.required' => 'Please enter no. of free washes.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();
        /*update loyalties table starts*/
        $data = Loyalty::findOrFail($id);
        $record = $data->update([
                'loyalty_name'=> $loyalty_name,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'is_active'=> $is_active,
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
        /*update loyalties table ends*/
        return back()->with('success', 'Loyalty 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 = Loyalty::findorfail($id); // fetch the record
        $record->is_active = "3";
        $record->save();
        return back()->with('msg', 'Loyalty has been deleted successfully.')
                ->with('msg_class', 'alert alert-success');
    }
}
