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

$currentUserId = $_SESSION['uid'];

// Get all suppliers for dropdown
$suppliersQuery = "SELECT ID, SupplierID, SupplierName, PhoneNumber, City, State FROM tblsuppliers WHERE Status = 'Active' ORDER BY SupplierName";
$suppliersResult = mysqli_query($con, $suppliersQuery);
$availableSuppliers = [];
if ($suppliersResult) {
    while ($supplier = mysqli_fetch_assoc($suppliersResult)) {
        $availableSuppliers[] = $supplier;
    }
}

// Get purchase types for dropdown
$purchaseTypesQuery = "SELECT ID, PurchaseType, InvoicePrefix, Description FROM tblpurchase_type_config WHERE IsActive = 1 ORDER BY PurchaseType";
$purchaseTypesResult = mysqli_query($con, $purchaseTypesQuery);
$availablePurchaseTypes = [];
if ($purchaseTypesResult) {
    while ($type = mysqli_fetch_assoc($purchaseTypesResult)) {
        $availablePurchaseTypes[] = $type;
    }
}
?>
<!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>HSN Wise Purchase 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/hsn-wise-purchase-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-danger 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 Softwares</div>
                            <h1>HSN/SAC Wise Purchase Report</h1>
                            <div class="report-meta">
                                <span id="reportPeriod">Select date range to view report</span>
                            </div>
                        </div>
                        
                        <div class="filter-section">
                            <div class="quick-filters">
                                <button class="quick-filter-btn" onclick="setQuickFilter('today')">Today</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('yesterday')">Yesterday</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('thisWeek')">This Week</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('lastWeek')">Last Week</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('thisMonth')">This Month</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('lastMonth')">Last Month</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('thisYear')">This Year</button>
                                <button class="quick-filter-btn" onclick="setQuickFilter('custom')">Custom Range</button>
                            </div>
                            
                            <div class="filter-row">
                                <div class="filter-group">
                                    <label>HSN/SAC Code</label>
                                    <select id="hsnFilter" class="form-control">
                                        <option value="">All HSN/SAC Codes</option>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Supplier</label>
                                    <select id="supplierFilter" class="form-control">
                                        <option value="">All Suppliers</option>
                                        <?php foreach ($availableSuppliers as $supplier): ?>
                                            <option value="<?php echo htmlspecialchars($supplier['SupplierID']); ?>">
                                                <?php echo htmlspecialchars($supplier['SupplierName']); ?>
                                                <?php if ($supplier['City']): ?>
                                                    - <?php echo htmlspecialchars($supplier['City']); ?>
                                                <?php endif; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Purchase Type</label>
                                    <select id="purchaseTypeFilter" class="form-control">
                                        <option value="">All Types</option>
                                        <?php foreach ($availablePurchaseTypes as $type): ?>
                                            <option value="<?php echo htmlspecialchars($type['PurchaseType']); ?>">
                                                <?php echo htmlspecialchars($type['PurchaseType']); ?>
                                            </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 class="custom-dates-row" id="customDatesRow">
                                <div class="filter-group">
                                    <label>Start Date</label>
                                    <input type="date" id="startDate" class="form-control" value="<?php echo date('Y-m-01'); ?>">
                                </div>
                                
                                <div class="filter-group">
                                    <label>End Date</label>
                                    <input type="date" id="endDate" class="form-control" value="<?php echo date('Y-m-t'); ?>">
                                </div>
                            </div>
                        </div>
                        
                        <div id="loadingState" class="loading" style="display: none;">
                            <i class="fas fa-spinner fa-spin"></i> Loading report data...
                        </div>
                        
                        <div id="reportContent" style="display: none;">
                            <div class="summary-cards">
                                <div class="summary-card">
                                    <h3>Total Purchase Value</h3>
                                    <div class="amount" id="totalPurchases">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Taxable Amount</h3>
                                    <div class="amount" id="taxableAmount">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>CGST Amount</h3>
                                    <div class="amount" id="cgstAmount">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>SGST Amount</h3>
                                    <div class="amount" id="sgstAmount">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>IGST Amount</h3>
                                    <div class="amount" id="igstAmount">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Total Tax</h3>
                                    <div class="amount" id="totalTax">₹0.00</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Total Quantity</h3>
                                    <div class="amount" id="totalQuantity">0</div>
                                </div>
                                
                                <div class="summary-card">
                                    <h3>Unique HSN Codes</h3>
                                    <div class="amount" id="hsnCount">0</div>
                                </div>
                            </div>
                            
                            <h2 class="section-title">HSN/SAC Code Wise Summary <span id="hsnCodeCount" style="font-size: 14px; color: #858796;">(0 codes)</span></h2>
                            
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th style="width: 4%;">S.No</th>
                                        <th style="width: 10%;">HSN/SAC Code</th>
                                        <th style="width: 25%;">Item Names</th>
                                        <th style="width: 6%;" class="text-right">Transactions</th>
                                        <th style="width: 8%;" class="text-right">Total Qty</th>
                                        <th style="width: 8%;" class="text-right">Taxable Value</th>
                                        <th style="width: 7%;" class="text-right">CGST Amt</th>
                                        <th style="width: 7%;" class="text-right">SGST Amt</th>
                                        <th style="width: 7%;" class="text-right">IGST Amt</th>
                                        <th style="width: 8%;" class="text-right">Total Tax</th>
                                        <th style="width: 10%;" class="text-right">Total Amount</th>
                                    </tr>
                                </thead>
                                <tbody id="hsnTableBody">
                                    <tr>
                                        <td colspan="11" class="no-data">No data available. Please select filters and generate report.</td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="4" class="text-right"><strong>TOTALS:</strong></td>
                                        <td class="text-right" id="totalQtyFooter">0</td>
                                        <td class="text-right" id="totalTaxableFooter">₹0.00</td>
                                        <td class="text-right" id="totalCGSTFooter">₹0.00</td>
                                        <td class="text-right" id="totalSGSTFooter">₹0.00</td>
                                        <td class="text-right" id="totalIGSTFooter">₹0.00</td>
                                        <td class="text-right" id="totalTaxFooter">₹0.00</td>
                                        <td class="text-right" id="totalFinalFooter">₹0.00</td>
                                    </tr>
                                </tfoot>
                            </table>
                        </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/hsn-wise-purchase-report.js"></script>
</body>
</html>
