@extends('layouts.app')

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

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Create Transaction</h4>
                    <div class="card-tools">
                        <a href="{{ route('account-transactions.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to List
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('account-transactions.store') }}" method="POST">
                        @csrf

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="account_id" class="form-label">Account *</label>
                                    <select class="form-select @error('account_id') is-invalid @enderror"
                                            id="account_id" name="account_id" required>
                                        <option value="">Select Account</option>
                                        @foreach($accounts as $account)
                                            <option value="{{ $account->id }}" {{ old('account_id') == $account->id ? 'selected' : '' }}>
                                                {{ $account->name }} ({{ $account->model_name }})
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('account_id')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="chart_of_account_id" class="form-label">Chart of Account *</label>
                                    <select class="form-select @error('chart_of_account_id') is-invalid @enderror"
                                            id="chart_of_account_id" name="chart_of_account_id" required>
                                        <option value="">Select Chart of Account</option>
                                        @foreach($chartOfAccounts as $chartOfAccount)
                                            <option value="{{ $chartOfAccount->id }}" {{ old('chart_of_account_id') == $chartOfAccount->id ? 'selected' : '' }}>
                                                {{ $chartOfAccount->code }} - {{ $chartOfAccount->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('chart_of_account_id')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="amount" class="form-label">Amount *</label>
                                    <input type="number" step="0.01" min="0.01" class="form-control @error('amount') is-invalid @enderror"
                                           id="amount" name="amount" value="{{ old('amount') }}" required>
                                    @error('amount')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="transaction_type" class="form-label">Transaction Type *</label>
                                    <select class="form-select @error('transaction_type') is-invalid @enderror"
                                            id="transaction_type" name="transaction_type" required>
                                        <option value="">Select Type</option>
                                        <option value="debit" {{ old('transaction_type') == 'debit' ? 'selected' : '' }}>Debit</option>
                                        <option value="credit" {{ old('transaction_type') == 'credit' ? 'selected' : '' }}>Credit</option>
                                    </select>
                                    @error('transaction_type')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="reference_type" class="form-label">Reference Type</label>
                                    <input type="text" class="form-control @error('reference_type') is-invalid @enderror"
                                           id="reference_type" name="reference_type" value="{{ old('reference_type') }}"
                                           placeholder="e.g., App\Models\PurchaseOrder">
                                    @error('reference_type')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="reference_id" class="form-label">Reference ID</label>
                                    <input type="number" class="form-control @error('reference_id') is-invalid @enderror"
                                           id="reference_id" name="reference_id" value="{{ old('reference_id') }}"
                                           placeholder="e.g., 123">
                                    @error('reference_id')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Create Transaction
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
