<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\BusinessType;
use App\Helpers\CurrentUser;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //To fet userId..
        $userId = CurrentUser::getUserId();
        $businessTypeData = BusinessType::orderBy('id', 'desc')->where('user_id', $userId)->paginate(10);
        $allBusinessTypeCount = BusinessType::orderBy('id', 'desc')->where('user_id', $userId)->count();

        return view('admin.others.businessType.index',compact('businessTypeData','allBusinessTypeCount'));
    }

    /**
     * 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)
    {
        $request->validate([
            'type_name'=> 'required',
        ]);

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

        if(BusinessType::create($data)){
            Toastr::success('BusinessType Created Successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Sorry, Something Is Wrong.!', 'Error', ["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)
    {
        $request->validate([
            'type_name'=> 'required',
        ]);

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

        if($singleBusinessType->update($data)){
            Toastr::success('BusinessType Updated Successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Sorry, Something Is Wrong.!', 'Error', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $singleBusinessTypeData = BusinessType::where('id', $id)->first();
        if($singleBusinessTypeData->delete()){
            Toastr::success('BusinessType Deleted Successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Soething Is Wrong!.', 'Error', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    //To active status...
    public function businessTypeActive($id)
    {
        BusinessType::where('id', $id)->update(['status' => true]);
        Toastr::success('BusinessType Activated Successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
    
    //To inactive status...
    public function businessTypeInactive($id)
    {
        BusinessType::where('id', $id)->update(['status' => false]);
        Toastr::success('BusinessType In-Activated Successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
}
