<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use App\Models\BusinessType;
use App\Models\ServiceCharge;
use App\Models\Floor;
use App\Models\Shop;
use App\Models\User;
use App\Helpers\CurrentUser;
use Hash;

class ShopController extends Controller
{

    function __construct()
    {
         $this->middleware('permission:shop-list|shop-create|shop-edit|shop-delete', ['only' => ['index','show']]);
         $this->middleware('permission:shop-create', ['only' => ['create','store']]);
         $this->middleware('permission:shop-edit', ['only' => ['edit','update']]);
         $this->middleware('permission:shop-delete', ['only' => ['destroy']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //To fet userId..
        $userId = CurrentUser::getUserId();
        $shopData = Shop::orderBy('id', 'desc')->where('user_id', $userId)->paginate(10);
        $allShopCount = Shop::orderBy('id', 'desc')->where('user_id', $userId)->count();
        //To fetch business type & floor...
        $floorData = Floor::orderBy('floor_name', 'asc')->where('status', true)->where('user_id', $userId)->get();
        $businessTypeData = BusinessType::orderBy('type_name', 'asc')->where('status', true)->where('user_id', $userId)->get();
        $serviceChargeData = ServiceCharge::orderBy('id', 'asc')->where('status', true)->where('user_id', $userId)->get();
        $shopOwnerData = User::orderBy('name', 'asc')->where('status', true)->whereNotIn('role',['superadmin','admin'])->get();
        $shopKeeperData = User::orderBy('name', 'asc')->where('status', true)->whereNotIn('role',['superadmin','admin'])->get();

        return view('admin.shop.index',compact('shopData','allShopCount','floorData','businessTypeData','serviceChargeData'
                ,'shopOwnerData','shopKeeperData'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'floor_id'=> 'required',
            'business_type_id'=> 'required',
            'service_charge_id'=> 'required',
            'shop_owner_id'=> 'required',
            'shop_keeper_id'=> 'required',
            'shop_name'=> 'required',
            'shop_no' => 'required|unique:shops',
            'shop_size'=> 'required',
            'shop_description'=> 'nullable',
        ]);
    
        //To fet userId..
        $userId = CurrentUser::getUserId();
        $data = $request->all();
        $data['user_id'] = $userId;

        if(Shop::create($data)){
            Toastr::success('Shop Created Successfully!.', '', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Something is error there...!', '', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
     public function edit($id)
     {
        //
     }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   
        $this->validate($request, [
            'floor_id'=> 'required',
            'business_type_id'=> 'required',
            'service_charge_id'=> 'required',
            'shop_owner_id'=> 'required',
            'shop_keeper_id'=> 'required',
            'shop_name'=> 'required',
            'shop_no' => 'required|unique:shops,shop_no,'.$id,
            'shop_size'=> 'required',
            'shop_description'=> 'nullable',
        ]);

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

        if($singleShop->update($data)){
            Toastr::success('Shop Updated Successfully!.', '', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Something is error there...!', '', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $singleShop = Shop::findOrFail($id);
        if($singleShop->delete()){
            Toastr::success('Shop Deleted Successfully!.', '', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Something is error there...!', '', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    //To active ruser...
    public function shopActive($id)
    {
        $singleShop = Shop::find($id);
        $singleShop->status = 1;
        $singleShop->save();
        Toastr::success('Shop Activated Successfully!.', '', ["progressbar" => true]);
        return redirect()->back();
    }
    
    //To active user...
    public function shopInactive($id)
    {
        $singleShop = Shop::find($id);
        $singleShop->status = 0;
        $singleShop->save();
        Toastr::success('Shop Inactivated Successfully!.', '', ["progressbar" => true]);
        return redirect()->back();
    }

}
