@extends('layouts.app')
@section('title', 'Accounting Settings')
@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Accounting Settings</li>
@endsection
@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Accounting Settings</h1>
        <p class="page-subtitle">Configure accounting periods and posting behaviour</p>
    </div>
</div>

@include('settings._tabs', ['active' => 'accounting'])

@if(session('success'))
<div class="alert alert-success alert-dismissible d-flex gap-2 mb-4">
    <i class="bi bi-check-circle flex-shrink-0"></i><div>{{ session('success') }}</div>
    <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
</div>
@endif

<form method="POST" action="{{ route('settings.accounting.update') }}" id="accForm">
@csrf @method('PUT')
<div class="row g-4">

    {{-- Fiscal Year --}}
    <div class="col-12 col-xl-6">
        <div class="card h-100">
            <div class="card-header"><i class="bi bi-calendar3 me-2 text-primary"></i>Fiscal Year</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Fiscal Year Start Month <span class="text-danger">*</span></label>
                        <select name="fiscal_year_start_month" class="form-select">
                            @php $months=['January','February','March','April','May','June','July','August','September','October','November','December']; @endphp
                            @foreach($months as $i => $month)
                                <option value="{{ $i+1 }}" {{ $settings['fiscal_year_start_month']==$i+1?'selected':'' }}>{{ $month }}</option>
                            @endforeach
                        </select>
                        <div class="form-text"><i class="bi bi-info-circle me-1 text-primary"></i>Fiscal periods are created automatically from this start month. To close a year, set the <a href="{{ route('admin.fiscal-years.index') }}">Books Closing Date</a> under Period Closing — no closing journal entry is needed.</div>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Prior Period Posting</label>
                        <div class="d-flex gap-3 mt-1">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="allow_prior_period_posting" value="1" id="priorYes" {{ $settings['allow_prior_period_posting']==='1'?'checked':'' }}>
                                <label class="form-check-label" for="priorYes">Allow</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="allow_prior_period_posting" value="0" id="priorNo" {{ $settings['allow_prior_period_posting']!=='1'?'checked':'' }}>
                                <label class="form-check-label" for="priorNo">Disallow</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Show Warning for Prior Period Posting</label>
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="prior_period_warning" value="1" id="priorWarn" role="switch" {{ $settings['prior_period_warning']==='1'?'checked':'' }}>
                            <label class="form-check-label" for="priorWarn">Show warning before posting to a prior period</label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- Accounting Guards --}}
    <div class="col-12 col-xl-6">
        <div class="card h-100">
            <div class="card-header"><i class="bi bi-shield-check me-2 text-primary"></i>Accounting Guards &amp; Modules</div>
            <div class="card-body">
                <div class="row g-3">

                    {{-- Negative Cash --}}
                    <div class="col-12">
                        <div class="p-3 rounded-2 border {{ $settings['allow_negative_cash_bank']==='0' ? 'border-success-subtle bg-success-subtle' : 'border-warning-subtle bg-warning-subtle' }}">
                            <div class="form-check form-switch mb-1">
                                <input class="form-check-input" type="checkbox" name="allow_negative_cash_bank"
                                       value="1" id="negCash" role="switch"
                                       {{ $settings['allow_negative_cash_bank']==='1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="negCash">
                                    Allow Negative Cash Balance
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                When <strong>OFF</strong> (recommended): any transaction that would make a
                                Cash account balance negative is blocked at save time. Bank accounts are not affected.
                            </div>
                        </div>
                    </div>

                    {{-- Voucher Approval --}}
                    <div class="col-12">
                        <div class="p-3 rounded-2 border bg-light">
                            <div class="form-check form-switch mb-1">
                                <input class="form-check-input" type="checkbox" name="voucher_approval_required"
                                       value="1" id="approvalReq" role="switch"
                                       {{ $settings['voucher_approval_required']==='1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="approvalReq">
                                    Use Voucher Approval Process
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                When <strong>ON</strong>: vouchers are saved as Draft and require approval before posting.
                                When <strong>OFF</strong>: clicking Save on a voucher posts it directly to the ledger.
                            </div>
                        </div>
                    </div>

                    {{-- Multi-Currency --}}
                    <div class="col-12">
                        <div class="p-3 rounded-2 border bg-light">
                            <div class="form-check form-switch mb-1">
                                <input class="form-check-input" type="checkbox" name="use_multi_currency"
                                       value="1" id="multiCurrency" role="switch"
                                       {{ ($settings['use_multi_currency'] ?? '0') === '1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="multiCurrency">
                                    Use Multi-Currency
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                When <strong>ON</strong>: foreign currency fields appear on transactions,
                                exchange rate management is enabled, and multi-currency reports
                                (Unrealised Gains/Losses, Currency Revaluation) become available.
                                When <strong>OFF</strong>: all currency-related features are hidden.
                            </div>
                        </div>
                    </div>

                    {{-- Fixed Assets Module --}}
                    <div class="col-12">
                        <div class="p-3 rounded-2 border bg-light">
                            <div class="form-check form-switch mb-1">
                                <input class="form-check-input" type="checkbox" name="use_fixed_assets_module"
                                       value="1" id="useFixedAssetsModule" role="switch"
                                       {{ ($settings['use_fixed_assets_module'] ?? '0') === '1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="useFixedAssetsModule">
                                    Use Fixed Assets Module
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                When <strong>ON</strong>: asset register, categories, and depreciation screens appear
                                under the Fixed Assets menu. Track purchase cost, useful life, accumulated depreciation,
                                and run monthly straight-line depreciation.
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

    {{-- Cheque Processing --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-bank me-2 text-primary"></i>Cheque Processing
            </div>
            <div class="card-body">
                <p class="text-muted mb-3" style="font-size:13px">Cheque printing and register options.</p>
                <div class="row g-3">
                    <div class="col-12">
                        <div class="p-3 rounded-2 border bg-light">
                            <div class="form-check mb-1">
                                <input class="form-check-input" type="checkbox" name="use_cheque_printing"
                                       value="1" id="useChequePrinting"
                                       {{ ($settings['use_cheque_printing'] ?? '0') === '1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="useChequePrinting">
                                    Use Cheque Printing
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                Design cheque layouts to match your bank's cheque leaf and print Payee, Date,
                                Amount and Amount in Words onto pre-printed cheques. Adds a Cheque Format option
                                to the listing section and a Cheque Print queue to the transaction section.
                            </div>
                        </div>
                    </div>
                    <div class="col-12">
                        <div class="p-3 rounded-2 border bg-light">
                            <div class="form-check mb-1">
                                <input class="form-check-input" type="checkbox" name="use_cheque_register"
                                       value="1" id="useChequeRegister"
                                       {{ ($settings['use_cheque_register'] ?? '0') === '1' ? 'checked' : '' }}>
                                <label class="form-check-label fw-semibold" for="useChequeRegister">
                                    Use Cheque Register
                                </label>
                            </div>
                            <div class="text-muted" style="font-size:12px">
                                Track cheque books with bank, branch, account number, book number and leaf range.
                                Adds a Cheque Books option to the listing section, a leaf-number selector to the
                                Cheque Print queue, and Cheque Register and Void Cheque Register reports.
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="d-flex gap-3 justify-content-end">
            <a href="{{ route('dashboard') }}" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary px-4" id="saveBtn">
                <i class="bi bi-check-circle me-1"></i>Save Accounting Settings
            </button>
        </div>
    </div>
</div>
</form>
@endsection
@push('scripts')
<script>
document.getElementById('accForm').addEventListener('submit', function() {
    var btn = document.getElementById('saveBtn');
    btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Saving...';
    btn.disabled = true;
});
</script>
@endpush
