<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\Expense;
use App\Models\ExpenseCategory;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $expenseData = Expense::orderBy('id', 'desc')->paginate(10);
        $expenseCategoryData = ExpenseCategory::orderBy('id','desc')->where('status',true)->get();

        $allExpenseCount = Expense::orderBy('id', 'desc')->count();
        $activeExpenseCount = Expense::orderBy('id','desc')->where('status',true)->count();
        $inactiveExpenseCount = Expense::orderBy('id','desc')->where('status',false)->count();

        return view('admin.expenses.expense.index',compact('expenseData','expenseCategoryData','allExpenseCount','activeExpenseCount','inactiveExpenseCount'));
    }

    //To get active data...
    public function activeList()
    {
        $expenseData = Expense::orderBy('id','desc')->where('status',true)->paginate(10);
        $expenseCategoryData = ExpenseCategory::orderBy('id','desc')->where('status',true)->get();

        $allExpenseCount = Expense::orderBy('id', 'desc')->count();
        $activeExpenseCount = Expense::orderBy('id','desc')->where('status',true)->count();
        $inactiveExpenseCount = Expense::orderBy('id','desc')->where('status',false)->count();

        return view('admin.expenses.expense.activeList',compact('expenseData','expenseCategoryData','allExpenseCount','activeExpenseCount','inactiveExpenseCount'));
    }

    //To get inactive data...
    public function inActiveList()
    {
        $expenseData = Expense::orderBy('id','desc')->where('status',false)->paginate(10);
        $expenseCategoryData = ExpenseCategory::orderBy('id','desc')->where('status',true)->get();

        $allExpenseCount = Expense::orderBy('id', 'desc')->count();
        $activeExpenseCount = Expense::orderBy('id','desc')->where('status',true)->count();
        $inactiveExpenseCount = Expense::orderBy('id','desc')->where('status',false)->count();
        
        return view('admin.expenses.expense.inActiveList',compact('expenseData','expenseCategoryData','allExpenseCount','activeExpenseCount','inactiveExpenseCount'));
    }

    /**
     * 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([
            'expense_category_id'=> 'required',
            'expense_name'=> 'required',
        ]);

        $data = $request->all();

        if(Expense::create($data)){
            Toastr::success('Expense 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([
            'expense_category_id'=> 'required',
            'expense_name'=> 'required',
        ]);

        $data = $request->all();
        $singleExpense = Expense::where('id', $id)->first();

        if($singleExpense->update($data)){
            Toastr::success('Expense 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)
    {
        $singleExpenseData = Expense::where('id', $id)->first();
        if($singleExpenseData->delete()){
            Toastr::success('Expense 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 expenseActive($id)
    {
        Expense::where('id', $id)->update(['status' => true]);
        Toastr::success('Expense activated successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
    
    //To inactive status...
    public function expenseInactive($id)
    {
        Expense::where('id', $id)->update(['status' => false]);
        Toastr::success('Expense in-activated successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
}
