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

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

<?php
$statusClass = match ($employee['status']) {
    'active'     => 'active',
    'on_leave'   => 'on_leave',
    'terminated' => 'terminated',
    default      => 'inactive',
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent'   => 'Employee Profile',
    'workspaceTitle'        => $employee['full_name'],
    'workspaceDesc'         => 'Comprehensive personnel dossier including identity verification, departmental alignment, emergency contacts, and payroll routing information.',
    'workspaceAction'       => [
        'href'  => base_url('employees/' . $employee['id'] . '/edit'),
        'label' => 'Edit Record',
        'icon'  => 'fa-solid fa-pen',
    ],
]) ?>

<div class="enterprise-profile-grid">
    <aside>
        <div class="enterprise-profile-card">
            <?php if (! empty($employee['photo_path'])): ?>
                <img src="<?= base_url($employee['photo_path']) ?>" class="enterprise-profile-card__photo" alt="">
            <?php else: ?>
                <span class="enterprise-profile-card__photo enterprise-profile-card__photo--placeholder">
                    <?= esc(strtoupper(substr($employee['full_name'], 0, 1))) ?>
                </span>
            <?php endif; ?>

            <h2 class="enterprise-profile-card__name"><?= esc($employee['full_name']) ?></h2>
            <div class="enterprise-profile-card__role"><?= esc($employee['job_title'] ?: '—') ?></div>
            <div class="enterprise-profile-card__id"><?= esc($employee['employee_number']) ?></div>

            <span class="enterprise-status-pill enterprise-status-pill--<?= esc($statusClass) ?> mb-3">
                <?= esc(ucwords(str_replace('_', ' ', $employee['status']))) ?>
            </span>

            <form action="<?= base_url('employees/' . $employee['id'] . '/photo') ?>" method="post" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <input type="file" name="photo" accept="image/jpeg,image/png,image/webp" class="form-control form-control-sm mb-2">
                <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Upload Photo</button>
            </form>
        </div>
    </aside>

    <div>
        <article class="enterprise-detail-card">
            <h3 class="enterprise-detail-card__title">Work Information</h3>
            <div class="enterprise-detail-list">
                <dl class="enterprise-detail-item"><dt>Department</dt><dd><?= esc($deptName) ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Branch</dt><dd><?= esc($branchName) ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Employment Type</dt><dd><?= esc(ucwords(str_replace('_', ' ', $employee['employment_type']))) ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Hire Date</dt><dd><?= esc($employee['hire_date'] ?: '—') ?></dd></dl>
            </div>
        </article>

        <article class="enterprise-detail-card">
            <h3 class="enterprise-detail-card__title">Identity & Contact</h3>
            <div class="enterprise-detail-list">
                <dl class="enterprise-detail-item"><dt>CPR Number</dt><dd><?= esc($employee['cpr_number'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Nationality</dt><dd><?= esc($employee['nationality'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Date of Birth</dt><dd><?= esc($employee['date_of_birth'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Gender</dt><dd><?= esc($employee['gender'] ? ucfirst($employee['gender']) : '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Marital Status</dt><dd><?= esc($employee['marital_status'] ? ucfirst($employee['marital_status']) : '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Email</dt><dd><?= esc($employee['email'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Phone</dt><dd><?= esc($employee['phone'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Mobile</dt><dd><?= esc($employee['mobile'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item enterprise-detail-item--full"><dt>Address</dt><dd><?= esc($employee['address'] ?: '—') ?><?= $employee['city'] ? ', ' . esc($employee['city']) : '' ?><?= $employee['country'] ? ', ' . esc($employee['country']) : '' ?></dd></dl>
            </div>
        </article>

        <article class="enterprise-detail-card">
            <h3 class="enterprise-detail-card__title">Emergency & Payroll</h3>
            <div class="enterprise-detail-list">
                <dl class="enterprise-detail-item enterprise-detail-item--full"><dt>Emergency Contact</dt><dd><?= esc($employee['emergency_contact_name'] ?: '—') ?><?= $employee['emergency_contact_phone'] ? ' (' . esc($employee['emergency_contact_phone']) . ')' : '' ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Bank</dt><dd><?= esc($employee['bank_name'] ?: '—') ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Account (IBAN)</dt><dd><span class="enterprise-data-table__mono"><?= esc($employee['bank_account'] ?: '—') ?></span></dd></dl>
                <dl class="enterprise-detail-item enterprise-detail-item--full"><dt>Notes</dt><dd><?= esc($employee['notes'] ?: '—') ?></dd></dl>
            </div>
        </article>

        <?php if (! empty($employee['cpr_document_path'])): ?>
        <article class="enterprise-detail-card">
            <h3 class="enterprise-detail-card__title">Identity Documentation</h3>
            <a href="<?= base_url($employee['cpr_document_path']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary">
                <i class="fa-solid fa-file me-1"></i>View CPR Document
            </a>
        </article>
        <?php endif; ?>

        <article class="enterprise-detail-card">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h3 class="enterprise-detail-card__title mb-0">Assigned Assets</h3>
                <a href="<?= base_url('assets/assignments?employee_id=' . $employee['id']) ?>" class="btn btn-sm btn-outline-secondary">
                    <i class="fa-solid fa-hand-holding me-1"></i>Manage Assets
                </a>
            </div>

            <?php if (count($assets) === 0): ?>
                <p class="text-muted mb-3">No assets assigned to this employee yet.</p>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Asset</th>
                                <th>Identifier</th>
                                <th>Qty</th>
                                <th>Assigned</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($assets as $a): ?>
                                <tr>
                                    <td>
                                        <?= esc($a['asset_name']) ?>
                                        <br><small class="text-muted"><?= esc($a['type_name']) ?></small>
                                    </td>
                                    <td><span class="enterprise-data-table__mono"><?= esc($a['identifier'] ?: '—') ?></span></td>
                                    <td><?= number_format((float) $a['quantity'], $a['quantity'] == (int) $a['quantity'] ? 0 : 2) ?></td>
                                    <td><?= esc(date('M j, Y', strtotime($a['assigned_date']))) ?></td>
                                    <td>
                                        <span class="enterprise-status-pill enterprise-status-pill--<?= $a['status'] === 'assigned' ? 'active' : 'inactive' ?>">
                                            <?= esc(ucfirst($a['status'])) ?>
                                        </span>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>

            <details class="mt-3">
                <summary class="btn btn-sm btn-primary">Quick Assign Asset</summary>
                <form action="<?= base_url('assets/assignments') ?>" method="post" class="mt-3">
                    <?= csrf_field() ?>
                    <input type="hidden" name="employee_id" value="<?= $employee['id'] ?>">
                    <input type="hidden" name="redirect_to" value="<?= base_url('employees/' . $employee['id']) ?>">
                    <div class="row g-2">
                        <div class="col-md-6">
                            <select name="asset_type_id" class="form-select form-select-sm" required>
                                <option value="">Asset type...</option>
                                <?php foreach ($assetTypes as $t): ?>
                                    <option value="<?= $t['id'] ?>"><?= esc($t['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <input type="text" name="asset_name" class="form-control form-control-sm" placeholder="Item name" required>
                        </div>
                        <div class="col-md-6">
                            <input type="text" name="identifier" class="form-control form-control-sm" placeholder="Serial / IMEI (optional)">
                        </div>
                        <div class="col-md-3">
                            <input type="number" name="quantity" class="form-control form-control-sm" value="1" min="0.01" step="0.01">
                        </div>
                        <div class="col-md-3">
                            <input type="date" name="assigned_date" class="form-control form-control-sm" value="<?= date('Y-m-d') ?>" required>
                        </div>
                        <div class="col-12">
                            <button type="submit" class="btn btn-sm btn-primary">Assign</button>
                        </div>
                    </div>
                </form>
            </details>
        </article>

        <article class="enterprise-detail-card">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h3 class="enterprise-detail-card__title mb-0">Employee Documents</h3>
                <a href="<?= base_url('documents?employee_id=' . $employee['id']) ?>" class="btn btn-sm btn-outline-secondary">
                    <i class="fa-solid fa-folder-open me-1"></i>Manage Documents
                </a>
            </div>

            <?php if (count($documents) === 0): ?>
                <p class="text-muted mb-3">No documents uploaded for this employee yet.</p>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Document</th>
                                <th>Number</th>
                                <th>Expiry</th>
                                <th>Versions</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($documents as $doc): ?>
                                <tr>
                                    <td><?= esc($doc['type_name']) ?></td>
                                    <td><span class="enterprise-data-table__mono"><?= esc($doc['document_number'] ?: '—') ?></span></td>
                                    <td><?= esc($doc['expiry_date'] ? date('M j, Y', strtotime($doc['expiry_date'])) : '—') ?></td>
                                    <td><?= (int) $doc['version_count'] ?></td>
                                    <td class="text-end">
                                        <a href="<?= base_url('documents/' . $doc['id'] . '/download') ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-download"></i></a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>

            <details class="mt-3">
                <summary class="btn btn-sm btn-primary">Quick Upload Document</summary>
                <form action="<?= base_url('documents') ?>" method="post" enctype="multipart/form-data" class="mt-3">
                    <?= csrf_field() ?>
                    <input type="hidden" name="employee_id" value="<?= $employee['id'] ?>">
                    <input type="hidden" name="redirect_to" value="<?= base_url('employees/' . $employee['id']) ?>">
                    <div class="row g-2">
                        <div class="col-md-6">
                            <select name="document_type_id" class="form-select form-select-sm" required>
                                <option value="">Document type...</option>
                                <?php foreach ($documentTypes as $t): ?>
                                    <option value="<?= $t['id'] ?>"><?= esc($t['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <input type="text" name="document_number" class="form-control form-control-sm" placeholder="Document number">
                        </div>
                        <div class="col-md-6">
                            <input type="date" name="expiry_date" class="form-control form-control-sm" placeholder="Expiry">
                        </div>
                        <div class="col-md-6">
                            <input type="file" name="document_file" class="form-control form-control-sm" accept=".pdf,.jpg,.jpeg,.png,.webp" required>
                        </div>
                        <div class="col-12">
                            <button type="submit" class="btn btn-sm btn-primary">Upload</button>
                        </div>
                    </div>
                </form>
            </details>
        </article>
    </div>
</div>

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