<?php
session_start();
if (!isset($_SESSION['username'])) {
    header("Location: index.php");
    exit();
}

include 'db.php'; // mysqli connection

if (!isset($_GET['supplier_id'])) {
    echo "Supplier ID missing!";
    exit;
}

$supplier_id = (int)$_GET['supplier_id'];

// Fetch supplier info
$supplierSql = "SELECT supplier_name, phone, address FROM suppliers WHERE supplier_id = $supplier_id";
$supplierRes = $conn->query($supplierSql);
if (!$supplierRes || $supplierRes->num_rows == 0) {
    echo "Supplier not found!";
    exit;
}
$supplier = $supplierRes->fetch_assoc();

// Fetch purchases
$purchasesSql = "SELECT purchase_id, invoice_no, purchase_date, total, paid, due 
                 FROM purchases WHERE supplier_id = $supplier_id ORDER BY purchase_date ASC";
$purchasesRes = $conn->query($purchasesSql);

if (!$purchasesRes) {
    echo "SQL Error: " . $conn->error;
    exit;
}

// Modal header
echo "<div class='supplier-header'>
        <div class='left' style='margin-bottom: 10px;'>
            <strong>{$supplier['supplier_name']}</strong><br>
            <span class='small-muted'>Phone: {$supplier['phone']}</span><br>
            <span class='small-muted'>Address: {$supplier['address']}</span>
        </div>
      </div>";

// Table
echo "<table class='items-table' style='width:100%; border-collapse:collapse;'>
        <thead>
            <tr>
                <th>Serial</th>
                <th>Invoice No</th>
                <th>Date</th>
                <th>Products</th>
                <th>Total</th>
                <th>Paid</th>
                <th>Due</th>
            </tr>
        </thead>
        <tbody>";

$serial = 1;
$totalAmount = $totalPaid = $totalDue = 0;

while ($purchase = $purchasesRes->fetch_assoc()) {
    // Fetch products for this purchase
    $itemsSql = "SELECT product, size, qty FROM purchase_items WHERE purchase_id = " . (int)$purchase['purchase_id'];
    $itemsRes = $conn->query($itemsSql);
    $productsArr = [];
    while ($item = $itemsRes->fetch_assoc()) {
        $productsArr[] = htmlspecialchars($item['product'] . " (" . $item['size'] . ") x" . $item['qty']);
    }
    $productsStr = implode(", ", $productsArr);

    echo "<tr>
            <td>{$serial}</td>
            <td>{$purchase['invoice_no']}</td>
            <td>{$purchase['purchase_date']}</td>
            <td>{$productsStr}</td>
            <td>" . number_format($purchase['total'],2) . "</td>
            <td>" . number_format($purchase['paid'],2) . "</td>
            <td>" . number_format($purchase['due'],2) . "</td>
          </tr>";

    $totalAmount += (float)$purchase['total'];
    $totalPaid   += (float)$purchase['paid'];
    $totalDue    += (float)$purchase['due'];
    $serial++;
}

echo "</tbody>
      <tfoot>
        <tr>
            <td colspan='4' style='text-align:right;font-weight:bold;'>Total</td>
            <td>" . number_format($totalAmount,2) . "</td>
            <td>" . number_format($totalPaid,2) . "</td>
            <td>" . number_format($totalDue,2) . "</td>
        </tr>
      </tfoot>
      </table>";
?>
