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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Shift Planning',
    'workspaceTitle'        => 'Shift Planning',
    'workspaceDesc'         => 'Plan attendance shifts for employees only — assign default shifts with effective dates.',
]) ?>

<?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-clock-rotate-left"></i>
                <h2 class="enterprise-settings-card__title">Plan shift</h2>
            </div>
            <form action="<?= base_url('scheduling/shifts') ?>" method="post" novalidate>
                <?= csrf_field() ?>
                <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">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="<?= (int) $s['id'] ?>" <?= (int) old('shift_id') === (int) $s['id'] ? 'selected' : '' ?>>
                                    <?= esc($s['name']) ?> (<?= esc($s['code']) ?>) — <?= esc(substr((string) $s['start_time'], 0, 5)) ?>–<?= esc(substr((string) $s['end_time'], 0, 5)) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?php if (count($shifts) === 0): ?>
                            <div class="form-text">No active shifts. Create them under Attendance → Shifts.</div>
                        <?php endif; ?>
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Effective from <span class="text-danger">*</span></label>
                        <input type="date" name="effective_from" class="form-control" required
                               value="<?= esc(old('effective_from', date('Y-m-d'))) ?>">
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <label class="enterprise-field-label">Effective to</label>
                        <input type="date" name="effective_to" class="form-control" value="<?= esc(old('effective_to')) ?>">
                    </div>
                </div>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary" <?= count($shifts) === 0 ? 'disabled' : '' ?>>Save plan</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">Employee shift plans</h2>
            </div>
            <div class="enterprise-data-panel__toolbar">
                <form action="<?= base_url('scheduling/shifts') ?>" method="get">
                    <div class="enterprise-filter-grid">
                        <div style="min-width:200px;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 style="min-width:180px;flex:1;">
                            <label class="enterprise-field-label">Shift</label>
                            <select name="shift_id" class="form-select form-select-sm" onchange="this.form.submit()">
                                <option value="">All shifts</option>
                                <?php foreach ($shifts as $s): ?>
                                    <option value="<?= (int) $s['id'] ?>" <?= (int) ($filters['shift_id'] ?? 0) === (int) $s['id'] ? 'selected' : '' ?>>
                                        <?= esc($s['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>Hours</th>
                                <th>Effective</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['shift_name'] ?? '') ?> <span class="text-muted small">(<?= esc($row['shift_code'] ?? '') ?>)</span></td>
                                    <td class="small"><?= esc(substr((string) ($row['start_time'] ?? ''), 0, 5)) ?>–<?= esc(substr((string) ($row['end_time'] ?? ''), 0, 5)) ?></td>
                                    <td class="small text-muted">
                                        <?= esc($row['effective_from'] ?? '') ?>
                                        →
                                        <?= esc($row['effective_to'] ?: 'Open') ?>
                                    </td>
                                    <td class="text-end">
                                        <form action="<?= base_url('scheduling/shifts/' . $row['id'] . '/remove') ?>" method="post"
                                              onsubmit="return confirm('Remove this shift plan?');">
                                            <?= 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="5" class="text-muted">No shift plans found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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