<?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;
use Illuminate\Validation\Rule;

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_beforeStartingNewLoyaltyPoints(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name',
            'loyalty_name' => 'required',
            'loyalty_type' => 'required',
            'no_of_paid_wash' => 'required|numeric',
            // 'no_of_paid_wash' => [
            //     Rule::when($request->input('loyalty_type') == 1, 'required')
            // ],
            // 'no_of_free_wash' => 'nullable|numeric',
            'is_active' => 'required',
            'discount_value' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 2, 'max:90'),
                Rule::when($request->input('loyalty_type') == 2, 'required')
            ],
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            'loyalty_type.required' => 'Please select a loyalty type.',
            'no_of_paid_wash.required' => 'Please enter no. of paid wash.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'discount_value.numeric' => 'Discount percentage should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $loyalty_type = $request->input('loyalty_type');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $discount_value = $request->input('discount_value');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();

        if($loyalty_type == 1) {
            $discount_value = '';
        }
        if($loyalty_type == 2) {
            $no_of_free_wash = '';
        }

        $getActiveCount = Loyalty::where('is_active', 1)->where('cwo_id', $cwo_id)->count();
        if($getActiveCount > 0){
            if($is_active == 1) {
                return redirect()->route('owner.loyalty.create')->with('error', 'You already have an active Loyalty Program. Plaese deactivate or delete it and then add an active Loyalty Program.')->with('msg_class', 'alert alert-danger');
            }
        }



        /*insert into loyalties table starts*/
        $record = Loyalty::create([
                'loyalty_name'=> $loyalty_name,
                'loyalty_type'=> $loyalty_type,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'discount_value'=> $discount_value,
                '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');
    }

    public function store(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name',
            'loyalty_name' => 'required',
            'loyalty_type' => 'required',
            // 'no_of_paid_wash' => 'required|numeric',
            // 'no_of_paid_wash' => [
            //     Rule::when($request->input('loyalty_type') == 1, 'required')
            // ],
            'no_of_paid_wash' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 1, 'required')
            ],
            // 'no_of_free_wash' => 'nullable|numeric',
            'is_active' => 'required',
            'start_date' => 'required',
            'end_date' => 'required',
            'discount_value' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 2, 'max:90'),
                Rule::when($request->input('loyalty_type') == 2, 'required')
            ],
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            'loyalty_type.required' => 'Please select a loyalty type.',
            'no_of_paid_wash.required' => 'Please enter no. of paid wash.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'discount_value.numeric' => 'Discount percentage should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $loyalty_type = $request->input('loyalty_type');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $discount_value = $request->input('discount_value');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();

        if($loyalty_type == 1) {
            $discount_value = '';
        }
        if($loyalty_type == 2) {
            $no_of_free_wash = '';
        }

        if($loyalty_type == 3) {
            $no_of_paid_wash = '';
            $no_of_free_wash = '';
            $discount_value = '';
        }

        $start_date = $request->input('start_date');
        if($start_date != ''){
            $start_date = date("Y-m-d", strtotime($start_date));
        }
        $end_date = $request->input('end_date');
        if($end_date != ''){
            $end_date = date("Y-m-d", strtotime($end_date));
        }

        $getActiveCount = Loyalty::where('is_active', 1)->where('cwo_id', $cwo_id)->count();
        if($getActiveCount > 0){
            if($is_active == 1) {
                return redirect()->route('owner.loyalty.create')->with('error', 'You already have an active Loyalty Program. Plaese deactivate or delete it and then add an active Loyalty Program.')->with('msg_class', 'alert alert-danger');
            }
        }



        /*insert into loyalties table starts*/
        $record = Loyalty::create([
                'loyalty_name'=> $loyalty_name,
                'loyalty_type'=> $loyalty_type,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'discount_value'=> $discount_value,
                'cwo_id'=> $cwo_id,
                'is_active'=> $is_active,
                'start_date'=> $start_date,
                'end_date'=> $end_date,
                '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_beforeStartingNewLoyaltyPoints(Request $request, $id)
    {
        // echo $id;exit;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name',
            'loyalty_name' => 'required',
            'loyalty_type' => 'required',
            'no_of_paid_wash' => 'required|numeric',
            // 'no_of_paid_wash' => [
            //     Rule::when($request->input('loyalty_type') == 1, 'required')
            // ],
            // 'no_of_free_wash' => 'nullable|numeric',
            'is_active' => 'required',
            'discount_value' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 2, 'max:90'),
                Rule::when($request->input('loyalty_type') == 2, 'required')
            ],
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            'loyalty_type.required' => 'Please select a loyalty type.',
            'no_of_paid_wash.required' => 'Please enter no. of paid wash.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'discount_value.numeric' => 'Discount percentage should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $loyalty_type = $request->input('loyalty_type');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $discount_value = $request->input('discount_value');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();

        if($loyalty_type == 1) {
            $discount_value = '';
        }
        if($loyalty_type == 2) {
            $no_of_free_wash = '';
        }

        $data = Loyalty::findOrFail($id);

        $getActiveCount = Loyalty::where('is_active', 1)->where('cwo_id', $data->cwo_id)->where('id', '!=', $id)->count();
        if($getActiveCount > 0){
            if($is_active == 1) {
                return redirect()->route('owner.loyalty.edit', array('id' => $id))->with('error', 'You already have an active Loyalty Program. Plaese deactivate or delete it and then add an active Loyalty Program.')->with('msg_class', 'alert alert-danger');
            }
        }

        /*update loyalties table starts*/
        // $data = Loyalty::findOrFail($id);
        $record = $data->update([
                'loyalty_name'=> $loyalty_name,
                'loyalty_type'=> $loyalty_type,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'discount_value'=> $discount_value,
                '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');
    }

    public function update(Request $request, $id)
    {
        // echo $id;exit;
        $request->validate([
            // 'loyalty_name' => 'required|unique:loyalties,loyalty_name',
            'loyalty_name' => 'required',
            'loyalty_type' => 'required',
            // 'no_of_paid_wash' => 'required|numeric',
            // 'no_of_paid_wash' => [
            //     Rule::when($request->input('loyalty_type') == 1, 'required')
            // ],
            'no_of_paid_wash' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 1, 'required')
            ],
            // 'no_of_free_wash' => 'nullable|numeric',
            'is_active' => 'required',
            'start_date' => 'required',
            'end_date' => 'required',
            'discount_value' => [
                'nullable',
                'numeric',
                Rule::when($request->input('loyalty_type') == 2, 'max:90'),
                Rule::when($request->input('loyalty_type') == 2, 'required')
            ],
        ],[
            'loyalty_name.required' => 'Please enter loyalty name.',
            'loyalty_type.required' => 'Please select a loyalty type.',
            'no_of_paid_wash.required' => 'Please enter no. of paid wash.',
            'no_of_paid_wash.numeric' => 'No. of paid wash should be numeric.',
            'no_of_free_wash.numeric' => 'No.of free wash should be numeric.',
            'discount_value.numeric' => 'Discount percentage should be numeric.',
            'is_active.required' => 'Please select a status.'
        ]);
        $loyalty_name = $request->input('loyalty_name');
        $loyalty_type = $request->input('loyalty_type');
        $no_of_paid_wash = $request->input('no_of_paid_wash');
        $no_of_free_wash = $request->input('no_of_free_wash');
        $discount_value = $request->input('discount_value');
        $is_active = $request->input('is_active');
        $dt = Carbon::now();

        if($loyalty_type == 1) {
            $discount_value = '';
        }
        if($loyalty_type == 2) {
            $no_of_free_wash = '';
        }

        if($loyalty_type == 3) {
            $no_of_paid_wash = '';
            $no_of_free_wash = '';
            $discount_value = '';
        }

        $start_date = $request->input('start_date');
        if($start_date != ''){
            $start_date = date("Y-m-d", strtotime($start_date));
        }
        $end_date = $request->input('end_date');
        if($end_date != ''){
            $end_date = date("Y-m-d", strtotime($end_date));
        }

        $data = Loyalty::findOrFail($id);

        $getActiveCount = Loyalty::where('is_active', 1)->where('cwo_id', $data->cwo_id)->where('id', '!=', $id)->count();
        if($getActiveCount > 0){
            if($is_active == 1) {
                return redirect()->route('owner.loyalty.edit', array('id' => $id))->with('error', 'You already have an active Loyalty Program. Plaese deactivate or delete it and then add an active Loyalty Program.')->with('msg_class', 'alert alert-danger');
            }
        }

        /*update loyalties table starts*/
        // $data = Loyalty::findOrFail($id);
        $record = $data->update([
                'loyalty_name'=> $loyalty_name,
                'loyalty_type'=> $loyalty_type,
                'no_of_paid_wash'=> $no_of_paid_wash,
                'no_of_free_wash'=> $no_of_free_wash,
                'discount_value'=> $discount_value,
                'is_active'=> $is_active,
                'start_date'=> $start_date,
                'end_date'=> $end_date,
                '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');
    }
}
