<?php
$page_title = 'Shop Management';
require_once 'includes/header.php';

// Fetch All Shops
try {
    $stmt = $pdo->query("SELECT * FROM shops ORDER BY shop_id DESC");
    $shops = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>

<div class="card">
    <div class="card-header">
        <h3 class="card-title"><i class="fas fa-store"></i> ຈັດການຮ້ານຄ້າ</h3>
        <button class="btn btn-primary" onclick="openAddModal()">
            <i class="fas fa-plus"></i> ເພີ່ມຮ້ານຄ້າ
        </button>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table" style="width:100%; border-collapse: collapse; text-align: left;">
                <thead>
                    <tr style="border-bottom: 1px solid var(--border-color);">
                        <th style="padding: 12px;">ID</th>
                        <th style="padding: 12px;">Shop Name</th>
                        <th style="padding: 12px;">Code</th>
                        <th style="padding: 12px;">Phone</th>
                        <th style="padding: 12px;">Address</th>
                        <th style="padding: 12px;">Status</th>
                        <th style="padding: 12px;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($shops as $shop): ?>
                        <tr style="border-bottom: 1px solid var(--border-color);">
                            <td style="padding: 12px;">
                                <?= $shop['shop_id'] ?>
                            </td>
                            <td style="padding: 12px;">
                                <?= htmlspecialchars($shop['shop_name']) ?>
                            </td>
                            <td style="padding: 12px;">
                                <?= htmlspecialchars($shop['shop_code']) ?>
                            </td>
                            <td style="padding: 12px;">
                                <?= htmlspecialchars($shop['phone']) ?>
                            </td>
                            <td style="padding: 12px;">
                                <?= htmlspecialchars($shop['address']) ?>
                            </td>
                            <td style="padding: 12px;">
                                <?php if ($shop['status'] == 1): ?>
                                    <span style="color: var(--success);">Active</span>
                                <?php else: ?>
                                    <span style="color: var(--danger);">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td style="padding: 12px;">
                                <button class="btn btn-secondary" onclick='editShop(<?= json_encode($shop) ?>)'
                                    style="padding: 5px 10px; font-size: 13px;">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="btn btn-danger" onclick="deleteShop(<?= $shop['shop_id'] ?>)"
                                    style="padding: 5px 10px; font-size: 13px; background: var(--danger); color: white; border: none;">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add/Edit Shop Modal -->
<div id="shopModal" class="modal-overlay">
    <div class="modal">
        <div class="modal-header">
            <h3 class="modal-title" id="modalTitle">ເພີ່ມຮ້ານຄ້າ</h3>
            <button class="modal-close" onclick="closeGenericModal('shopModal')">&times;</button>
        </div>
        <form id="shopForm" action="actions/shop_actions.php" method="POST">
            <div class="modal-body">
                <input type="hidden" name="action" id="action" value="add">
                <input type="hidden" name="shop_id" id="shop_id">

                <div class="form-group">
                    <label class="form-label">ຊື່ຮ້ານຄ້າ</label>
                    <input type="text" name="shop_name" id="shop_name" class="form-control" required>
                </div>
                <div class="form-group">
                    <label class="form-label">ລະຫັດຮ້ານຄ້າ (Shop Code)</label>
                    <input type="text" name="shop_code" id="shop_code" class="form-control">
                </div>
                <div class="form-group">
                    <label class="form-label">Phone</label>
                    <input type="text" name="phone" id="phone" class="form-control">
                </div>
                <div class="form-group">
                    <label class="form-label">Address</label>
                    <textarea name="address" id="address" class="form-control" rows="3"></textarea>
                </div>
                <div class="form-group">
                    <label class="form-label">Status</label>
                    <select name="status" id="status" class="form-control">
                        <option value="1">Active</option>
                        <option value="0">Inactive</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary"
                    onclick="closeGenericModal('shopModal')">ຍົກເລີກ</button>
                <button type="submit" class="btn btn-primary">ບັນທຶກ</button>
            </div>
        </form>
    </div>
</div>

<script>
    function openAddModal() {
        document.getElementById('modalTitle').innerText = 'ເພີ່ມຮ້ານຄ້າ';
        document.getElementById('action').value = 'add';
        document.getElementById('shop_id').value = '';
        document.getElementById('shopForm').reset();
        openGenericModal('shopModal');
    }

    function editShop(shop) {
        document.getElementById('modalTitle').innerText = 'ແກ້ໄຂຮ້ານຄ້າ';
        document.getElementById('action').value = 'edit';
        document.getElementById('shop_id').value = shop.shop_id;
        document.getElementById('shop_name').value = shop.shop_name;
        document.getElementById('shop_code').value = shop.shop_code || '';
        document.getElementById('phone').value = shop.phone || '';
        document.getElementById('address').value = shop.address || '';
        document.getElementById('status').value = shop.status;
        openGenericModal('shopModal');
    }

    function deleteShop(id) {
        Swal.fire({
            title: 'ຢືນຢັນການລຶບ?',
            text: "ທ່ານຕ້ອງການລຶບຮ້ານຄ້ານີ້ບໍ່?",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'ລຶບ!',
            cancelButtonText: 'ຍົກເລີກ'
        }).then((result) => {
            if (result.isConfirmed) {
                const formData = new FormData();
                formData.append('action', 'delete');
                formData.append('shop_id', id);

                fetch('actions/shop_actions.php', {
                    method: 'POST',
                    body: formData
                })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            Swal.fire('Deleted!', 'ລຶບສຳເລັດແລ້ວ.', 'success')
                                .then(() => location.reload());
                        } else {
                            Swal.fire('Error!', data.message || 'ເກີດຂໍ້ຜິດພາດ', 'error');
                        }
                    });
            }
        });
    }

    document.getElementById('shopForm').onsubmit = function (e) {
        e.preventDefault();
        const formData = new FormData(this);

        fetch('actions/shop_actions.php', {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    closeGenericModal('shopModal');
                    Swal.fire('Success!', 'ບັນທຶກສຳເລັດແລ້ວ.', 'success')
                        .then(() => location.reload());
                } else {
                    Swal.fire('Error!', data.message || 'ເກີດຂໍ້ຜິດພາດ', 'error');
                }
            });
    };
</script>

<?php require_once 'includes/footer.php'; ?>