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

include 'db.php'; // $conn

if (!isset($_GET['customer_id'])) {
    echo "Customer not found.";
    exit;
}

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

// Get customer info
$custSql = "SELECT customer_name, customer_phone, customer_email, customer_address 
            FROM customers WHERE customer_id = $customer_id";
$custRes = $conn->query($custSql);
if (!$custRes || $custRes->num_rows == 0) {
    echo "Customer not found.";
    exit;
}
$customer = $custRes->fetch_assoc();

// Get sales + sale items for this customer
$salesSql = "SELECT s.sale_id, s.invoice_no, s.sale_date, s.total, s.paid, s.due 
             FROM sales s 
             WHERE s.customer_id = $customer_id 
             ORDER BY s.sale_date ASC";
$salesRes = $conn->query($salesSql);
if (!$salesRes) {
    die("SQL Error: " . $conn->error);
}

echo '<div class="supplier-header">';
echo '<div class="left"><strong> Name: ' . htmlspecialchars($customer['customer_name']) . '</strong><br> Phone: ' . htmlspecialchars($customer['customer_phone']) . ' . </strong><br> Address: ' . htmlspecialchars($customer['customer_address']) . ' </div>';
echo '</div>';

echo '<table class="items-table" width="100%">';
echo '<thead><tr>
        <th>SL</th>
        <th>Invoice No</th>
        <th>Date</th>
        <th>Product / Size / Qty</th>
        <th>Total</th>
        <th>Paid</th>
        <th>Due</th>
      </tr></thead>';
echo '<tbody>';

$sl = 1;
$total_total = $total_paid = $total_due = 0;

while ($sale = $salesRes->fetch_assoc()) {
    // Get items for this sale
    $itemsSql = "SELECT product, size, qty FROM sale_items WHERE sale_id = " . $sale['sale_id'];
    $itemsRes = $conn->query($itemsSql);
    $itemDetails = [];
    while ($item = $itemsRes->fetch_assoc()) {
        // CONDITION FOR SIZE DISPLAY
$rawSize = trim($item['size']);

// Normalize size: remove spaces, convert × to x, lowercase everything
$normSize = strtolower(str_replace(['×', '*', '-',' '], 'x', $rawSize));

// Remove double xx (just in case)
$normSize = preg_replace('/x+/', 'x', $normSize);

// Now check if size means 1x1
if ($normSize === "1x1") {
    $showSize = "-"; // or "." if you want dot
} else {
    $showSize = $rawSize; // show original clean size
}

$itemDetails[] = $item['product'] . ' | ' . $showSize . ' | ' . $item['qty'];


    }
    $itemText = implode('<br>', $itemDetails);

    echo '<tr>';
    echo '<td>' . $sl++ . '</td>';
    echo '<td>' . htmlspecialchars($sale['invoice_no']) . '</td>';
    echo '<td>' . htmlspecialchars($sale['sale_date']) . '</td>';
    echo '<td>' . $itemText . '</td>';
    echo '<td>' . number_format($sale['total'],2) . '</td>';
    echo '<td>' . number_format($sale['paid'],2) . '</td>';
    echo '<td>' . number_format($sale['due'],2) . '</td>';
    echo '</tr>';

    $total_total += (float)$sale['total'];
    $total_paid += (float)$sale['paid'];
    $total_due += (float)$sale['due'];
}

echo '</tbody>';
echo '<tfoot>';
echo '<tr>
        <td colspan="4">Total</td>
        <td>' . number_format($total_total,2) . '</td>
        <td>' . number_format($total_paid,2) . '</td>
        <td>' . number_format($total_due,2) . '</td>
      </tr>';
echo '</tfoot>';
echo '</table>';
