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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Staff Scheduling',
    'workspaceTitle'        => 'Staff Scheduling',
    'workspaceDesc'         => 'Search companies, open their contracts, and schedule employees onto them.',
]) ?>

<?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="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Find company &amp; contract</h2>
        <p class="enterprise-data-panel__subtitle mb-0">Only employees from the HR directory can be scheduled.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('scheduling/staff') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div style="min-width:200px;flex:1;">
                    <label class="enterprise-field-label">Search companies</label>
                    <input type="text" name="q" class="form-control form-control-sm"
                           value="<?= esc($filters['search'] ?? '') ?>" placeholder="Company name…">
                </div>
                <div style="min-width:240px;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="">Select company</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:280px;flex:1;">
                    <label class="enterprise-field-label">Contract</label>
                    <select name="contract_id" class="form-select form-select-sm" <?= count($contracts) === 0 ? 'disabled' : '' ?> onchange="this.form.submit()">
                        <option value=""><?= count($contracts) === 0 ? 'Select a company first' : 'Select contract' ?></option>
                        <?php foreach ($contracts as $ct): ?>
                            <option value="<?= (int) $ct['id'] ?>" <?= (int) ($filters['contract_id'] ?? 0) === (int) $ct['id'] ? 'selected' : '' ?>>
                                <?= esc($ct['number']) ?> — <?= esc($ct['title']) ?> (<?= esc($ct['status']) ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="d-flex align-items-end">
                    <button type="submit" class="btn btn-sm btn-outline-secondary">Apply</button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php if (($filters['company_id'] ?? 0) > 0 && count($contracts) > 0 && ! $contract): ?>
    <div class="enterprise-data-panel mb-4">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title">Contracts</h2>
        </div>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Number</th>
                            <th>Title</th>
                            <th>Type</th>
                            <th>Status</th>
                            <th>Period</th>
                            <th class="text-end">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($contracts as $ct): ?>
                            <tr>
                                <td><span class="enterprise-data-table__primary"><?= esc($ct['number']) ?></span></td>
                                <td><?= esc($ct['title']) ?></td>
                                <td><?= esc(ucfirst($ct['contract_type'] ?? '')) ?></td>
                                <td><?= esc(ucfirst($ct['status'] ?? '')) ?></td>
                                <td class="small text-muted">
                                    <?= esc($ct['start_date'] ?? '—') ?>
                                    →
                                    <?= esc($ct['end_date'] ?? 'Open') ?>
                                </td>
                                <td class="text-end">
                                    <a class="btn btn-sm btn-outline-primary"
                                       href="<?= base_url('scheduling/staff?company_id=' . (int) $filters['company_id'] . '&contract_id=' . (int) $ct['id']) ?>">
                                        Schedule staff
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endif; ?>

<?php if ($contract): ?>
    <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-user-clock"></i>
                    <h2 class="enterprise-settings-card__title">Schedule employee</h2>
                </div>
                <p class="small text-muted mb-3">
                    Assigning to <strong><?= esc($contract['number']) ?></strong> — <?= esc($contract['title']) ?>
                </p>
                <form action="<?= base_url('scheduling/staff/assign') ?>" method="post" novalidate>
                    <?= csrf_field() ?>
                    <input type="hidden" name="contract_id" value="<?= (int) $contract['id'] ?>">
                    <div class="enterprise-settings-fields">
                        <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')) ?>" placeholder="e.g. Site supervisor">
                        </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">Schedule employee</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">Scheduled employees</h2>
                </div>
                <div class="enterprise-data-panel__body">
                    <div class="table-responsive">
                        <table class="enterprise-data-table">
                            <thead>
                                <tr>
                                    <th>Employee</th>
                                    <th>Role</th>
                                    <th>Hours</th>
                                    <th class="text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($staff 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['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/staff/' . $row['id'] . '/remove') ?>" method="post"
                                                  onsubmit="return confirm('Remove this employee from the contract?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (count($staff) === 0): ?>
                                    <tr><td colspan="4" class="text-muted">No employees scheduled on this contract yet.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>

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