<?php
session_start();
include_once('../includes/config.php');
include_once('../includes/quick-menu.php');
if (!isset($_SESSION['uid'])) {
    header('location:../logout.php');
    exit();
}

$currentUserId = $_SESSION['uid'];
$userType = $_SESSION['usertype'];

// Fetch filters data
$brands = [];
$categories = [];
$locations = [];

$brandsQuery = "SELECT * FROM tblbrands WHERE Status = 'Active' ORDER BY BrandName";
$brandsResult = mysqli_query($con, $brandsQuery);
if ($brandsResult) {
    while ($row = mysqli_fetch_assoc($brandsResult)) {
        $brands[] = $row;
    }
}

$categoriesQuery = "SELECT * FROM tblcategories WHERE Status = 'Active' ORDER BY CategoryName";
$categoriesResult = mysqli_query($con, $categoriesQuery);
if ($categoriesResult) {
    while ($row = mysqli_fetch_assoc($categoriesResult)) {
        $categories[] = $row;
    }
}

$locationsQuery = "SELECT * FROM tbllocations WHERE Status = 'Active' ORDER BY LocationName";
$locationsResult = mysqli_query($con, $locationsQuery);
if ($locationsResult) {
    while ($row = mysqli_fetch_assoc($locationsResult)) {
        $locations[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>UZRS Inventory | Stock Report</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <link href="../vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
    <link href="css/stock-report.css" rel="stylesheet">
    <?php renderQuickMenuCSS(); ?>
</head>
<body id="page-top">
    <div id="wrapper">
        <?php include_once('../includes/sidebar.php');?>
        <div id="content-wrapper" class="d-flex flex-column">
            <div id="content">
                <?php include_once('../includes/topbar.php');?>
                
                <div class="container-fluid">
                    
                    <?php renderQuickMenu("Stock Report", "View and edit inventory stock levels"); ?>

                    <!-- Stock Report Card -->
                    <div class="card shadow mb-4">
                        <div class="card-header py-3 d-flex justify-content-between align-items-center">
                            <h6 class="m-0 font-weight-bold text-primary">
                                <i class="fas fa-warehouse"></i> Stock Report - Editable Grid
                            </h6>
                            <div class="header-actions">
                                <button class="btn btn-success btn-sm" id="exportExcel">
                                    <i class="fas fa-file-excel"></i> Export to Excel
                                </button>
                                <button class="btn btn-info btn-sm" id="refreshBtn">
                                    <i class="fas fa-sync-alt"></i> Refresh
                                </button>
                            </div>
                        </div>
                        <div class="card-body">
                            <!-- Filters -->
                            <div class="filters-section mb-3">
                                <div class="row">
                                    <div class="col-md-3">
                                        <label>Search</label>
                                        <input type="text" class="form-control form-control-sm" id="searchInput" placeholder="Search items...">
                                    </div>
                                    <div class="col-md-2">
                                        <label>Category</label>
                                        <select class="form-control form-control-sm" id="categoryFilter">
                                            <option value="">All Categories</option>
                                            <?php foreach($categories as $category): ?>
                                            <option value="<?php echo $category['ID']; ?>"><?php echo htmlspecialchars($category['CategoryName']); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2">
                                        <label>Brand</label>
                                        <select class="form-control form-control-sm" id="brandFilter">
                                            <option value="">All Brands</option>
                                            <?php foreach($brands as $brand): ?>
                                            <option value="<?php echo $brand['ID']; ?>"><?php echo htmlspecialchars($brand['BrandName']); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2">
                                        <label>Location</label>
                                        <select class="form-control form-control-sm" id="locationFilter">
                                            <option value="">All Locations</option>
                                            <?php foreach($locations as $location): ?>
                                            <option value="<?php echo $location['ID']; ?>"><?php echo htmlspecialchars($location['LocationName']); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2">
                                        <label>Stock Status</label>
                                        <select class="form-control form-control-sm" id="stockStatusFilter">
                                            <option value="">All</option>
                                            <option value="low">Low Stock</option>
                                            <option value="out">Out of Stock</option>
                                            <option value="sufficient">Sufficient Stock</option>
                                        </select>
                                    </div>
                                    <div class="col-md-1">
                                        <label>&nbsp;</label>
                                        <button class="btn btn-secondary btn-sm btn-block" id="clearFilters">
                                            <i class="fas fa-times"></i> Clear
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <!-- Info Messages -->
                            <div id="alertContainer"></div>

                            <!-- Stock Table -->
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover" id="stockTable" width="100%" cellspacing="0">
                                    <thead class="thead-light">
                                        <tr>
                                            <th>Item Code</th>
                                            <th class="editable-col">Item Name</th>
                                            <th>Category</th>
                                            <th>Brand</th>
                                            <th>Unit</th>
                                            <th class="editable-col">SKU</th>
                                            <th class="editable-col">Barcode</th>
                                            <th class="editable-col">HSN</th>
                                            <th>Location</th>
                                            <th>Rack</th>
                                            <th class="editable-col">Current Stock</th>
                                            <th class="editable-col">Alert Qty</th>
                                            <th class="editable-col">Purchase Price</th>
                                            <th class="editable-col">Sales Price</th>
                                            <th class="editable-col">Shop Rate</th>
                                            <th class="editable-col">Wholesale Rate</th>
                                            <th class="editable-col">MRP</th>
                                            <th>Stock Value</th>
                                            <th>Status</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <!-- Data will be loaded via AJAX -->
                                    </tbody>
                                    <tfoot>
                                        <tr class="table-info font-weight-bold">
                                            <td colspan="10">TOTAL</td>
                                            <td id="totalStock">0</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td id="totalValue">₹0.00</td>
                                            <td colspan="2"></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>

                            <div class="mt-3">
                                <small class="text-muted">
                                    <i class="fas fa-info-circle"></i> 
                                    <strong>Instructions:</strong> Click on any cell in the GREEN columns to edit (Item Name, SKU, Barcode, HSN, Stock, Alert Qty, Prices). 
                                    Press Enter or click outside to save changes. Categories/Brands/Units/Locations can be changed from the item details page.
                                </small>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include_once('../includes/footer.php');?>
        </div>
    </div>

    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>
    <script src="../js/sb-admin-2.min.js"></script>
    <script src="../vendor/datatables/jquery.dataTables.min.js"></script>
    <script src="../vendor/datatables/dataTables.bootstrap4.min.js"></script>
    <?php renderQuickMenuScript(); ?>
    <script src="js/stock-report.js"></script>

    <script>
        $(document).ready(function() {
            $('body').addClass('sidebar-toggled');
            $('.sidebar').addClass('toggled');
        });
    </script>
</body>
</html>
