<?php
include 'db_connect.php';

if(!isset($_GET['batch_id'])){
    echo '<div class="alert alert-danger">Invalid batch ID</div>';
    exit;
}

$batch_id = intval($_GET['batch_id']);

// Get batch info
$batch = $conn->query("SELECT b.*, p.name as product_name, p.sku
    FROM batch_list b
    INNER JOIN product_list p ON p.id = b.product_id
    WHERE b.id = ".$batch_id)->fetch_assoc();

if(!$batch){
    echo '<div class="alert alert-danger">Batch not found</div>';
    exit;
}

$available = $batch['quantity_received'] - $batch['quantity_sold'] - $batch['quantity_expired'];
?>

<div class="mb-4 p-3" style="background: var(--gray-100); border-radius: 8px;">
    <div class="row">
        <div class="col-md-6">
            <p class="mb-1"><strong>Batch Number:</strong> <?php echo htmlspecialchars($batch['batch_number']); ?></p>
            <p class="mb-1"><strong>Product:</strong> <?php echo htmlspecialchars($batch['product_name']); ?></p>
            <p class="mb-0"><strong>SKU:</strong> <?php echo htmlspecialchars($batch['sku']); ?></p>
        </div>
        <div class="col-md-6">
            <p class="mb-1"><strong>Expiry Date:</strong> <?php echo date('M d, Y', strtotime($batch['expiry_date'])); ?></p>
            <p class="mb-1"><strong>Date Received:</strong> <?php echo date('M d, Y', strtotime($batch['date_received'])); ?></p>
            <p class="mb-0"><strong>Status:</strong>
                <span class="badge badge-<?php echo $batch['status'] == 'active' ? 'success' : ($batch['status'] == 'depleted' ? 'secondary' : 'danger'); ?>">
                    <?php echo ucfirst($batch['status']); ?>
                </span>
            </p>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="text-center p-3" style="background: #e3f2fd; border-radius: 8px;">
            <h4 class="mb-0 text-primary"><?php echo number_format($batch['quantity_received']); ?></h4>
            <small class="text-muted">Received</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="text-center p-3" style="background: #e8f5e9; border-radius: 8px;">
            <h4 class="mb-0 text-success"><?php echo number_format($batch['quantity_sold']); ?></h4>
            <small class="text-muted">Sold</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="text-center p-3" style="background: #ffebee; border-radius: 8px;">
            <h4 class="mb-0 text-danger"><?php echo number_format($batch['quantity_expired']); ?></h4>
            <small class="text-muted">Expired</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="text-center p-3" style="background: #fff3e0; border-radius: 8px;">
            <h4 class="mb-0 text-warning"><?php echo number_format($available); ?></h4>
            <small class="text-muted">Available</small>
        </div>
    </div>
</div>

<h6 class="mb-3"><i class="fas fa-list mr-2"></i>Sales History</h6>
<div class="table-responsive">
    <table class="table table-sm table-bordered">
        <thead style="background: var(--gray-100);">
            <tr>
                <th>Date</th>
                <th>Sale Ref#</th>
                <th>Customer</th>
                <th>Qty Sold</th>
                <th>Unit Price</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $history = $conn->query("SELECT sbd.*, s.ref_no, s.date_updated, c.name as customer_name
                FROM sales_batch_details sbd
                INNER JOIN sales_list s ON s.id = sbd.sales_id
                LEFT JOIN customer_list c ON c.id = s.customer_id
                WHERE sbd.batch_id = ".$batch_id."
                ORDER BY sbd.date_created DESC");

            if($history && $history->num_rows > 0):
                while($h = $history->fetch_assoc()):
            ?>
            <tr>
                <td><?php echo date('M d, Y h:i A', strtotime($h['date_created'])); ?></td>
                <td><strong><?php echo htmlspecialchars($h['ref_no']); ?></strong></td>
                <td><?php echo htmlspecialchars($h['customer_name'] ?? 'Walk-in'); ?></td>
                <td class="text-center"><?php echo number_format($h['quantity']); ?></td>
                <td class="text-right"><?php echo number_format($h['unit_price'], 2); ?></td>
            </tr>
            <?php
                endwhile;
            else:
            ?>
            <tr>
                <td colspan="5" class="text-center text-muted py-3">No sales recorded for this batch</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
