<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

$filterPid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
$query = "SELECT l.*, p.productName, p.sku, v.store_name 
          FROM inventory_logs l 
          JOIN products p ON l.product_id = p.id 
          LEFT JOIN vendors v ON l.vendor_id = v.id 
          WHERE 1=1";

if ($filterPid > 0) {
    $query .= " AND l.product_id='$filterPid'";
    $pageTitle = "Log History";
} else {
    $pageTitle = "All Inventory Logs";
}
$query .= " ORDER BY l.created_at DESC";

$result = mysqli_query($con, $query);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Inventory History - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="css/styles.css?v=4.0" rel="stylesheet" />
    <link href="css/admin-professional.css?v=4.0" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>
    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>
        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-history me-2"></i> Inventory Logs</h1>
                            <p>Global stock movement history</p>
                        </div>
                        <div>
                            <a href="inventory.php" class="btn btn-secondary"><i class="fas fa-arrow-left me-2"></i>Back
                                to Inventory</a>
                        </div>
                    </div>

                    <div class="table-card shadow-sm border-0">
                        <div class="card-header bg-white border-bottom py-3">
                            <h5 class="mb-0 fw-bold text-dark">
                                <i class="fas fa-history me-2 text-primary"></i>Movement Logs History
                            </h5>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive">
                                <table id="datatablesSimple" class="table table-hover align-middle mb-0">
                                    <thead>
                                        <tr>
                                            <th>Date & Time</th>
                                            <th>Product Details</th>
                                            <th>Owner / Source</th>
                                            <th>Reason</th>
                                            <th class="text-center">Change</th>
                                            <th class="text-center">Previous</th>
                                            <th class="text-center">New Stock</th>
                                            <th class="text-center">Admin</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = mysqli_fetch_array($result)) {
                                            $adj = intval($row['adjustment']);
                                            $adjClass = ($adj >= 0) ? 'text-success' : 'text-danger';
                                            $adjSign = ($adj >= 0) ? '+' : '';
                                            $owner = ($row['store_name']) ? htmlentities($row['store_name']) : 'Official Mall';
                                            $ownerBadge = ($row['store_name']) ? 'bg-light text-dark border' : 'bg-primary text-white';
                                            ?>
                                            <tr>
                                                <td class="small text-muted">
                                                    <div class="fw-bold text-dark">
                                                        <?php echo date('M d, Y', strtotime($row['created_at'])); ?></div>
                                                    <div><?php echo date('h:i A', strtotime($row['created_at'])); ?></div>
                                                </td>
                                                <td>
                                                    <div class="fw-bold text-dark">
                                                        <?php echo htmlentities($row['productName']); ?></div>
                                                    <div class="small text-muted">SKU:
                                                        <?php echo htmlentities($row['sku']); ?></div>
                                                </td>
                                                <td>
                                                    <span class="badge <?php echo $ownerBadge; ?> px-2 py-1 fw-normal">
                                                        <?php echo $owner; ?>
                                                    </span>
                                                </td>
                                                <td class="small">
                                                    <?php echo htmlentities($row['reason']); ?>
                                                </td>
                                                <td class="text-center <?php echo $adjClass; ?> fw-bold">
                                                    <?php echo $adjSign . $adj; ?>
                                                </td>
                                                <td class="text-center text-muted">
                                                    <?php echo number_format($row['previous_stock']); ?>
                                                </td>
                                                <td class="text-center">
                                                    <span class="badge bg-secondary px-2">
                                                        <?php echo number_format($row['current_stock']); ?>
                                                    </span>
                                                </td>
                                                <td class="text-center small text-muted">
                                                    <i class="fas fa-user-shield me-1"></i><?php echo ($row['created_by']) ? 'Admin #' . $row['created_by'] : 'System'; ?>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
    <script src="js/datatables-simple-demo.js"></script>
</body>

</html>