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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Shift Assignments',
    'workspaceTitle'        => 'Employee Shift Assignments',
    'workspaceDesc'         => 'Assign work shifts to directory employees for attendance tracking and auto-punch.',
]) ?>

<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">Assign Shift</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('attendance/shifts/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">Shift <span class="text-danger">*</span></label>
                        <select name="shift_id" class="form-select" required>
                            <option value="">Select shift...</option>
                            <?php foreach ($shifts as $s): ?>
                                <option value="<?= $s['id'] ?>" <?= old('shift_id') == $s['id'] ? 'selected' : '' ?>>
                                    <?= esc($s['name']) ?> (<?= esc(substr($s['start_time'], 0, 5)) ?> – <?= esc(substr($s['end_time'], 0, 5)) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Effective From <span class="text-danger">*</span></label>
                        <input type="date" name="effective_from" class="form-control" value="<?= esc(old('effective_from', date('Y-m-d'))) ?>" required>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label">Effective To</label>
                        <input type="date" name="effective_to" class="form-control" value="<?= esc(old('effective_to')) ?>">
                        <small class="text-muted">Leave blank for ongoing assignment.</small>
                    </div>
                </div>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary">Assign Shift</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">Current Assignments</h2>
            </div>
            <div class="enterprise-data-panel__toolbar">
                <form action="<?= base_url('attendance/shifts/assignments') ?>" method="get">
                    <div class="enterprise-filter-grid">
                        <div>
                            <label class="enterprise-field-label">Filter by 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="<?= $e['id'] ?>" <?= $employeeId == $e['id'] ? 'selected' : '' ?>><?= esc($e['full_name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </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>Shift</th>
                                <th>From</th>
                                <th>To</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['shift_name']) ?> <code><?= esc($a['shift_code']) ?></code></td>
                                    <td><?= esc(date('M j, Y', strtotime($a['effective_from']))) ?></td>
                                    <td><?= $a['effective_to'] ? esc(date('M j, Y', strtotime($a['effective_to']))) : '<span class="text-success">Ongoing</span>' ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($assignments) === 0): ?>
                                <tr><td colspan="4" class="text-center text-muted py-5">No shift assignments yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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