<?php
//use App\ExpenseSubcategory;
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\ExpenseCategory;
use App\ExpenseSubCategory;
class ExpenseSubcategoryController extends Controller
{

    public function __construct()
    {
        $this->middleware('can:create-expense_sub_category',  ['only' => ['create', 'store']]);
        $this->middleware('can:edit-expense_sub_category',  ['only' => ['edit', 'update']]);
        $this->middleware('can:delete-expense_sub_category', ['only' => ['destroy']]);
        $this->middleware('can:list-expense_sub_category', ['only' => ['index']]);
    }

    public function index(Request $request)
    {
        $sub_categories = new ExpenseSubCategory();
        
        if ($request->category_id) {
            $sub_categories = $sub_categories->where('category_id', $request->category_id);
        }

        $sub_categories = $sub_categories->latest()->get();
        $categories = ExpenseCategory::orderBy('name')->get();

        return view('pages.expense-sub_category.index')
            ->withExpenseSubCategories($sub_categories)
            ->with('expense_categories', $categories);

    }

    public function store(Request $request)
    {
        $this->validate($request, [
            // 'name' => 'required|max:255|unique:expense_sub_categories',
            'name' => 'required',
            'category_id' => 'required',
        ]);

        $ex_category = new ExpenseSubCategory();
        $ex_category->name = $request->name;
        $ex_category->category_id = $request->category_id;
        $ex_category->user_id = auth()->id();
        $ex_category->save();

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

    public function edit(ExpenseSubCategory $expense_sub_category)
    {
        $expense_categories = ExpenseCategory::latest()->get();
        $sub_categories = ExpenseSubCategory::latest()->paginate(25);
        return view('pages.expense-sub_category.index')
        ->withExpenseSubCategory($expense_sub_category)
        ->withExpenseSubCategories($sub_categories)
        ->with('expense_categories', $expense_categories);
    }

     public function update(Request $request, ExpenseSubCategory $expense_sub_category)
    {
        $this->validate($request, [
            'name' => 'required', // 'name' => 'required|max:255|unique:expense_sub_categories,name,' . $expense_sub_category->id, // 'name' => 'required|max:255|unique:expense_sub_categories,name,' . $expense_sub_category->id, // 'name
            'category_id' => 'required',
        ]);

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

    public function destroy(ExpenseSubCategory $expense_sub_category)
    {
        if($expense_sub_category->delete()){
            session()->flash('success', 'Expense Sub Category Deleted...');
        }else{
            session()->flash('warning', '! Opps Operation Failed...');
        }
        
        return redirect()->back();
    }
    public function toggleStatus(Request $request)
    {
        $category = ExpenseSubCategory::find($request->id);
        $category->status = $request->status;
        $category->save();
        return response()->json(['status' => $category->status]);
    }
}
