<?php $this->start('body');
    $doc     = $this->data['doc'] ?? null;
    $catalog = $this->data['catalog'] ?? [];
    $isEdit  = $doc !== null;
    $action  = $isEdit ? PROOT . 'CompanyDocuments/edit/' . (int)$doc['c0d'] : PROOT . 'CompanyDocuments/add';

    // Group catalog by file_cat for the optgroup dropdown
    $byCat = [];
    foreach ($catalog as $c) { $byCat[$c['file_cat']][] = $c; }
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-<?= $isEdit ? 'edit' : 'plus-circle'; ?> me-2" style="color:#7A8BA1;"></i><?= $isEdit ? 'Edit document' : 'Upload company document'; ?></h5>
    <a href="<?= PROOT; ?>CompanyDocuments<?= $isEdit ? '/view/' . (int)$doc['c0d'] : ''; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= $action; ?>" class="card border-0 shadow-sm" style="background:#F8FAFC;">
    <div class="card-body p-3">
        <div class="row g-2">
            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Document type *</label>
                <select name="doc_type_code" required class="form-select form-select-sm">
                    <option value="">— choose type —</option>
                    <?php foreach ($byCat as $cat => $items): ?>
                        <optgroup label="<?= htmlspecialchars(ucwords(strtolower(str_replace('_',' ', $cat)))); ?>">
                            <?php foreach ($items as $it): ?>
                                <option value="<?= htmlspecialchars($it['type_code']); ?>" <?= ($doc['doc_type_code'] ?? '') === $it['type_code'] ? 'selected' : ''; ?>>
                                    <?= htmlspecialchars($it['type_label']); ?><?= (int)$it['is_statutory'] === 1 ? ' ★' : ''; ?>
                                </option>
                            <?php endforeach; ?>
                        </optgroup>
                    <?php endforeach; ?>
                </select>
                <div class="text-muted small mt-1" style="color:#94A3B8;font-size:.7rem;">Types marked ★ are statutory / compliance.</div>
            </div>
            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Document name *</label>
                <input type="text" name="doc_name" required value="<?= htmlspecialchars((string)($doc['doc_name'] ?? '')); ?>" placeholder="e.g. BRELA Certificate of Incorporation 2026" class="form-control form-control-sm">
            </div>

            <div class="col-md-4">
                <label class="form-label" style="color:#334155;">Reference number</label>
                <input type="text" name="ref_no" value="<?= htmlspecialchars((string)($doc['ref_no'] ?? '')); ?>" placeholder="e.g. BRELA-2026-04412" class="form-control form-control-sm">
            </div>
            <div class="col-md-4">
                <label class="form-label" style="color:#334155;">Issue date</label>
                <input type="date" name="issue_date" value="<?= htmlspecialchars((string)($doc['issue_date'] ?? '')); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-4">
                <label class="form-label" style="color:#334155;">Expiry date</label>
                <input type="date" name="expiry_date" value="<?= htmlspecialchars((string)($doc['expiry_date'] ?? '')); ?>" class="form-control form-control-sm">
            </div>

            <?php if ($isEdit): ?>
            <div class="col-md-4">
                <label class="form-label" style="color:#334155;">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <?php foreach (['CURRENT','SUPERSEDED','EXPIRED','PENDING_RENEWAL'] as $s): ?>
                        <option value="<?= $s; ?>" <?= (($doc['status'] ?? 'CURRENT') === $s) ? 'selected' : ''; ?>><?= ucwords(strtolower(str_replace('_',' ', $s))); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <?php endif; ?>

            <div class="col-12">
                <label class="form-label" style="color:#334155;">Notes</label>
                <textarea name="notes" rows="2" class="form-control form-control-sm" placeholder="Optional context — where the original is stored, renewal instructions, etc."><?= htmlspecialchars((string)($doc['notes'] ?? '')); ?></textarea>
            </div>

            <div class="col-12">
                <div class="p-2" style="background:#F1F5F9;border:1px dashed #E2E8F0;border-radius:3px;color:#94A3B8;font-size:.78rem;">
                    <i class="fas fa-info-circle me-1"></i>File-upload UI is deferred to a follow-on slice. For now the document metadata is tracked here; keep the physical PDF in the shared drive and note its path in the Notes field.
                </div>
            </div>
        </div>
    </div>
    <div class="card-footer bg-white d-flex justify-content-between align-items-center" style="border-top:1px solid #E2E8F0;">
        <?php if (!$isEdit): ?>
        <span class="text-muted small" style="color:#64748B;font-size:.74rem;">
            <i class="fas fa-shield-alt me-1" style="color:#7A8BA1;"></i>Default access on save: <strong>Administrator</strong>, <strong>Finance Manager</strong>, <strong>Executive</strong>, plus <strong><?= htmlspecialchars((string)($_SESSION['user_group_name'] ?? 'your role')); ?></strong> (the uploader).
        </span>
        <?php else: ?>
        <span></span>
        <?php endif; ?>
        <div class="d-flex gap-2">
            <a href="<?= PROOT; ?>CompanyDocuments" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;">Cancel</a>
            <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-save me-1"></i><?= $isEdit ? 'Save changes' : 'Save document'; ?></button>
        </div>
    </div>
</form>

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