<?php

namespace App\Http\Controllers\ChartOfAccounts;

use App\Http\Controllers\Controller;
use App\Models\AccountTypes;
use Illuminate\Http\Request;

class AccountTypeController extends Controller
{
    public function index(Request $request)
    {
        $query = AccountTypes::query();

        if ($request->filled('name')) {
            $query->where('name', 'like', '%' . $request->string('name')->toString() . '%');
        }

        if ($request->filled('code_from')) {
            $query->where('code', '>=', (int) $request->input('code_from'));
        }

        if ($request->filled('code_to')) {
            $query->where('code', '<=', (int) $request->input('code_to'));
        }

        $accountTypes = $query->orderBy('code')->paginate(25)->withQueryString();

        return view('chart-of-accounts.account-types.index', compact('accountTypes'));
    }

    public function destroy(AccountTypes $accountType)
    {
        $accountType->delete();

        return redirect()
            ->route('chart-of-accounts.account-types.index')
            ->with('success', 'Account type deleted successfully.');
    }
}

