<div class="hero">
    <div>
        <h2 style="margin:0 0 6px;">Manajemen Pegawai</h2>
        <p style="margin:0; opacity:0.95;">Tambah atau ubah barber yang tersedia.</p>
    </div>
</div>

<div class="panel">
    <div style="display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap;">
        <h3 style="margin:0;">Daftar Pegawai</h3>
        <button class="btn-primary" type="button" onclick="openBarberModal()" style="max-width:180px;">Tambah Baru</button>
    </div>
</div>

<?php
$iconEdit = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4Z"></path></svg>';
$iconDelete = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"></path><path d="M10 11v6"></path><path d="M14 11v6"></path><path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"></path></svg>';
?>
<div class="panel">
    <h3>Daftar Pegawai</h3>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Nama</th><th>Alamat</th><th>Tanggal Bergabung</th><th>Aksi</th></tr>
            </thead>
            <tbody>
                <?php foreach ($barbers as $barber) : ?>
                    <tr>
                        <td><?= esc($barber['name']) ?></td>
                        <td><?= esc($barber['address'] ?? '-') ?></td>
                        <td><?= !empty($barber['join_date']) ? esc(date('d/m/Y', strtotime($barber['join_date']))) : '-' ?></td>
                        <td>
                            <div class="action-cell">
                                <a class="icon-btn icon-edit" href="#" title="Edit" data-id="<?= esc($barber['id']) ?>" data-name="<?= esc($barber['name']) ?>" data-address="<?= esc($barber['address'] ?? '') ?>" data-join-date="<?= esc($barber['join_date'] ?? '') ?>" onclick="openBarberModal(this); return false;"><?= $iconEdit ?></a>
                                <a class="icon-btn icon-delete" href="#" title="Hapus" onclick="confirmDelete('/owner/delete-barber/<?= esc($barber['id']) ?>'); return false;"><?= $iconDelete ?></a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal" id="barberModal" tabindex="-1" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,0.45); z-index:9999; align-items:center; justify-content:center;">
    <div style="background:white; width:min(560px, 92vw); border-radius:16px; padding:20px;">
        <h3 id="barberModalTitle" style="margin-top:0;">Tambah Pegawai</h3>
        <form id="barberForm" method="post" action="/owner/save-barber">
            <input type="hidden" name="id" id="barberId">
            <div class="form-grid">
                <input type="text" name="name" id="barberName" placeholder="Nama Pegawai" required>
                <div>
                    <label for="barberJoinDate" class="muted" style="display:block; font-size:12px; margin-bottom:4px;">Tanggal Bergabung</label>
                    <input type="date" name="join_date" id="barberJoinDate">
                </div>
            </div>
            <div class="form-grid">
                <textarea name="address" id="barberAddress" placeholder="Alamat" rows="3" style="grid-column:1/-1; width:100%; padding:12px; border-radius:12px; border:1px solid var(--border); font-size:14px; font-family:inherit; box-sizing:border-box; resize:vertical; min-height:88px;"></textarea>
            </div>
            <div class="form-grid">
                <button class="btn-primary" type="submit">Simpan</button>
                <button class="btn-secondary" type="button" onclick="closeBarberModal();">Batal</button>
            </div>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
.swal2-container { z-index: 10050 !important; }
</style>
<script>
function openBarberModal(el) {
    const modal = document.getElementById('barberModal');
    const form = document.getElementById('barberForm');
    document.getElementById('barberModalTitle').textContent = el ? 'Edit Pegawai' : 'Tambah Pegawai';
    document.getElementById('barberId').value = el ? el.dataset.id : '';
    document.getElementById('barberName').value = el ? el.dataset.name : '';
    document.getElementById('barberAddress').value = el ? el.dataset.address : '';
    document.getElementById('barberJoinDate').value = el ? el.dataset.joinDate : '';
    form.action = el ? '/owner/update-barber' : '/owner/save-barber';
    modal.style.display = 'flex';
}
function closeBarberModal() { document.getElementById('barberModal').style.display = 'none'; }
function confirmDelete(url) {
    Swal.fire({ title: 'Apakah Anda yakin?', text: 'Data ini akan dihapus permanen.', icon: 'warning', showCancelButton: true, confirmButtonText: 'Ya, hapus', cancelButtonText: 'Batal' }).then((result) => {
        if (result.isConfirmed) {
            fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } }).then(r => r.json()).then(() => { Swal.fire('Berhasil', 'Data Berhasil Dihapus', 'success').then(() => location.reload()); });
        }
    });
}
document.getElementById('barberForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const formData = new FormData(this);
    fetch(this.action, { method: 'POST', body: formData, headers: { 'X-Requested-With': 'XMLHttpRequest' } })
        .then(r => r.json())
        .then(() => { closeBarberModal(); Swal.fire('Berhasil', 'Data Berhasil Disimpan', 'success').then(() => location.reload()); });
});
</script>
