<?php

namespace App\Http\Controllers;

use App\Expense;
use App\ExpenseCategory;
use App\ExpenseSubCategory;
use DB;
use Illuminate\Http\Request;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        $expenses = new Expense();
        $expenses = $expenses->filter($request, $expenses);
        $expenses = $expenses->orderBy('expense_date', 'desc');

        $expense_category = ExpenseCategory::all();
        $expense_sub_category = ExpenseSubCategory::all();
        return view('pages.expense.index',compact('expense_sub_category'))
            ->withExpenseCategory($expense_category)
            // ->withExpenseSubCategory($expense_sub_category)
            ->withExpenses($expenses->get());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $expese_categories = ExpenseCategory:: where('status', 1)->orderBy('id', 'desc')->get();
        return view('pages.expense.create')
            ->with('expese_categories', $expese_categories);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'category_id' => 'required',
            'expense_date' => 'required',
            'amount' => ['required','max:10', 'min:1']
        ]);

        $data = $request->all();
        $data['user_id'] = auth()->id();
        $expense = Expense::create($data);
        session()->flash('success', 'Expense Save...');
        return back();
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Expense  $expense
     * @return \Illuminate\Http\Response
     */
    public function edit(Expense $expense)
    {
        $expese_categories = ExpenseCategory:: where('status', 1)->latest()->get();
        $expense_sub_category = ExpenseSubCategory:: where('status', 1)->latest()->get();
        return view('pages.expense.create',compact('expense_sub_category'))
            ->with('expese_categories', $expese_categories)
            // ->withExpenseSubCategory($expense_sub_category)
            ->withExpense($expense);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Expense  $expense
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Expense $expense)
    {
        $this->validate($request, [
            'category_id' => 'required',
            'expense_date' => 'required',
            'amount' => ['required','max:10', 'min:1']
        ]);

        $data = $request->all();
        $expense = $expense->update($data);
        session()->flash('success', 'Expense Update...');
        return redirect()->route('expense.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Expense  $expense
     * @return \Illuminate\Http\Response
     */
    public function destroy(Expense $expense)
    {
        $expense->delete();

        session()->flash('success', 'Expense Deleted...');
        return redirect()->back();
    }

     public function categories()
    {
        $query = request('query');
        $categories = ExpenseCategory:: where('status', 1)->select('id', 'name')->where('name', 'LIKE', "%$query%")->get();
        return $categories;
    }

    public function subcategories($id)
    {
         echo json_encode(DB::table('expense_sub_categories')-> where('status', 1)->where('category_id',$id)->get());        
    }
}
