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

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

<?php
$statusPill = static function (string $status): string {
    $class = match ($status) {
        'assigned' => 'active',
        'returned' => 'inactive',
        'lost'     => 'terminated',
        'damaged'  => 'on_leave',
        default    => 'inactive',
    };
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Asset Assignments',
    'workspaceTitle'        => 'Employee Asset Assignments',
    'workspaceDesc'         => 'Issue anything to employees — phones, SIMs, laptops, cleaning supplies, uniforms, vehicles, and more.',
]) ?>

<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-hand-holding"></i>
                <h2 class="enterprise-settings-card__title">Assign Asset</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('assets/assignments') ?>" method="post">
                <?= csrf_field() ?>
                <div class="enterprise-settings-fields enterprise-settings-fields--single">
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Employee <span class="text-danger">*</span></label>
                        <select name="employee_id" class="form-select" required>
                            <option value="">Select employee...</option>
                            <?php foreach ($employees as $e): ?>
                                <option value="<?= $e['id'] ?>" <?= old('employee_id', $employeeId) == $e['id'] ? 'selected' : '' ?>>
                                    <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Asset Type <span class="text-danger">*</span></label>
                        <select name="asset_type_id" class="form-select" required>
                            <option value="">Select type...</option>
                            <?php foreach ($types as $t): ?>
                                <option value="<?= $t['id'] ?>" <?= old('asset_type_id', $assetTypeId) == $t['id'] ? 'selected' : '' ?>>
                                    <?= esc($t['name']) ?> (<?= esc($t['code']) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Item Name <span class="text-danger">*</span></label>
                        <input type="text" name="asset_name" class="form-control" value="<?= esc(old('asset_name')) ?>" required placeholder="e.g. iPhone 14, Mop & bucket set">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Serial / IMEI / Identifier</label>
                        <input type="text" name="identifier" class="form-control" value="<?= esc(old('identifier')) ?>" placeholder="IMEI, SIM number, asset tag">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Quantity</label>
                        <input type="number" name="quantity" class="form-control" step="0.01" min="0.01" value="<?= esc(old('quantity', '1')) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Assigned Date <span class="text-danger">*</span></label>
                        <input type="date" name="assigned_date" class="form-control" value="<?= esc(old('assigned_date', date('Y-m-d'))) ?>" required>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Condition on Assign</label>
                        <input type="text" name="condition_on_assign" class="form-control" value="<?= esc(old('condition_on_assign')) ?>" placeholder="e.g. New, Good">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes')) ?></textarea>
                    </div>
                </div>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary">Assign Asset</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">Assignment Records</h2>
            </div>
            <div class="enterprise-data-panel__toolbar">
                <form action="<?= base_url('assets/assignments') ?>" 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 employees</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">Asset Type</label>
                            <select name="asset_type_id" class="form-select form-select-sm">
                                <option value="">All types</option>
                                <?php foreach ($types as $t): ?>
                                    <option value="<?= $t['id'] ?>" <?= $assetTypeId == $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">
                                <option value="">Any</option>
                                <?php foreach (['assigned', 'returned', 'lost', 'damaged'] as $s): ?>
                                    <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $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('assets/types') ?>" class="btn btn-sm btn-outline-secondary">Asset 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>Asset</th>
                                <th>Identifier</th>
                                <th>Qty</th>
                                <th>Assigned</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($assignments as $a): ?>
                                <tr>
                                    <td>
                                        <span class="enterprise-data-table__primary"><?= esc($a['full_name']) ?></span>
                                        <br><small class="text-muted"><?= esc($a['employee_number']) ?></small>
                                    </td>
                                    <td>
                                        <?= esc($a['asset_name']) ?>
                                        <br><small class="text-muted"><?= esc($a['type_name']) ?> <code><?= esc($a['type_code']) ?></code></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><?= $statusPill($a['status']) ?></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">
                                                <?php if ($a['status'] === 'assigned'): ?>
                                                    <li><a class="dropdown-item" href="<?= base_url('assets/assignments/' . $a['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit</a></li>
                                                    <li>
                                                        <button type="button" class="dropdown-item" data-bs-toggle="modal" data-bs-target="#returnModal<?= $a['id'] ?>">
                                                            <i class="fa-solid fa-rotate-left me-2"></i>Return / Close
                                                        </button>
                                                    </li>
                                                <?php endif; ?>
                                                <li>
                                                    <form action="<?= base_url('assets/assignments/' . $a['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this assignment record?');">
                                                        <?= csrf_field() ?>
                                                        <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i>Delete</button>
                                                    </form>
                                                </li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>

                                <?php if ($a['status'] === 'assigned'): ?>
                                <div class="modal fade" id="returnModal<?= $a['id'] ?>" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <form action="<?= base_url('assets/assignments/' . $a['id'] . '/return') ?>" method="post">
                                                <?= csrf_field() ?>
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Return Asset</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <p class="small text-muted mb-3"><?= esc($a['asset_name']) ?> — <?= esc($a['full_name']) ?></p>
                                                    <div class="mb-3">
                                                        <label class="form-label">Return Date</label>
                                                        <input type="date" name="returned_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Outcome</label>
                                                        <select name="return_status" class="form-select">
                                                            <option value="returned">Returned in good condition</option>
                                                            <option value="damaged">Returned damaged</option>
                                                            <option value="lost">Lost / not returned</option>
                                                        </select>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Condition on Return</label>
                                                        <input type="text" name="condition_on_return" class="form-control" placeholder="e.g. Good, Scratched, Missing">
                                                    </div>
                                                    <div>
                                                        <label class="form-label">Notes</label>
                                                        <textarea name="notes" class="form-control" rows="2"><?= esc($a['notes'] ?? '') ?></textarea>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                                                    <button type="submit" class="btn btn-primary">Confirm Return</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                                <?php endif; ?>
                            <?php endforeach; ?>
                            <?php if (count($assignments) === 0): ?>
                                <tr><td colspan="7" class="text-center text-muted py-5">No asset assignments yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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