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

$currentUserId = $_SESSION['uid'];

// Get categories for dropdown
$categoriesQuery = "SELECT ID, CategoryName FROM tblcategories WHERE Status = 'Active' ORDER BY CategoryName";
$categoriesResult = mysqli_query($con, $categoriesQuery);
$availableCategories = [];
if ($categoriesResult) {
    while ($cat = mysqli_fetch_assoc($categoriesResult)) {
        $availableCategories[] = $cat;
    }
}

// Get brands for dropdown
$brandsQuery = "SELECT ID, BrandName FROM tblbrands WHERE Status = 'Active' ORDER BY BrandName";
$brandsResult = mysqli_query($con, $brandsQuery);
$availableBrands = [];
if ($brandsResult) {
    while ($brand = mysqli_fetch_assoc($brandsResult)) {
        $availableBrands[] = $brand;
    }
}

// Get locations for dropdown
$locationsQuery = "SELECT ID, LocationName FROM tbllocations WHERE Status = 'Active' ORDER BY LocationName";
$locationsResult = mysqli_query($con, $locationsQuery);
$availableLocations = [];
if ($locationsResult) {
    while ($loc = mysqli_fetch_assoc($locationsResult)) {
        $availableLocations[] = $loc;
    }
}

// Get items for dropdown
$itemsQuery = "SELECT ID, ItemCode, ItemName FROM tblitems WHERE Status = 'Active' ORDER BY ItemName";
$itemsResult = mysqli_query($con, $itemsQuery);
$availableItems = [];
if ($itemsResult) {
    while ($item = mysqli_fetch_assoc($itemsResult)) {
        $availableItems[] = $item;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Item Stock Report | UZRS Inventory</title>
    
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    
    <!-- Select2 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
    
    <link href="css/item-stock-report.css" rel="stylesheet">
</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">
                    <!-- Navigation Buttons -->
                    <div class="d-sm-flex align-items-center justify-content-between mb-3">
                        <div>
                            <a href="index.php" class="btn btn-sm btn-primary shadow-sm">
                                <i class="fas fa-arrow-left fa-sm text-white-50"></i> Back to Reports Dashboard
                            </a>
                            <a href="../dashboard.php" class="btn btn-sm btn-secondary shadow-sm">
                                <i class="fas fa-home fa-sm text-white-50"></i> Home
                            </a>
                        </div>
                    </div>
                    
                    <div class="report-container">
                        <div class="report-header">
                            <div class="company-name">UZRS Inventory System</div>
                            <h1>Item Stock Report</h1>
                            <div class="report-meta">
                                <span id="reportPeriod">Current Stock Levels - <?php echo date('d M Y'); ?></span>
                            </div>
                        </div>
                        
                        <div class="filter-section">
                            <div class="quick-filters">
                                <button class="quick-filter-btn" onclick="setStockFilter('all')">All Items</button>
                                <button class="quick-filter-btn active" onclick="setStockFilter('in-stock')">In Stock</button>
                                <button class="quick-filter-btn" onclick="setStockFilter('low-stock')">Low Stock</button>
                                <button class="quick-filter-btn" onclick="setStockFilter('out-of-stock')">Out of Stock</button>
                                <button class="quick-filter-btn" onclick="setStockFilter('negative')">Negative Stock</button>
                            </div>
                            
                            <div class="filter-row">
                                <div class="filter-group">
                                    <label>Category</label>
                                    <select id="categoryFilter" class="form-control">
                                        <option value="">All Categories</option>
                                        <?php foreach ($availableCategories as $category): ?>
                                            <option value="<?php echo htmlspecialchars($category['ID']); ?>">
                                                <?php echo htmlspecialchars($category['CategoryName']); ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Brand</label>
                                    <select id="brandFilter" class="form-control">
                                        <option value="">All Brands</option>
                                        <?php foreach ($availableBrands as $brand): ?>
                                            <option value="<?php echo htmlspecialchars($brand['ID']); ?>">
                                                <?php echo htmlspecialchars($brand['BrandName']); ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Location</label>
                                    <select id="locationFilter" class="form-control">
                                        <option value="">All Locations</option>
                                        <?php foreach ($availableLocations as $location): ?>
                                            <option value="<?php echo htmlspecialchars($location['ID']); ?>">
                                                <?php echo htmlspecialchars($location['LocationName']); ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Item</label>
                                    <select id="itemFilter" class="form-control">
                                        <option value="">All Items</option>
                                        <?php foreach ($availableItems as $item): ?>
                                            <option value="<?php echo htmlspecialchars($item['ItemCode']); ?>">
                                                <?php echo htmlspecialchars($item['ItemName']); ?> (<?php echo htmlspecialchars($item['ItemCode']); ?>)
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <button class="btn-filter" onclick="loadReport()">
                                        <i class="fas fa-search"></i> Generate Report
                                    </button>
                                </div>
                                
                                <div class="filter-group">
                                    <button class="btn-print" onclick="openPrintView()">
                                        <i class="fas fa-print"></i> Print
                                    </button>
                                </div>
                                
                                <div class="filter-group">
                                    <button class="btn-export" onclick="exportToExcel()">
                                        <i class="fas fa-file-excel"></i> Export
                                    </button>
                                </div>
                            </div>
                        </div>
                        
                        <div id="loadingState" class="loading" style="display: none;">
                            <i class="fas fa-spinner fa-spin"></i> Loading stock data...
                        </div>
                        
                        <div id="reportContent" style="display: none;">
                            <div class="summary-cards">
                                <div class="summary-card">
                                    <h3>Total Items</h3>
                                    <div class="amount" id="totalItems">0</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Opening Stock</h3>
                                    <div class="amount" id="totalOpeningStock">0</div>
                                </div>
                                
                                <div class="summary-card highlight">
                                    <h3>Current Stock</h3>
                                    <div class="amount" id="totalCurrentStock">0</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Stock Value (Cost)</h3>
                                    <div class="amount" id="totalStockValueCost">₹0.00</div>
                                </div>
                                
                                <div class="summary-card success">
                                    <h3>Stock Value (Selling)</h3>
                                    <div class="amount" id="totalStockValueSelling">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Potential Profit</h3>
                                    <div class="amount" id="totalPotentialProfit">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>In Stock Items</h3>
                                    <div class="amount" id="inStockItems">0</div>
                                </div>
                                
                                <div class="summary-card warning">
                                    <h3>Low Stock Items</h3>
                                    <div class="amount" id="lowStockItems">0</div>
                                </div>
                                
                                <div class="summary-card danger">
                                    <h3>Out of Stock</h3>
                                    <div class="amount" id="outOfStockItems">0</div>
                                </div>
                                
                                <div class="summary-card danger">
                                    <h3>Negative Stock</h3>
                                    <div class="amount" id="negativeStockItems">0</div>
                                </div>
                            </div>
                            
                            <h2 class="section-title">Stock Details <span id="itemCount" style="font-size: 14px; color: #858796;">(0)</span></h2>
                            
                            <div class="table-responsive">
                                <table class="data-table">
                                    <thead>
                                        <tr>
                                            <th style="width: 3%;">S.No</th>
                                            <th style="width: 8%;">Item Code</th>
                                            <th style="width: 15%;">Item Name</th>
                                            <th style="width: 9%;">Category</th>
                                            <th style="width: 8%;">Brand</th>
                                            <th style="width: 7%;">Location</th>
                                            <th style="width: 4%;" class="text-center">Unit</th>
                                            <th style="width: 6%;" class="text-right">Opening</th>
                                            <th style="width: 6%;" class="text-right">Current</th>
                                            <th style="width: 6%;" class="text-right">Alert Qty</th>
                                            <th style="width: 6%;" class="text-right">Cost Price</th>
                                            <th style="width: 7%;" class="text-right">Selling Price</th>
                                            <th style="width: 8%;" class="text-right">Value (Cost)</th>
                                            <th style="width: 7%;" class="text-right">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody id="itemsTableBody">
                                        <tr>
                                            <td colspan="14" class="no-data">No data available. Please generate report.</td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <td colspan="7" class="text-right"><strong>TOTALS:</strong></td>
                                            <td class="text-right" id="totalOpeningFooter">0</td>
                                            <td class="text-right" id="totalCurrentFooter">0</td>
                                            <td class="text-right">-</td>
                                            <td class="text-right">-</td>
                                            <td class="text-right">-</td>
                                            <td class="text-right" id="totalValueFooter">₹0.00</td>
                                            <td class="text-right">-</td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </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="../js/sb-admin-2.min.js"></script>
    
    <!-- Select2 JS -->
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    
    <script src="js/item-stock-report.js"></script>
</body>
</html>
