<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$expiryBadge = static function (?string $expiryDate) use ($expiry, $today): string {
    $status = $expiry->expiryStatus($expiryDate, $today);
    $map = [
        'expired'  => ['terminated', 'Expired'],
        'critical' => ['on_leave', 'Expiring soon'],
        'warning'  => ['on_leave', 'Expiring'],
        'ok'       => ['active', 'Valid'],
        'none'     => ['inactive', 'No expiry'],
    ];
    [$class, $label] = $map[$status] ?? ['inactive', '—'];
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Employee Documents',
    'workspaceTitle'        => 'Employee Documents',
    'workspaceDesc'         => 'Passport, CPR, visa, work permit, driving license, contracts, certificates, and medical records.',
    'workspaceAction'       => [
        'href'  => base_url('documents/create'),
        'label' => 'Upload Document',
        'icon'  => 'fa-solid fa-file-arrow-up',
    ],
]) ?>

<div class="row g-4">
    <div class="col-lg-5">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-file-arrow-up"></i>
                <h2 class="enterprise-settings-card__title">Quick Upload</h2>
            </div>
            <?php if (session()->getFlashdata('errors')): ?>
                <div class="alert alert-danger py-2 small">
                    <ul class="mb-0 ps-3">
                        <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                            <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>
            <form action="<?= base_url('documents') ?>" method="post" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <?= $this->include('Modules\Documents\Views\records\_form') ?>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary">Upload Document</button>
                </div>
            </form>
        </article>
    </div>

    <div class="col-lg-7">
        <div class="enterprise-data-panel">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Document Records</h2>
                <p class="enterprise-data-panel__subtitle">Secure storage with version history and expiry tracking.</p>
            </div>
            <div class="enterprise-data-panel__toolbar">
                <form action="<?= base_url('documents') ?>" method="get">
                    <div class="enterprise-filter-grid">
                        <div>
                            <label class="enterprise-field-label">Employee</label>
                            <select name="employee_id" class="form-select form-select-sm">
                                <option value="">All</option>
                                <?php foreach ($employees as $e): ?>
                                    <option value="<?= $e['id'] ?>" <?= $employeeId == $e['id'] ? 'selected' : '' ?>><?= esc($e['full_name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="enterprise-field-label">Type</label>
                            <select name="document_type_id" class="form-select form-select-sm">
                                <option value="">All</option>
                                <?php foreach ($types as $t): ?>
                                    <option value="<?= $t['id'] ?>" <?= $documentTypeId == $t['id'] ? 'selected' : '' ?>><?= esc($t['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="enterprise-field-label">Status</label>
                            <select name="status" class="form-select form-select-sm">
                                <?php foreach (['active', 'archived'] as $s): ?>
                                    <option value="<?= $s ?>" <?= ($filters['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="enterprise-filter-actions">
                            <button type="submit" class="btn btn-sm btn-primary">Filter</button>
                            <a href="<?= base_url('documents/expiry') ?>" class="btn btn-sm btn-outline-warning"><i class="fa-solid fa-clock me-1"></i>Expiry Tracker</a>
                            <a href="<?= base_url('documents/types') ?>" class="btn btn-sm btn-outline-secondary">Types</a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr><th>Employee</th><th>Document</th><th>Number</th><th>Expiry</th><th>Versions</th><th class="text-end">Actions</th></tr>
                        </thead>
                        <tbody>
                            <?php foreach ($documents as $doc): ?>
                                <tr>
                                    <td>
                                        <a href="<?= base_url('employees/' . $doc['employee_id']) ?>" class="enterprise-data-table__primary text-decoration-none"><?= esc($doc['full_name']) ?></a>
                                        <br><small class="text-muted"><?= esc($doc['employee_number']) ?></small>
                                    </td>
                                    <td>
                                        <a href="<?= base_url('documents/' . $doc['id']) ?>" class="text-decoration-none fw-semibold"><?= esc($doc['type_name']) ?></a>
                                        <br><small class="text-muted"><code><?= esc($doc['type_code']) ?></code></small>
                                    </td>
                                    <td><span class="enterprise-data-table__mono"><?= esc($doc['document_number'] ?: '—') ?></span></td>
                                    <td>
                                        <?= $formatDate($doc['expiry_date']) ?>
                                        <br><?= $expiryBadge($doc['expiry_date']) ?>
                                    </td>
                                    <td><?= (int) $doc['version_count'] ?></td>
                                    <td class="text-end enterprise-row-actions">
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown"><i class="fa-solid fa-ellipsis"></i></button>
                                            <ul class="dropdown-menu dropdown-menu-end enterprise-nav-menu">
                                                <li><a class="dropdown-item" href="<?= base_url('documents/' . $doc['id']) ?>"><i class="fa-solid fa-eye me-2"></i>View & History</a></li>
                                                <li><a class="dropdown-item" href="<?= base_url('documents/' . $doc['id'] . '/download') ?>"><i class="fa-solid fa-download me-2"></i>Secure Download</a></li>
                                                <li><a class="dropdown-item" href="<?= base_url('documents/' . $doc['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit / New Version</a></li>
                                                <li><hr class="dropdown-divider"></li>
                                                <li>
                                                    <form action="<?= base_url('documents/' . $doc['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Archive this document?');">
                                                        <?= csrf_field() ?>
                                                        <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-archive me-2"></i>Archive</button>
                                                    </form>
                                                </li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($documents) === 0): ?>
                                <tr><td colspan="6" class="text-center text-muted py-5">No documents uploaded yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
