<div class="hero">
    <div>
        <h2 style="margin:0 0 6px;">Manajemen Member</h2>
        <p style="margin:0; opacity:0.95;">Kelola data pelanggan member dan lihat poin yang mereka kumpulkan.</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 Member</h3>
        <button class="btn-primary" type="button" onclick="openMemberModal()" 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 Member</h3>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Nama</th><th>No. HP</th><th>Email</th><th>Poin</th><th>Aksi</th></tr>
            </thead>
            <tbody>
                <?php foreach ($members as $member) : ?>
                    <tr>
                        <td><?= esc($member['name']) ?></td>
                        <td><?= esc($member['phone'] ?? '-') ?></td>
                        <td><?= esc($member['email'] ?? '-') ?></td>
                        <td><span class="pill" style="margin:0;"><?= esc(number_format((int) $member['points'], 0, ',', '.')) ?> poin</span></td>
                        <td>
                            <?php if (($user['role'] ?? 'owner') === 'owner') : ?>
                                <div class="action-cell">
                                    <a class="icon-btn icon-edit" href="#" title="Edit" data-id="<?= esc($member['id']) ?>" data-name="<?= esc($member['name']) ?>" data-phone="<?= esc($member['phone'] ?? '') ?>" data-email="<?= esc($member['email'] ?? '') ?>" data-points="<?= esc($member['points']) ?>" onclick="openMemberModal(this); return false;"><?= $iconEdit ?></a>
                                    <a class="icon-btn icon-delete" href="#" title="Hapus" onclick="confirmDelete('/owner/delete-member/<?= esc($member['id']) ?>'); return false;"><?= $iconDelete ?></a>
                                </div>
                            <?php else : ?>
                                <span class="muted">-</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($members)) : ?>
                    <tr><td colspan="5" class="muted">Belum ada member.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal" id="memberModal" 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="memberModalTitle" style="margin-top:0;">Tambah Member</h3>
        <form id="memberForm" method="post" action="/owner/save-member">
            <input type="hidden" name="id" id="memberId">
            <div class="form-grid">
                <input type="text" name="name" id="memberName" placeholder="Nama Member" required>
                <input type="text" name="phone" id="memberPhone" placeholder="No. HP">
                <input type="email" name="email" id="memberEmail" placeholder="Email (opsional)">
            </div>
            <div class="form-grid" id="memberPointsField" style="display:none;">
                <div>
                    <label for="memberPoints" class="muted" style="display:block; font-size:12px; margin-bottom:4px;">Poin</label>
                    <input type="number" name="points" id="memberPoints" placeholder="Poin" min="0">
                </div>
            </div>
            <div class="form-grid">
                <button class="btn-primary" type="submit">Simpan</button>
                <button class="btn-secondary" type="button" onclick="closeMemberModal();">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 openMemberModal(el) {
    const modal = document.getElementById('memberModal');
    const form = document.getElementById('memberForm');
    document.getElementById('memberModalTitle').textContent = el ? 'Edit Member' : 'Tambah Member';
    document.getElementById('memberId').value = el ? el.dataset.id : '';
    document.getElementById('memberName').value = el ? el.dataset.name : '';
    document.getElementById('memberPhone').value = el ? el.dataset.phone : '';
    document.getElementById('memberEmail').value = el ? el.dataset.email : '';
    document.getElementById('memberPoints').value = el ? el.dataset.points : '0';
    document.getElementById('memberPointsField').style.display = el ? 'grid' : 'none';
    form.action = el ? '/owner/update-member' : '/owner/save-member';
    modal.style.display = 'flex';
}
function closeMemberModal() { document.getElementById('memberModal').style.display = 'none'; }
function confirmDelete(url) {
    Swal.fire({ title: 'Apakah Anda yakin?', text: 'Data member 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('memberForm').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(() => { closeMemberModal(); Swal.fire('Berhasil', 'Data Berhasil Disimpan', 'success').then(() => location.reload()); });
});
</script>
