<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\CurrentUser;
use App\Models\ServiceCharge;
use Validator;

class ServiceChargeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //To fet userId..
        $userId = CurrentUser::getUserId();
        //To fetch all the service charge data...
        $serviceChargeData = ServiceCharge::orderBy('id', 'desc')->paginate(10);
        $allServiceChargeCount = ServiceCharge::orderBy('id', 'desc')->count();

        if(!empty($serviceChargeData)){
            // Check user has permission...
            if (auth()->user()->hasPermissionTo('service-charge-list', 'web')){
                return response()->json([
                    'message'   =>  'Successfully loaded ata.',
                    'serviceChargeData'   =>  $serviceChargeData,
                    'allServiceChargeCount'   =>  $allServiceChargeCount,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Oops! Access Denied.',
                    'status_code'   => 403
                ], 403);
            }
        }else{
            return response()->json([
				'message'   =>  'Sorry you have no data.',
                'status_code'   => 500
			], 500);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'service_charge'=> 'required',
        ]);

        if ($validator->fails()) {
        	return response(['errors'=>$validator->errors()->all()], 422);
        }

        //To fet userId..
        $userId = CurrentUser::getUserId();
        $data = $request->all();
        $data['user_id'] = $userId;

        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-create', 'web')){
            if(ServiceCharge::create($data)){
                return response()->json([
                    'message'   =>  'ServiceCharge created successfully.',
                    'serviceChargeData'   =>  $data,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $singleServiceChargeData = ServiceCharge::where('id', $id)->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-list', 'web')){
            if(isset($singleServiceChargeData) && $singleServiceChargeData != null){
                return response()->json([
                    'message'   =>  'ServiceCharge type loaded successfully.',
                    'singleServiceChargeData'   => $singleServiceChargeData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Business type not found.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'service_charge'=> 'required',
        ]);

        //To fet userId..
        $userId = CurrentUser::getUserId();
        $data = $request->all();
        $data['user_id'] = $userId;
        $singleServiceChargeData = ServiceCharge::where('id', $id)->first();

        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-edit', 'web')){
            if($singleServiceChargeData->update($data)){
                return response()->json([
                    'message'   =>  'ServiceCharge updated successfully.',
                    'singleServiceChargeData'   =>  $singleServiceChargeData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $singleServiceChargeData = ServiceCharge::where('id', $id)->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-delete', 'web')) {
            if($singleServiceChargeData->delete()){
                return response()->json([
                    'message'   =>  'ServiceCharge deleted successfully.',
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    //To active status...
    public function serviceChargeActive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-edit', 'web')) {
            ServiceCharge::where('id', $id)->update(['status' => true]);
            return response()->json([
                'message'   =>  'ServiceCharge activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
    
    //To inactive status...
    public function serviceChargeInactive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('service-charge-edit', 'web')) {
            ServiceCharge::where('id', $id)->update(['status' => false]);
            return response()->json([
                'message'   =>  'ServiceCharge in-activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
}
