@extends('layouts.app')
@section('title', 'Voucher Numbering')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Voucher Numbering</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Voucher Numbering</h1>
        <p class="page-subtitle">Configure how each document type is numbered</p>
    </div>
</div>

<div class="alert alert-info d-flex gap-2 align-items-start mb-4" style="font-size:13.5px">
    <i class="bi bi-info-circle fs-5 flex-shrink-0 mt-1"></i>
    <div>Click <strong>Configure</strong> on any row to set prefix, suffix, padding, year/month inclusion and auto-reset rules.
    Numbers are generated atomically — no duplicates even under concurrent load.</div>
</div>

@php
    $groups = [
        'Accounting Vouchers' => ['cash_payment','bank_payment','cash_receipt','bank_receipt','contra','journal'],
        'Accounts Receivable' => ['quotation','sales_order','delivery_note','sales_invoice','sales_return','cash_sale','receive_payment'],
        'Accounts Payable'    => ['purchase_req','purchase_order','grn','bill','bill_payment'],
        'Inventory & Mfg'     => ['stock_adjustment','warehouse_transfer','production_order'],
    ];
@endphp

@foreach($groups as $groupName => $types)
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-hash me-2 text-primary"></i>{{ $groupName }}
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>Document Type</th>
                        <th>Prefix</th>
                        <th>Format</th>
                        <th>Next Number</th>
                        <th>Reset</th>
                        <th>Number On</th>
                        <th style="width:110px">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($types as $type)
                    @if(isset($merged[$type]))
                    @php $item = $merged[$type]; $s = $item['setting']; @endphp
                    <tr>
                        <td class="fw-semibold" style="font-size:13.5px">{{ $item['label'] }}</td>
                        <td>
                            @if($s && $s->prefix)
                                <code style="font-size:12px">{{ $s->prefix }}</code>
                            @else
                                <span class="text-muted" style="font-size:12px">None</span>
                            @endif
                        </td>
                        <td style="font-size:12px">
                            @if($s)
                                @if($s->include_year) <span class="badge bg-light text-dark border me-1">Year</span> @endif
                                @if($s->include_month) <span class="badge bg-light text-dark border me-1">Month</span> @endif
                                <span class="text-muted">{{ str_repeat('0', $s->padding_digits) }}</span>
                                @if($s->suffix) <code style="font-size:11px">{{ $s->suffix }}</code> @endif
                            @else
                                <span class="text-muted">Default</span>
                            @endif
                        </td>
                        <td>
                            <code class="text-primary" style="font-size:12.5px">{{ $item['preview'] }}</code>
                        </td>
                        <td style="font-size:12px">
                            @if($s)
                                @php $resets=['never'=>'Never','yearly'=>'Yearly','monthly'=>'Monthly']; @endphp
                                <span class="badge bg-light text-dark">{{ $resets[$s->reset_period] ?? 'Never' }}</span>
                            @else
                                <span class="text-muted">Never</span>
                            @endif
                        </td>
                        <td style="font-size:12px">
                            @if($s)
                                <span class="badge {{ $s->number_on === 'draft' ? 'bg-light text-dark' : '' }}"
                                      style="{{ $s->number_on === 'approval' ? 'background:#cfe2ff;color:#084298' : '' }}">
                                    {{ ucfirst($s->number_on) }}
                                </span>
                            @else
                                <span class="text-muted">Draft</span>
                            @endif
                        </td>
                        <td>
                            @if(auth()->user()->hasPermission('settings','edit'))
                            <a href="{{ route('admin.voucher-numbering.edit', $type) }}"
                               class="btn btn-outline-primary btn-sm">
                                <i class="bi bi-pencil me-1"></i>Configure
                            </a>
                            @endif
                        </td>
                    </tr>
                    @endif
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endforeach
@endsection
