<?php
require_once __DIR__ . '/../config.php';
requireRole(['admin', 'manager', 'clerk']);

$pageTitle = 'Suppliers';
include __DIR__ . '/../includes/admin-header.php';

require_once __DIR__ . '/../lib/logic.php';
$suppliers = getSupplierPerformance();
?>

        <div class="row g-4 mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">Supplier Performance</h5>
                        <div class="d-flex gap-2">
                            <div class="dropdown-container" style="position: relative;">
                                <button class="btn btn-sm btn-outline-success dropdown-toggle" onclick="toggleExportDropdown(this)">
                                    <i class="bi bi-download"></i> Export
                                </button>
                                <div class="export-dropdown hidden" style="position: absolute; right: 0; top: 100%; margin-top: 4px; background: white; border: 1px solid #ddd; border-radius: 4px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 100; min-width: 140px;">
                                    <button onclick="exportData('suppliers', getFilters())" style="display: block; width: 100%; padding: 8px 12px; text-align: left; border: none; background: none; cursor: pointer;">Excel (.xlsx)</button>
                                    <button onclick="exportToCSV('suppliers', getFilters())" style="display: block; width: 100%; padding: 8px 12px; text-align: left; border: none; background: none; cursor: pointer; border-top: 1px solid #eee;">CSV (.csv)</button>
                                </div>
                            </div>
                            <div class="dropdown-container" style="position: relative;">
                                <button class="btn btn-sm btn-outline-primary dropdown-toggle" onclick="toggleImportDropdown(this)">
                                    <i class="bi bi-upload"></i> Import
                                </button>
                                <div class="import-dropdown hidden" style="position: absolute; right: 0; top: 100%; margin-top: 4px; background: white; border: 1px solid #ddd; border-radius: 4px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 100; min-width: 140px;">
                                    <button onclick="showImportModal('suppliers', 'xlsx')" style="display: block; width: 100%; padding: 8px 12px; text-align: left; border: none; background: none; cursor: pointer;">Excel (.xlsx)</button>
                                    <button onclick="showImportModal('suppliers', 'csv')" style="display: block; width: 100%; padding: 8px 12px; text-align: left; border: none; background: none; cursor: pointer; border-top: 1px solid #eee;">CSV (.csv)</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Supplier ID</th>
                                        <th>Supplier Name</th>
                                        <th>Contact Person</th>
                                        <th>Phone</th>
                                        <th>Email</th>
                                        <th>Lead Time (Days)</th>
                                        <th>Rating</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $pdo = getDBConnection();
                                    $allSuppliers = $pdo->query("SELECT * FROM suppliers ORDER BY SupplierName")->fetchAll();
                                    if (empty($allSuppliers)): ?>
                                    <tr><td colspan="7" class="text-center">No suppliers found</td></tr>
                                    <?php else: ?>
                                    <?php foreach ($allSuppliers as $s): ?>
                                    <tr>
                                        <td><?php echo sanitize($s['SupplierID']); ?></td>
                                        <td><?php echo sanitize($s['SupplierName']); ?></td>
                                        <td><?php echo sanitize($s['ContactPerson'] ?? '-'); ?></td>
                                        <td><?php echo sanitize($s['Phone'] ?? '-'); ?></td>
                                        <td><?php echo sanitize($s['Email'] ?? '-'); ?></td>
                                        <td><?php echo $s['LeadTime_Days']; ?></td>
                                        <td><?php echo number_format($s['Rating'] ?? 0, 1); ?>/5</td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
        function getFilters() {
            return { supplier: '' };
        }
        </script>

<?php include __DIR__ . '/../includes/admin-footer.php'; ?>

