<?php $this->start('body');
    $rows  = $this->data['rows'] ?? [];
    $group = strtoupper((string)($_SESSION['user_group_name'] ?? ''));
    $canEdit = ($group === 'ADMINISTRATOR');
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-hashtag me-1 text-muted"></i>
            Document numbering · <?= count($rows); ?> configured
        </h5>
        <div class="text-muted" style="font-size:.72rem;">Central atomic allocator. All LPO / IPC / CR / SCRUB / PO / SI / RCP numbers come from here.</div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:140px;">Doc key</th>
                    <th>Label</th>
                    <th style="width:80px;">Prefix</th>
                    <th style="width:60px;">Sep</th>
                    <th style="width:80px;">Year?</th>
                    <th style="width:130px;">Reset</th>
                    <th style="width:70px;" class="text-end">Width</th>
                    <th style="width:80px;" class="text-end">Next #</th>
                    <th style="width:180px;">Preview</th>
                    <?php if ($canEdit): ?><th style="width:70px;" class="text-end">Edit</th><?php endif; ?>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($rows as $r):
                $scope = ['NEVER'=>['#7A8BA1','Never'],'YEARLY'=>['#5B5738','Yearly'],'MONTHLY'=>['#6B3F2A','Monthly']][$r['reset_scope']] ?? ['#94a3b8', $r['reset_scope']];
            ?>
                <tr data-id="<?= (int)$r['c0d']; ?>">
                    <td><code style="font-size:.85rem;color:#334155;background:#F1F5F9;padding:2px 6px;border-radius:3px;"><?= htmlspecialchars($r['doc_key']); ?></code></td>
                    <td class="small">
                        <?= htmlspecialchars($r['label']); ?>
                        <?php if (!empty($r['notes'])): ?><div class="text-muted"><?= htmlspecialchars($r['notes']); ?></div><?php endif; ?>
                    </td>
                    <td><strong><?= htmlspecialchars($r['prefix']); ?></strong></td>
                    <td class="text-muted"><?= htmlspecialchars($r['separator']); ?></td>
                    <td class="small text-muted"><?= (int)$r['include_year'] === 1 ? 'yes ('.$r['year_position'].')' : 'no'; ?></td>
                    <td><span class="badge" style="background:<?= $scope[0]; ?>;color:#fff;"><?= htmlspecialchars($scope[1]); ?></span></td>
                    <td class="text-end small"><?= (int)$r['seq_width']; ?></td>
                    <td class="text-end"><strong><?= (int)$r['next_no']; ?></strong></td>
                    <td><code style="color:#3D5B44;background:#EAF2E8;padding:2px 6px;border-radius:3px;"><?= htmlspecialchars($r['next_preview']); ?></code></td>
                    <?php if ($canEdit): ?>
                    <td class="text-end">
                        <button class="btn btn-sm btn-outline-secondary btn-edit"
                                data-id="<?= (int)$r['c0d']; ?>"
                                data-doc-key="<?= htmlspecialchars($r['doc_key']); ?>"
                                data-label="<?= htmlspecialchars($r['label']); ?>"
                                data-prefix="<?= htmlspecialchars($r['prefix']); ?>"
                                data-separator="<?= htmlspecialchars($r['separator']); ?>"
                                data-seq-width="<?= (int)$r['seq_width']; ?>"
                                data-start-no="<?= (int)$r['start_no']; ?>"
                                data-next-no="<?= (int)$r['next_no']; ?>"
                                data-include-year="<?= (int)$r['include_year']; ?>"
                                data-year-position="<?= htmlspecialchars($r['year_position']); ?>"
                                data-reset-scope="<?= htmlspecialchars($r['reset_scope']); ?>"
                                data-notes="<?= htmlspecialchars($r['notes']); ?>">
                            <i class="fas fa-edit"></i>
                        </button>
                    </td>
                    <?php endif; ?>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
