<?php $this->start('body');
    $rows   = $this->data['rows']          ?? [];
    $counts = $this->data['counts']        ?? [];
    $action = $this->data['action_needed'] ?? [];
    $f      = $this->data['filter']        ?? [];
    $isAdm  = !empty($this->data['is_admin_role']);

    $statusColor = ['CURRENT'=>'#7A9E7E','SUPERSEDED'=>'#94A3B8','EXPIRED'=>'#C48579','PENDING_RENEWAL'=>'#D4A574'];
    $today = date('Y-m-d');
    $in60  = date('Y-m-d', strtotime('+60 days'));
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-folder-open me-2" style="color:#7A8BA1;"></i>Company Documents Library</h5>
        <div class="text-muted small mt-1" style="color:#64748B;">Central store of reusable company certificates &amp; standard documents. Upload once, attach to any tender.</div>
    </div>
    <?php if (UserGroups::can('CompanyDocuments','add')): ?>
        <a href="<?= PROOT; ?>CompanyDocuments/add" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-plus me-1"></i>Upload document</a>
    <?php endif; ?>
</div>

<!-- KPIs ── -->
<div class="row g-3 mb-3">
    <div class="col-md-4"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A8BA1 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;"><i class="fas fa-folder me-1"></i>Total documents</div>
        <div style="font-weight:700;font-size:1.5rem;color:#334155;"><?= (int)($counts['total'] ?? 0); ?></div>
        <div class="text-muted small" style="color:#64748B;font-size:.72rem;"><?= (int)($counts['current'] ?? 0); ?> current &middot; <?= (int)($counts['superseded'] ?? 0); ?> superseded</div>
    </div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #D4A574 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;"><i class="fas fa-hourglass-half me-1" style="color:#D4A574;"></i>Expiring within 60 days</div>
        <div style="font-weight:700;font-size:1.5rem;color:#D4A574;"><?= (int)($counts['expiring60'] ?? 0); ?></div>
        <div class="text-muted small" style="color:#64748B;font-size:.72rem;">click rows below to review</div>
    </div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #C48579 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;"><i class="fas fa-exclamation-triangle me-1" style="color:#C48579;"></i>Already expired</div>
        <div style="font-weight:700;font-size:1.5rem;color:#C48579;"><?= (int)($counts['expired'] ?? 0); ?></div>
        <div class="text-muted small" style="color:#64748B;font-size:.72rem;">renew and upload new versions</div>
    </div></div></div>
</div>

<!-- Action Needed ── -->
<?php if (!empty($action)): ?>
<div class="card border-0 shadow-sm mb-3" style="background:#FEFDF6;border-left:3px solid #D4A574 !important;">
    <div class="card-header bg-white" style="border-bottom:1px solid #EFE7D2;">
        <strong style="color:#334155;"><i class="fas fa-bell me-2" style="color:#D4A574;"></i>Action Needed</strong>
        <span class="text-muted small ms-2" style="color:#64748B;font-size:.72rem;">Expiring soon or already expired</span>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <tbody>
                <?php foreach ($action as $a):
                    $daysToExpiry = round((strtotime($a['expiry_date']) - time()) / 86400);
                    $chipLabel = $daysToExpiry < 0 ? 'Expired ' . abs($daysToExpiry) . ' days ago' : 'Expires in ' . $daysToExpiry . ' days';
                    $chipColor = $daysToExpiry < 0 ? '#C48579' : '#D4A574';
                ?>
                <tr>
                    <td style="width:38%;color:#334155;font-weight:500;"><?= htmlspecialchars($a['doc_name']); ?></td>
                    <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$a['ref_no']); ?></td>
                    <td class="text-end" style="color:<?= $chipColor; ?>;font-size:.78rem;font-weight:500;"><?= $chipLabel; ?></td>
                    <td class="text-end" style="width:1%;"><a href="<?= PROOT; ?>CompanyDocuments/view/<?= (int)$a['c0d']; ?>" class="btn btn-sm" style="background:#fff;border:1px solid #EFE7D2;color:#334155;"><i class="fas fa-eye me-1" style="color:#7A8BA1;"></i>Review</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Filter + full list ── -->
