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

$currentUserId = $_SESSION['uid'];

// Get all customers for dropdown
$customersQuery = "SELECT ID, CustomerID, CustomerName, Mobile, City FROM tblcustomers WHERE Status = 'Active' ORDER BY CustomerName";
$customersResult = mysqli_query($con, $customersQuery);
$availableCustomers = [];
if ($customersResult) {
    while ($customer = mysqli_fetch_assoc($customersResult)) {
        $availableCustomers[] = $customer;
    }
}

// Get sales types for dropdown
$salesTypesQuery = "SELECT ID, SalesType, InvoicePrefix, Description FROM tblsales_type_config WHERE IsActive = 1 ORDER BY SalesType";
$salesTypesResult = mysqli_query($con, $salesTypesQuery);
$availableSalesTypes = [];
if ($salesTypesResult) {
    while ($type = mysqli_fetch_assoc($salesTypesResult)) {
        $availableSalesTypes[] = $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>GST Sales 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/gst-sales-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>GST Sales 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>Customer</label>
                                    <select id="customerFilter" class="form-control">
                                        <option value="">All Customers</option>
                                        <?php foreach ($availableCustomers as $customer): ?>
                                            <option value="<?php echo htmlspecialchars($customer['CustomerID']); ?>">
                                                <?php echo htmlspecialchars($customer['CustomerName']); ?>
                                                <?php if ($customer['City']): ?>
                                                    - <?php echo htmlspecialchars($customer['City']); ?>
                                                <?php endif; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Sales Type</label>
                                    <select id="salesTypeFilter" class="form-control">
                                        <option value="">All Types</option>
                                        <?php foreach ($availableSalesTypes as $type): ?>
                                            <option value="<?php echo htmlspecialchars($type['SalesType']); ?>">
                                                <?php echo htmlspecialchars($type['SalesType']); ?>
                                            </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 Sales</h3>
                                    <div class="amount" id="totalSales">₹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>
                            
                            <h2 class="section-title">Sales Transactions <span id="transactionCount" style="font-size: 14px; color: #858796;">(0)</span></h2>
                            
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th style="width: 4%;">S.No</th>
                                        <th style="width: 7%;">Date</th>
                                        <th style="width: 9%;">Invoice No</th>
                                        <th style="width: 10%;">Customer Name</th>
                                        <th style="width: 9%;">GSTIN</th>
                                        <th style="width: 6%;">Tax Type</th>
                                        <th style="width: 8%;" class="text-right">Taxable Value</th>
                                        <th style="width: 4%;" class="text-right">CGST%</th>
                                        <th style="width: 6%;" class="text-right">CGST Amt</th>
                                        <th style="width: 4%;" class="text-right">SGST%</th>
                                        <th style="width: 6%;" class="text-right">SGST Amt</th>
                                        <th style="width: 4%;" class="text-right">IGST%</th>
                                        <th style="width: 6%;" class="text-right">IGST Amt</th>
                                        <th style="width: 8%;" class="text-right">Total Tax</th>
                                        <th style="width: 9%;" class="text-right">Total</th>
                                    </tr>
                                </thead>
                                <tbody id="salesTableBody">
                                    <tr>
                                        <td colspan="15" class="no-data">No data available. Please select filters and generate report.</td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="6" class="text-right"><strong>TOTALS:</strong></td>
                                        <td class="text-right" id="totalTaxableFooter">₹0.00</td>
                                        <td class="text-right"></td>
                                        <td class="text-right" id="totalCGSTFooter">₹0.00</td>
                                        <td class="text-right"></td>
                                        <td class="text-right" id="totalSGSTFooter">₹0.00</td>
                                        <td class="text-right"></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/gst-sales-report.js"></script>
</body>
</html>
