@extends('layouts.app')

@section('title', __('messages.document_sequences'))

@section('content')
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">Document Numbering Sequences</h5>
        <span class="text-muted fs-xs">Centralized Numbering Engine for Invoices, Receipts, Payments & Journals (Safe Transaction Atomic Increment)</span>
    </div>
</div>

<div class="card-erp">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>Sequence Type</th>
                    <th>Prefix</th>
                    <th>Next Number</th>
                    <th>Digit Length</th>
                    <th>Preview Example</th>
                    <th>Branch Specific</th>
                    <th>Year Specific</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($sequences as $seq)
                    <tr>
                        <td class="fw-bold text-dark text-capitalize">{{ str_replace('_', ' ', $seq->sequence_type) }}</td>
                        <td><span class="badge bg-secondary font-monospace">{{ $seq->prefix ?? '-' }}</span></td>
                        <td class="fw-bold text-primary">{{ $seq->next_number }}</td>
                        <td>{{ $seq->digit_length }} Digits</td>
                        <td>
                            <code class="fw-bold text-success fs-xs">
                                {{ ($seq->prefix ? $seq->prefix . '-' : '') . str_pad($seq->next_number, $seq->digit_length, '0', STR_PAD_LEFT) }}
                            </code>
                        </td>
                        <td>
                            @if($seq->is_branch_specific)
                                <span class="badge bg-info text-dark">Branch Specific</span>
                            @else
                                <span class="text-muted fs-xs">Global</span>
                            @endif
                        </td>
                        <td>
                            @if($seq->is_year_specific)
                                <span class="badge bg-info text-dark">Year Specific</span>
                            @else
                                <span class="text-muted fs-xs">Global</span>
                            @endif
                        </td>
                        <td class="text-end">
                            <button class="btn btn-dense btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editSeqModal{{ $seq->id }}">
                                <i class="bi bi-pencil"></i> Edit
                            </button>
                        </td>
                    </tr>

                    <!-- Edit Sequence Modal -->
                    <div class="modal fade" id="editSeqModal{{ $seq->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form method="POST" action="{{ route('document-sequences.update', $seq->id) }}">
                                    @csrf
                                    @method('PUT')
                                    <div class="modal-header py-2">
                                        <h6 class="modal-title fw-bold">Edit Sequence: {{ str_replace('_', ' ', $seq->sequence_type) }}</h6>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="mb-3">
                                            <label class="form-label small fw-semibold">Prefix (e.g. INV, SI, PUR, REC)</label>
                                            <input type="text" name="prefix" class="form-control form-control-sm" value="{{ $seq->prefix }}">
                                        </div>
                                        <div class="row g-2 mb-3">
                                            <div class="col-6">
                                                <label class="form-label small fw-semibold">Next Number</label>
                                                <input type="number" name="next_number" class="form-control form-control-sm" value="{{ $seq->next_number }}" required>
                                            </div>
                                            <div class="col-6">
                                                <label class="form-label small fw-semibold">Digit Length</label>
                                                <input type="number" name="digit_length" class="form-control form-control-sm" value="{{ $seq->digit_length }}" required>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer py-2">
                                        <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-sm btn-primary">Update Sequence</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                @empty
                    <tr><td colspan="8" class="text-center py-3 text-muted">No document sequences found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
