@extends('layouts.app')

@section('title', 'Create Expense')

@section('content')
    <x-page-head title="Record Expense" subtitle="Posts journal — DR expense, CR cash or AP">
        <x-slot:actions>
            <a href="{{ route('finance.suppliers.create') }}" class="btn btn-outline">Add Supplier</a>
        </x-slot:actions>
    </x-page-head>

    <x-panel>
        <form method="POST" action="{{ route('finance.expenses.store') }}">
            @csrf
            <div style="display:grid; grid-template-columns: repeat(2, 1fr); gap:16px;">
                <div class="form-group">
                    <label class="form-label" for="supplier_id">Supplier</label>
                    <select class="input" id="supplier_id" name="supplier_id">
                        <option value="">— None —</option>
                        @foreach ($suppliers as $supplier)
                            <option value="{{ $supplier->id }}" @selected(old('supplier_id') == $supplier->id)>
                                {{ $supplier->code }} — {{ $supplier->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label" for="account_id">Expense Account</label>
                    <select class="input" id="account_id" name="account_id" required>
                        <option value="">Select account</option>
                        @foreach ($expenseAccounts as $account)
                            <option value="{{ $account->id }}" @selected(old('account_id') == $account->id)>
                                {{ $account->code }} — {{ $account->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label" for="amount">Amount</label>
                    <input class="input" type="number" step="0.01" min="0.01" id="amount" name="amount" value="{{ old('amount') }}" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="expense_date">Expense Date</label>
                    <input class="input" type="date" id="expense_date" name="expense_date" value="{{ old('expense_date', now()->toDateString()) }}" required>
                </div>
                <div class="form-group" style="grid-column: span 2;">
                    <label class="form-label" for="description">Description</label>
                    <input class="input" type="text" id="description" name="description" value="{{ old('description') }}">
                </div>
                <div class="form-group">
                    <label class="form-label">
                        <input type="checkbox" name="on_credit" value="1" @checked(old('on_credit'))> Pay on credit (Accounts Payable)
                    </label>
                </div>
            </div>
            <div style="display:flex; gap:10px; margin-top:16px;">
                <button type="submit" class="btn btn-primary">Post Expense</button>
                <a href="{{ route('finance.expenses.index') }}" class="btn btn-ghost">Cancel</a>
            </div>
        </form>
    </x-panel>
@endsection
