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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Assignments',
    'workspaceTitle'        => 'Contract Assignments',
    'workspaceDesc'         => 'Browse and manage all employee-to-contract assignments. Filter by company or employee.',
]) ?>

<?php if (session()->getFlashdata('success')): ?>
    <div class="alert alert-success py-2 small"><?= esc(session()->getFlashdata('success')) ?></div>
<?php endif; ?>
<?php if (session()->getFlashdata('error')): ?>
    <div class="alert alert-danger py-2 small"><?= esc(session()->getFlashdata('error')) ?></div>
<?php endif; ?>
<?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; ?>

<div class="row g-4 mb-4">
    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-user-plus"></i>
                <h2 class="enterprise-settings-card__title">New assignment</h2>
            </div>
            <form action="<?= base_url('scheduling/assignments') ?>" method="post" novalidate>
                <?= csrf_field() ?>
                <div class="enterprise-settings-fields">
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Company</label>
                        <select id="assign-company" class="form-select" onchange="filterContracts(this.value)">
                            <option value="">All / pick contract below</option>
                            <?php foreach ($companies as $c): ?>
                                <option value="<?= (int) $c['id'] ?>"><?= esc($c['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Contract <span class="text-danger">*</span></label>
                        <select name="contract_id" id="assign-contract" class="form-select" required>
                            <option value="">Select contract</option>
                            <?php foreach ($assignableContracts as $ct): ?>
                                <option value="<?= (int) $ct['id'] ?>"
                                        data-company="<?= (int) $ct['company_id'] ?>"
                                        <?= (int) old('contract_id') === (int) $ct['id'] ? 'selected' : '' ?>>
                                    <?= esc(($ct['company_name'] ?? '') . ' — ' . $ct['number'] . ' — ' . $ct['title']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <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="<?= (int) $e['id'] ?>" <?= (int) old('employee_id') === (int) $e['id'] ? 'selected' : '' ?>>
                                    <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Role on contract</label>
                        <input type="text" name="role_on_contract" class="form-control" maxlength="100" value="<?= esc(old('role_on_contract')) ?>">
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Hours allocated</label>
                        <input type="number" name="hours_allocated" class="form-control" step="0.01" min="0" value="<?= esc(old('hours_allocated')) ?>">
                    </div>
                </div>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary">Assign employee</button>
                </div>
            </form>
        </article>
    </div>
    <div class="col-lg-8">
        <div class="enterprise-data-panel">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">All assignments</h2>
            </div>
            <div class="enterprise-data-panel__toolbar">
                <form action="<?= base_url('scheduling/assignments') ?>" method="get">
                    <div class="enterprise-filter-grid">
                        <div style="min-width:160px;flex:1;">
                            <label class="enterprise-field-label">Search</label>
                            <input type="text" name="q" class="form-control form-control-sm"
                                   value="<?= esc($filters['search'] ?? '') ?>" placeholder="Employee, contract, company…">
                        </div>
                        <div style="min-width:180px;flex:1;">
                            <label class="enterprise-field-label">Company</label>
                            <select name="company_id" class="form-select form-select-sm" onchange="this.form.submit()">
                                <option value="">All companies</option>
                                <?php foreach ($companies as $c): ?>
                                    <option value="<?= (int) $c['id'] ?>" <?= (int) ($filters['company_id'] ?? 0) === (int) $c['id'] ? 'selected' : '' ?>>
                                        <?= esc($c['name']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div style="min-width:180px;flex:1;">
                            <label class="enterprise-field-label">Employee</label>
                            <select name="employee_id" class="form-select form-select-sm" onchange="this.form.submit()">
                                <option value="">All employees</option>
                                <?php foreach ($employees as $e): ?>
                                    <option value="<?= (int) $e['id'] ?>" <?= (int) ($filters['employee_id'] ?? 0) === (int) $e['id'] ? 'selected' : '' ?>>
                                        <?= esc($e['full_name']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="d-flex align-items-end">
                            <button type="submit" class="btn btn-sm btn-outline-secondary">Filter</button>
                        </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>Company</th>
                                <th>Contract</th>
                                <th>Role</th>
                                <th>Hours</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($assignments as $row): ?>
                                <tr>
                                    <td>
                                        <span class="enterprise-data-table__primary"><?= esc($row['full_name'] ?? '—') ?></span>
                                        <div class="text-muted small"><?= esc($row['employee_number'] ?? '') ?></div>
                                    </td>
                                    <td><?= esc($row['company_name'] ?? '—') ?></td>
                                    <td>
                                        <span class="enterprise-data-table__primary"><?= esc($row['contract_number'] ?? '') ?></span>
                                        <div class="text-muted small"><?= esc($row['contract_title'] ?? '') ?></div>
                                    </td>
                                    <td><?= esc($row['role_on_contract'] ?: '—') ?></td>
                                    <td><?= $row['hours_allocated'] !== null && $row['hours_allocated'] !== '' ? esc($row['hours_allocated']) : '—' ?></td>
                                    <td class="text-end">
                                        <form action="<?= base_url('scheduling/assignments/' . $row['id'] . '/remove') ?>" method="post"
                                              onsubmit="return confirm('Remove this assignment?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($assignments) === 0): ?>
                                <tr><td colspan="6" class="text-muted">No assignments found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function filterContracts(companyId) {
    var select = document.getElementById('assign-contract');
    if (!select) return;
    var options = select.querySelectorAll('option[data-company]');
    options.forEach(function (opt) {
        var match = !companyId || opt.getAttribute('data-company') === String(companyId);
        opt.hidden = !match;
        if (!match && opt.selected) {
            select.value = '';
        }
    });
}
</script>

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