<?php
/**
 * @var array $staffProfiles
 * @var string|null $message
 * @var string|null $error
 * @var string $csrf
 */
?>

<?php
$total = $stats['total'] ?? 0;
$active = $stats['active'] ?? 0;
// $csrf already provided
?>

<div class="row gy-3 mb-3">
    <div class="col-md-4">
        <div class="card summary-card shadow-sm border-0">
            <div class="card-body">
                <div class="text-muted text-uppercase small mb-1">Jumlah Pekerja</div>
                <div class="display-6 fw-semibold text-primary"><?= (int) $total ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card summary-card shadow-sm border-0">
            <div class="card-body">
                <div class="text-muted text-uppercase small mb-1">Pekerja Aktif</div>
                <div class="display-6 fw-semibold text-success"><?= (int) $active ?></div>
            </div>
        </div>
    </div>
</div>

<?php if (!empty($message)): ?>
    <div class="alert alert-success"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>
<?php if (!empty($error)): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>

<div class="card employee-table-card shadow-sm border-0">
    <div class="card-header d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
        <div>
            <h4 class="card-title mb-0">Senarai Pekerja</h4>
            <small class="text-muted">Pengurusan pekerja aktif & maklumat profil</small>
        </div>
        <div class="d-flex flex-column flex-md-row gap-2 w-100 w-md-auto">
            <form method="get" action="index.php" class="d-flex gap-2 align-items-end w-100">
                <input type="hidden" name="route" value="admin/employees">
                <div class="flex-grow-1">
                    <label class="form-label text-white-50 mb-1">Lokasi</label>
                    <select name="location" class="form-select form-select-sm">
                        <option value="">Semua Lokasi</option>
                        <?php foreach ($locations as $loc): ?>
                            <option value="<?= htmlspecialchars($loc) ?>" <?= ($filterLocation ?? '') === $loc ? 'selected' : '' ?>>
                                <?= htmlspecialchars($loc) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button class="btn btn-light btn-sm mt-3">Tapis</button>
            </form>
            <a class="btn btn-primary mt-3 mt-md-0" href="index.php?route=admin/employees/create">+ Pendaftaran Pekerja</a>
        </div>
    </div>
    <div class="card-body p-0">
        <?php if (empty($staffProfiles)): ?>
            <div class="p-4 text-center text-muted">Belum ada pekerja berdaftar.</div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-modern align-middle mb-0">
                    <thead>
                    <tr>
                        <th>Nama</th>
                        <th>Telefon</th>
                        <th>Lokasi</th>
                        <th>Tarikh Lantikan</th>
                        <th>Cuti Semasa</th>
                        <th>Cuti Dibenarkan</th>
                        <th>Baki Cuti</th>
                        <th class="text-center">Tindakan</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($staffProfiles as $staff): ?>
                        <?php
                        $usedLeave = (int) ($staff['used_leave'] ?? 0);
                        $allowedLeave = (int) ($staff['allowed_leave'] ?? 0);
                        $balanceLeave = max($allowedLeave - $usedLeave, 0);
                        ?>
                        <tr>
                            <td>
                                <div class="fw-semibold"><?= htmlspecialchars($staff['name']) ?></div>
                                <small class="text-muted">ID: <?= htmlspecialchars($staff['login_id']) ?></small>
                            </td>
                            <td><span class="badge bg-light text-dark"><?= htmlspecialchars($staff['phone'] ?? '-') ?></span></td>
                            <td><?= htmlspecialchars($staff['location'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($staff['hire_date'] ?? '-') ?></td>
                            <td><span class="badge bg-info-subtle text-info"><?= $usedLeave ?> hari</span></td>
                            <td><span class="badge bg-primary-subtle text-primary"><?= $allowedLeave ?> hari</span></td>
                            <td><span class="badge bg-success-subtle text-success"><?= $balanceLeave ?> hari</span></td>
                            <td class="text-center">
                                <div class="btn-group btn-group-sm" role="group">
                            <button
                                type="button"
                                class="btn btn-outline-secondary"
                                data-bs-toggle="modal"
                                data-bs-target="#viewEmployeeModal"
                                data-name="<?= htmlspecialchars($staff['name'], ENT_QUOTES) ?>"
                                data-ic="<?= htmlspecialchars($staff['ic_no'], ENT_QUOTES) ?>"
                                data-email="<?= htmlspecialchars($staff['email'] ?? '', ENT_QUOTES) ?>"
                                data-phone="<?= htmlspecialchars($staff['phone'] ?? '', ENT_QUOTES) ?>"
                                data-location="<?= htmlspecialchars($staff['location'] ?? '', ENT_QUOTES) ?>"
                                data-address="<?= htmlspecialchars($staff['address'] ?? '', ENT_QUOTES) ?>"
                                data-bank="<?= htmlspecialchars($staff['bank_name'] ?? '', ENT_QUOTES) ?>"
                                data-account="<?= htmlspecialchars($staff['account_no'] ?? '', ENT_QUOTES) ?>"
                                data-hire-date="<?= htmlspecialchars($staff['hire_date'] ?? '', ENT_QUOTES) ?>"
                                data-login="<?= htmlspecialchars($staff['login_id'] ?? '', ENT_QUOTES) ?>"
                                data-allowed="<?= $allowedLeave ?>"
                                data-used="<?= $usedLeave ?>"
                                data-balance="<?= $balanceLeave ?>"
                            >
                                Profil
                            </button>
                            <button
                                type="button"
                                class="btn btn-outline-primary"
                                data-bs-toggle="modal"
                                data-bs-target="#editEmployeeModal"
                                data-user-id="<?= (int) $staff['user_id'] ?>"
                                data-name="<?= htmlspecialchars($staff['name'], ENT_QUOTES) ?>"
                                data-email="<?= htmlspecialchars($staff['email'] ?? '', ENT_QUOTES) ?>"
                                data-phone="<?= htmlspecialchars($staff['phone'] ?? '', ENT_QUOTES) ?>"
                                data-location="<?= htmlspecialchars($staff['location'] ?? '', ENT_QUOTES) ?>"
                                data-bank="<?= htmlspecialchars($staff['bank_name'] ?? '', ENT_QUOTES) ?>"
                                data-account="<?= htmlspecialchars($staff['account_no'] ?? '', ENT_QUOTES) ?>"
                                data-hire-date="<?= htmlspecialchars($staff['hire_date'] ?? '', ENT_QUOTES) ?>"
                                data-address="<?= htmlspecialchars($staff['address'] ?? '', ENT_QUOTES) ?>"
                                        data-allowed="<?= $allowedLeave ?>"
                            >
                                Edit
                            </button>
                                    <form method="post" action="index.php?route=admin/employees/delete" onsubmit="return confirm('Padam pekerja ini? Akaun login juga akan dipadam.');">
                                        <?= $csrf ?>
                                        <input type="hidden" name="user_id" value="<?= (int) $staff['user_id'] ?>">
                                        <button class="btn btn-outline-danger" type="submit">Padam</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Modal View Pekerja -->
<div class="modal fade" id="viewEmployeeModal" tabindex="-1" aria-labelledby="viewEmployeeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="viewEmployeeModalLabel">Profil Pekerja</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <dl class="row mb-0">
                    <dt class="col-sm-4">Nama</dt>
                    <dd class="col-sm-8" id="viewEmployeeName"></dd>
                    <dt class="col-sm-4">ID Login</dt>
                    <dd class="col-sm-8" id="viewEmployeeLogin"></dd>
                    <dt class="col-sm-4">No. IC</dt>
                    <dd class="col-sm-8" id="viewEmployeeIc"></dd>
                    <dt class="col-sm-4">Email</dt>
                    <dd class="col-sm-8" id="viewEmployeeEmail"></dd>
                    <dt class="col-sm-4">Telefon</dt>
                    <dd class="col-sm-8" id="viewEmployeePhone"></dd>
                    <dt class="col-sm-4">Lokasi</dt>
                    <dd class="col-sm-8" id="viewEmployeeLocation"></dd>
                    <dt class="col-sm-4">Alamat</dt>
                    <dd class="col-sm-8" id="viewEmployeeAddress"></dd>
                    <dt class="col-sm-4">Bank</dt>
                    <dd class="col-sm-8" id="viewEmployeeBank"></dd>
                    <dt class="col-sm-4">No. Akaun</dt>
                    <dd class="col-sm-8" id="viewEmployeeAccount"></dd>
                    <dt class="col-sm-4">Cuti Semasa</dt>
                    <dd class="col-sm-8" id="viewEmployeeUsedLeave"></dd>
                    <dt class="col-sm-4">Cuti Dibenarkan</dt>
                    <dd class="col-sm-8" id="viewEmployeeAllowedLeave"></dd>
                    <dt class="col-sm-4">Baki Cuti</dt>
                    <dd class="col-sm-8" id="viewEmployeeBalanceLeave"></dd>
                    <dt class="col-sm-4">Tarikh Lantikan</dt>
                    <dd class="col-sm-8" id="viewEmployeeHireDate"></dd>
                </dl>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal Edit Pekerja -->
<div class="modal fade" id="editEmployeeModal" tabindex="-1" aria-labelledby="editEmployeeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <form method="post" action="index.php?route=admin/employees/update">
                <?= $csrf ?>
                <input type="hidden" name="user_id" id="editEmployeeUserId">
                <div class="modal-header">
                    <h5 class="modal-title" id="editEmployeeModalLabel">Edit Pekerja</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Nama Pekerja</label>
                            <input type="text" class="form-control" name="name" id="editEmployeeName" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Email</label>
                            <input type="email" class="form-control" name="email" id="editEmployeeEmail">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">No. Telefon</label>
                            <input type="text" class="form-control" name="phone" id="editEmployeePhone">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Lokasi Bekerja</label>
                            <?php if (!empty($locations)): ?>
                                <select class="form-select" name="location" id="editEmployeeLocation" required>
                                    <option value="">Pilih lokasi</option>
                                    <?php foreach ($locations as $loc): ?>
                                        <option value="<?= htmlspecialchars($loc) ?>"><?= htmlspecialchars($loc) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            <?php else: ?>
                                <input type="text" class="form-control" name="location" id="editEmployeeLocation" required>
                                <small class="text-muted">Tiada lokasi tersedia. Tambah di Tetapan Syarikat.</small>
                            <?php endif; ?>
                        </div>
                        <div class="col-12">
                            <label class="form-label">Alamat</label>
                            <textarea class="form-control" name="address" id="editEmployeeAddress" rows="2"></textarea>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Bank</label>
                            <input type="text" class="form-control" name="bank_name" id="editEmployeeBank">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">No. Akaun</label>
                            <input type="text" class="form-control" name="account_no" id="editEmployeeAccount">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Jumlah Cuti Dibenarkan (hari)</label>
                            <input type="number" class="form-control" name="allowed_leave" id="editEmployeeAllowedLeave" min="0">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Tarikh Lantikan</label>
                            <input type="date" class="form-control" name="hire_date" id="editEmployeeHireDate">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>



