<?php

namespace App\Http\Controllers\OwnerPanel;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\LoyaltyPointToZar;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Auth;

class OwnerLoyaltyPointToZarController extends Controller
{
    public function __construct()
    {
    }

    public function index(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        // echo $cwoid;exit;
        $dataBag['chMenu'] = '';
        $data = LoyaltyPointToZar::where('cwo_id', $cwo_id)->first();
        $dataBag['records'] = $data;
        // echo "<pre>";print_r($dataBag['records']->toArray());exit;
        return view('owner.loyalty_conversion.index', $dataBag);
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
       
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $cwo_id = Auth::user()->cwo_id;
        // $request->validate([
        //     'code' => ['required', 'numeric', 'digits:4'],
        // ],[
        //     'code.required' => 'Please enter passcode to cancel a car wash.'
        // ]);
        $request->validate([
            'loyalty_point' => 'required|numeric|not_in:0',
            'zar_value' => 'required|numeric|not_in:0',
        ],[
            'loyalty_point.required' => 'Please enter loyalty point.',
            'loyalty_point.numeric' => 'Please enter numeric value for loyalty point.',
            'loyalty_point.not_in' => 'Loyalty point should be greater than 0.',
            'zar_value.required' => 'Please enter zar value.',
            'zar_value.numeric' => 'Please enter numeric value for zar value.',
            'zar_value.not_in' => 'Zar value should be greater than 0.'
        ]);
        $loyalty_point = $request->input('loyalty_point');
        $zar_value = $request->input('zar_value');
        $dt = Carbon::now();

        $getData = LoyaltyPointToZar::where('cwo_id', $cwo_id)->first();
        if($getData != ''){
            $getData->update([
                'loyalty_point'=> $loyalty_point,
                'zar_value'=> $zar_value,
                'updated_at'=>$dt->toDayDateTimeString()
            ]);
        } else {
            /*insert into loyalty_point_to_zars table starts*/
            $record = LoyaltyPointToZar::create([
                    'cwo_id'=> $cwo_id,
                    'loyalty_point'=> $loyalty_point,
                    'zar_value'=> $zar_value,
                    'created_at'=>$dt->toDayDateTimeString(),
                    'updated_at'=>$dt->toDayDateTimeString()
                ]);
            /*insert into loyalty_point_to_zars table ends*/
        }

        return back()->with('success', 'Loyalty Point Conversion has been updated 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)
    {
        
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
    }

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

    public function delete($id){
        
    }
}
