<div class="hero">
    <div>
        <h2 style="margin:0 0 6px;">Manajemen Produk & Jasa</h2>
        <p style="margin:0; opacity:0.95;">Kelola layanan dan produk yang tersedia untuk pelanggan.</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 Produk/Jasa</h3>
        <button class="btn-primary" type="button" onclick="openItemModal()" style="max-width:180px;">Tambah Baru</button>
    </div>
    <form method="get" action="/owner/products" style="display:flex; gap:10px; align-items:center; margin-top:14px; flex-wrap:wrap;">
        <select name="type" onchange="this.form.submit()" style="min-width:180px; max-width:220px;">
            <option value="">Semua Jenis</option>
            <option value="service" <?= ($selectedType ?? '') === 'service' ? 'selected' : '' ?>>Service</option>
            <option value="product" <?= ($selectedType ?? '') === 'product' ? 'selected' : '' ?>>Product</option>
        </select>
    </form>
</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 Produk/Jasa</h3>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Nama</th><th>Harga</th><th>Jenis</th><th>Aksi</th></tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item) : ?>
                    <tr>
                        <td><?= esc($item['name']) ?></td>
                        <td>Rp <?= number_format($item['price'], 0, ',', '.') ?></td>
                        <td><?= esc($item['type']) ?></td>
                        <td>
                            <div class="action-cell">
                                <a class="icon-btn icon-edit" href="#" title="Edit" data-id="<?= esc($item['id']) ?>" data-name="<?= esc($item['name']) ?>" data-price="<?= esc($item['price']) ?>" data-type="<?= esc($item['type']) ?>" onclick="openItemModal(this); return false;"><?= $iconEdit ?></a>
                                <a class="icon-btn icon-delete" href="#" title="Hapus" onclick="confirmDelete('/owner/delete-item/<?= esc($item['id']) ?>'); return false;"><?= $iconDelete ?></a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal" id="itemModal" 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="itemModalTitle" style="margin-top:0;">Tambah Produk/Jasa</h3>
        <form id="itemForm" method="post" action="/owner/save-item">
            <input type="hidden" name="id" id="itemId">
            <div class="form-grid">
                <input type="text" name="name" id="itemName" placeholder="Nama" required>
                <input type="hidden" name="price" id="itemPrice">
                <input type="text" inputmode="numeric" id="itemPriceDisplay" placeholder="Harga (Rp)" required>
                <select name="type" id="itemType">
                    <option value="service">Service</option>
                    <option value="product">Product</option>
                </select>
            </div>
            <div class="form-grid">
                <button class="btn-primary" type="submit">Simpan</button>
                <button class="btn-secondary" type="button" onclick="closeItemModal();">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 formatRupiahDisplay(digits) {
    if (!digits) return '';
    return 'Rp ' + digits.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
}
document.getElementById('itemPriceDisplay').addEventListener('input', function () {
    const digits = this.value.replace(/\D/g, '');
    document.getElementById('itemPrice').value = digits;
    this.value = formatRupiahDisplay(digits);
});
function openItemModal(el) {
    const modal = document.getElementById('itemModal');
    const form = document.getElementById('itemForm');
    document.getElementById('itemModalTitle').textContent = el ? 'Edit Produk/Jasa' : 'Tambah Produk/Jasa';
    document.getElementById('itemId').value = el ? el.dataset.id : '';
    document.getElementById('itemName').value = el ? el.dataset.name : '';
    document.getElementById('itemPrice').value = el ? el.dataset.price : '';
    document.getElementById('itemPriceDisplay').value = el ? formatRupiahDisplay(el.dataset.price) : '';
    document.getElementById('itemType').value = el ? el.dataset.type : 'service';
    form.action = el ? '/owner/update-item' : '/owner/save-item';
    modal.style.display = 'flex';
}
function closeItemModal() { document.getElementById('itemModal').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('itemForm').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(() => { closeItemModal(); Swal.fire('Berhasil', 'Data Berhasil Disimpan', 'success').then(() => location.reload()); });
});
</script>
