<?php
$search = $_GET['search'] ?? '';

$query = "SELECT * FROM suppliers WHERE name_en LIKE ? OR name_ar LIKE ? OR contact_person LIKE ? OR phone LIKE ? ORDER BY id DESC";
$params = ["%$search%", "%$search%", "%$search%", "%$search%"];

$stmt = $db->prepare($query);
$stmt->execute($params);
$suppliers = $stmt->fetchAll();
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="fw-bold text-secondary"><?php echo __('suppliers'); ?></h3>
    <button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addSupplierModal" onclick="resetSupplierModal()">
        <i class="bi bi-plus-circle me-1"></i> <?php echo __('add_supplier'); ?>
    </button>
</div>

<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
    <div class="card-body">
        <form method="GET" action="">
            <div class="input-group">
                <span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
                <input type="text" name="search" class="form-control bg-light border-start-0" placeholder="<?php echo __('search'); ?>..." value="<?php echo htmlspecialchars($search); ?>">
                <button type="submit" class="btn btn-secondary"><?php echo __('search'); ?></button>
            </div>
        </form>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light text-secondary">
                    <tr>
                        <th class="px-4 py-3">#</th>
                        <th class="py-3"><?php echo __('name'); ?></th>
                        <th class="py-3"><?php echo __('contact_person'); ?></th>
                        <th class="py-3"><?php echo __('phone'); ?></th>
                        <th class="py-3"><?php echo __('email'); ?></th>
                        <th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($suppliers)): ?>
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">
                            <i class="bi bi-truck display-4 d-block mb-3"></i>
                            <?php echo __('no_suppliers_found'); ?>
                        </td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($suppliers as $supplier): ?>
                    <tr>
                        <td class="px-4 fw-medium text-secondary"><?php echo $supplier['id']; ?></td>
                        <td>
                            <div class="fw-semibold text-dark"><?php echo htmlspecialchars($supplier['name_'.$lang]); ?></div>
                            <small class="text-muted"><?php echo htmlspecialchars($supplier['name_'.($lang == 'en' ? 'ar' : 'en')]); ?></small>
                        </td>
                        <td><?php echo htmlspecialchars($supplier['contact_person'] ?? '-'); ?></td>
                        <td><?php echo htmlspecialchars($supplier['phone'] ?? '-'); ?></td>
                        <td><?php echo htmlspecialchars($supplier['email'] ?? '-'); ?></td>
                        <td class="text-end px-4">
                            <div class="btn-group shadow-sm border rounded bg-white">
                                <button class="btn btn-link text-primary py-1 px-2 border-end" 
                                        onclick="showEditSupplierModal(<?php echo htmlspecialchars(json_encode($supplier, JSON_UNESCAPED_UNICODE)); ?>)"
                                        data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
                                    <i class="bi bi-pencil-square"></i>
                                </button>
                                <button class="btn btn-link text-danger py-1 px-2" 
                                        onclick="showDeleteSupplierModal(<?php echo $supplier['id']; ?>)"
                                        data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
                                    <i class="bi bi-trash3"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add/Edit Supplier Modal -->
<div class="modal fade" id="addSupplierModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="supplierModalTitle"><?php echo __('add_supplier'); ?></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" action="">
                <input type="hidden" name="action" id="supplierAction" value="add_supplier">
                <input type="hidden" name="id" id="supplierId">
                <div class="modal-body p-4">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="name_en" id="supplierNameEn" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="name_ar" id="supplierNameAr" required>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label"><?php echo __('contact_person'); ?></label>
                            <input type="text" class="form-control" name="contact_person" id="supplierContact">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label"><?php echo __('phone'); ?></label>
                            <input type="text" class="form-control" name="phone" id="supplierPhone">
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label"><?php echo __('email'); ?></label>
                            <input type="email" class="form-control" name="email" id="supplierEmail">
                        </div>
                         <div class="col-md-6">
                            <label class="form-label"><?php echo __('address'); ?></label>
                            <input type="text" class="form-control" name="address" id="supplierAddress">
                        </div>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
                    <button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Supplier Modal -->
<div class="modal fade" id="deleteSupplierModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title"><?php echo __('delete_supplier'); ?></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" action="">
                <input type="hidden" name="action" value="delete_supplier">
                <input type="hidden" name="id" id="deleteSupplierId">
                <div class="modal-body p-4 text-center">
                    <div class="mb-3 text-danger">
                        <i class="bi bi-exclamation-triangle display-1"></i>
                    </div>
                    <p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
                    <p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
                </div>
                <div class="modal-footer bg-light justify-content-center">
                    <button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
                    <button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function resetSupplierModal() {
    document.getElementById('supplierModalTitle').textContent = '<?php echo __('add_supplier'); ?>';
    document.getElementById('supplierAction').value = 'add_supplier';
    document.getElementById('supplierId').value = '';
    
    document.getElementById('supplierNameEn').value = '';
    document.getElementById('supplierNameAr').value = '';
    document.getElementById('supplierContact').value = '';
    document.getElementById('supplierPhone').value = '';
    document.getElementById('supplierEmail').value = '';
    document.getElementById('supplierAddress').value = '';
}

function showEditSupplierModal(supplier) {
    document.getElementById('supplierModalTitle').textContent = '<?php echo __('edit_supplier'); ?>';
    document.getElementById('supplierAction').value = 'edit_supplier';
    document.getElementById('supplierId').value = supplier.id;
    
    document.getElementById('supplierNameEn').value = supplier.name_en;
    document.getElementById('supplierNameAr').value = supplier.name_ar;
    document.getElementById('supplierContact').value = supplier.contact_person || '';
    document.getElementById('supplierPhone').value = supplier.phone || '';
    document.getElementById('supplierEmail').value = supplier.email || '';
    document.getElementById('supplierAddress').value = supplier.address || '';
    
    var modal = new bootstrap.Modal(document.getElementById('addSupplierModal'));
    modal.show();
}

function showDeleteSupplierModal(id) {
    document.getElementById('deleteSupplierId').value = id;
    var modal = new bootstrap.Modal(document.getElementById('deleteSupplierModal'));
    modal.show();
}
</script>