$('.btn-edit').on('click', function () {
    var d = $(this).data();
    var html = ''
      + '<div class="row g-2">'
      +   '<div class="col-12"><label class="form-label" style="font-size:.78rem;">Label</label><input id="dn_label" class="form-control form-control-sm" value="' + (d.label || '') + '"></div>'
      +   '<div class="col-md-4"><label class="form-label" style="font-size:.78rem;">Prefix</label><input id="dn_prefix" class="form-control form-control-sm" value="' + (d.prefix || '') + '"></div>'
      +   '<div class="col-md-2"><label class="form-label" style="font-size:.78rem;">Separator</label><input id="dn_sep" class="form-control form-control-sm" value="' + (d.separator || '-') + '"></div>'
      +   '<div class="col-md-3"><label class="form-label" style="font-size:.78rem;">Seq width</label><input type="number" min="1" max="12" id="dn_width" class="form-control form-control-sm" value="' + (d.seqWidth || 4) + '"></div>'
      +   '<div class="col-md-3"><label class="form-label" style="font-size:.78rem;">Include year</label><select id="dn_iy" class="form-select form-select-sm"><option value="1">Yes</option><option value="0">No</option></select></div>'
      +   '<div class="col-md-6"><label class="form-label" style="font-size:.78rem;">Year position</label><select id="dn_ypos" class="form-select form-select-sm"><option value="AFTER_PREFIX">After prefix (PFX-YYYY-####)</option><option value="AFTER_SEQ">After seq (PFX-####-YYYY)</option><option value="BEFORE_SEQ">Before seq (alias)</option></select></div>'
      +   '<div class="col-md-6"><label class="form-label" style="font-size:.78rem;">Reset scope</label><select id="dn_scope" class="form-select form-select-sm"><option value="NEVER">Never</option><option value="YEARLY">Yearly</option><option value="MONTHLY">Monthly</option></select></div>'
      +   '<div class="col-md-6"><label class="form-label" style="font-size:.78rem;">Start no</label><input type="number" min="1" id="dn_start" class="form-control form-control-sm" value="' + (d.startNo || 1) + '"></div>'
      +   '<div class="col-md-6"><label class="form-label" style="font-size:.78rem;">Next no (careful)</label><input type="number" min="1" id="dn_next" class="form-control form-control-sm" value="' + (d.nextNo || 1) + '"></div>'
      +   '<div class="col-12"><label class="form-label" style="font-size:.78rem;">Notes</label><textarea id="dn_notes" class="form-control form-control-sm" rows="2">' + (d.notes || '') + '</textarea></div>'
      + '</div>';
    $.confirm({
        title: 'Edit numbering · ' + d.docKey,
        content: html,
        boxWidth: '560px', useBootstrap: false, columnClass: 'medium',
        onContentReady: function () {
            $('#dn_iy').val(String(d.includeYear));
            $('#dn_ypos').val(d.yearPosition);
            $('#dn_scope').val(d.resetScope);
        },
        buttons: {
            confirm: { text: 'Save', btnClass: 'btn-primary', action: function () {
                $.post('<?= PROOT; ?>DocNumbers/saveAj', {
                    id:            d.id,
                    label:         $('#dn_label').val(),
                    prefix:        $('#dn_prefix').val(),
                    separator:     $('#dn_sep').val(),
                    seq_width:     $('#dn_width').val(),
                    start_no:      $('#dn_start').val(),
                    next_no:       $('#dn_next').val(),
                    include_year:  $('#dn_iy').val(),
                    year_position: $('#dn_ypos').val(),
                    reset_scope:   $('#dn_scope').val(),
                    notes:         $('#dn_notes').val(),
                }, 'json').done(function (r) {
                    if (r && r.ok) { rsToast('Saved.', 'success'); location.reload(); }
                    else            { rsToast('Failed: ' + (r.msg || 'unknown'), 'error'); }
                });
            }},
            cancel: { text: 'Cancel' }
        }
    });
});
</script>
<?php $this->end(); ?>