<form method="get" action="<?= PROOT; ?>CompanyDocuments" class="card border-0 shadow-sm mb-3" style="background:#F8FAFC;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-4"><input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="Search name, ref, type…" class="form-control form-control-sm"></div>
            <div class="col-md-2"><select name="status" class="form-select form-select-sm">
                <option value="">All statuses</option>
                <?php foreach (['CURRENT','SUPERSEDED','EXPIRED','PENDING_RENEWAL'] as $s): ?><option value="<?= $s; ?>" <?= ($f['status'] ?? '') === $s ? 'selected' : ''; ?>><?= ucwords(strtolower(str_replace('_',' ', $s))); ?></option><?php endforeach; ?>
            </select></div>
            <div class="col-md-3">
                <label class="form-check-label small" style="color:#334155;"><input type="checkbox" name="expiring" value="1" <?= !empty($f['expiring']) ? 'checked' : ''; ?>> Expiring in 60d</label>
                <label class="form-check-label small ms-3" style="color:#334155;"><input type="checkbox" name="expired" value="1" <?= !empty($f['expired']) ? 'checked' : ''; ?>> Expired only</label>
            </div>
            <div class="col-md-3 text-end"><button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Filter</button></div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#F1F5F9;">
                <tr>
                    <th style="color:#334155;">Type</th>
                    <th style="color:#334155;">Document</th>
                    <th style="color:#334155;">Ref no</th>
                    <th style="color:#334155;">Issued</th>
                    <th style="color:#334155;">Expires</th>
                    <th style="color:#334155;">Status</th>
                    <th style="color:#334155;">Uploaded by</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $sc = $statusColor[$r['status']] ?? '#94A3B8';
                    $expOverdue = !empty($r['expiry_date']) && $r['expiry_date'] < $today;
                    $expSoon    = !empty($r['expiry_date']) && $r['expiry_date'] >= $today && $r['expiry_date'] <= $in60;
                ?>
                <tr>
                    <td class="text-muted small" style="color:#64748B;"><span class="badge" style="background:#F1F5F9;color:#334155;font-size:.62rem;"><?= htmlspecialchars((string)$r['doc_type_code']); ?></span></td>
                    <td style="color:#334155;font-weight:500;"><?= htmlspecialchars($r['doc_name']); ?>
                        <?php if (!empty($r['type_label'])): ?><div style="color:#94A3B8;font-size:.66rem;"><?= htmlspecialchars((string)$r['type_label']); ?></div><?php endif; ?>
                    </td>
                    <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['ref_no']) ?: '—'; ?></td>
                    <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)($r['issue_date'] ?? '—')); ?></td>
                    <td class="text-muted small" style="color:<?= $expOverdue ? '#C48579' : ($expSoon ? '#D4A574' : '#64748B'); ?>;"><?= htmlspecialchars((string)($r['expiry_date'] ?? '—')); ?></td>
                    <td><span class="badge" style="background:<?= $sc; ?>;color:#fff;font-size:.62rem;text-transform:capitalize;"><?= ucwords(strtolower(str_replace('_',' ', $r['status']))); ?></span></td>
                    <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['uploaded_by_name']); ?>
                        <?php if (!empty($r['uploaded_by_role'])): ?><div style="color:#94A3B8;font-size:.66rem;"><?= htmlspecialchars((string)$r['uploaded_by_role']); ?></div><?php endif; ?>
                    </td>
                    <td class="text-end"><a href="<?= PROOT; ?>CompanyDocuments/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-eye me-1" style="color:#7A8BA1;"></i>View</a></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="8" class="text-center py-4" style="color:#94A3B8;">
                        No documents visible to you.
                        <?php if ($isAdm && UserGroups::can('CompanyDocuments','add')): ?>
                            <a href="<?= PROOT; ?>CompanyDocuments/add" style="color:#7A8BA1;font-weight:600;">Upload one</a>.
                        <?php elseif (!$isAdm): ?>
                            <div class="small mt-1" style="color:#94A3B8;">Ask an Administrator, Finance Manager, or Executive to grant your role access.</div>
                        <?php endif; ?>
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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