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

$currentUserId = $_SESSION['uid'];
?>
<!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>Master Cash Flow 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">
    <link href="css/cash-flow-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 Softwares</div>
                            <h1>Master Cash Flow 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 active" onclick="setQuickFilter('custom')">Custom Range</button>
                            </div>
                            
                            <div class="filter-row">
                                <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-d'); ?>">
                                </div>
                                
                                <div class="filter-group">
                                    <label>Transaction Type</label>
                                    <select id="transactionTypeFilter" class="form-control">
                                        <option value="">All Transactions</option>
                                        <option value="SALES">Sales</option>
                                        <option value="SALES-RETURN">Sales Return</option>
                                        <option value="CUSTOMER-PAYMENT">Customer Payments</option>
                                        <option value="PURCHASE">Purchases</option>
                                        <option value="PURCHASE-RETURN">Purchase Return</option>
                                        <option value="SUPPLIER-PAYMENT">Supplier Payments</option>
                                        <option value="EXPENSE">Expenses</option>
                                    </select>
                                </div>
                                
                                <div class="filter-group">
                                    <label>Payment Mode</label>
                                    <select id="paymentModeFilter" class="form-control">
                                        <option value="">Cash + UPI</option>
                                        <option value="Cash">Cash Only</option>
                                        <option value="UPI">UPI Only</option>
                                    </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 cash flow data...
                        </div>
                        
                        <div id="reportContent" style="display: none;">
                            <!-- Summary Cards -->
                            <div class="summary-grid">
                                <div class="summary-card cash-in">
                                    <h3><i class="fas fa-arrow-down"></i> Total Cash In</h3>
                                    <div class="amount" id="totalCashIn">₹0.00</div>
                                    <div class="sub-details">
                                        <div>Sales: <span id="salesCash">₹0.00</span></div>
                                        <div>Cust. Payments: <span id="customerPaymentsCash">₹0.00</span></div>
                                    </div>
                                </div>
                                
                                <div class="summary-card cash-out">
                                    <h3><i class="fas fa-arrow-up"></i> Total Cash Out</h3>
                                    <div class="amount" id="totalCashOut">₹0.00</div>
                                    <div class="sub-details">
                                        <div>Purchases: <span id="purchasesCash">₹0.00</span></div>
                                        <div>Supp. Payments: <span id="supplierPaymentsCash">₹0.00</span></div>
                                        <div>Expenses: <span id="expensesCash">₹0.00</span></div>
                                    </div>
                                </div>
                                
                                <div class="summary-card net-cash">
                                    <h3><i class="fas fa-wallet"></i> Net Cash Flow</h3>
                                    <div class="amount" id="netCashFlow">₹0.00</div>
                                </div>
                                
                                <div class="summary-card upi-in">
                                    <h3><i class="fas fa-arrow-down"></i> Total UPI In</h3>
                                    <div class="amount" id="totalUpiIn">₹0.00</div>
                                    <div class="sub-details">
                                        <div>Sales: <span id="salesUpi">₹0.00</span></div>
                                        <div>Cust. Payments: <span id="customerPaymentsUpi">₹0.00</span></div>
                                    </div>
                                </div>
                                
                                <div class="summary-card upi-out">
                                    <h3><i class="fas fa-arrow-up"></i> Total UPI Out</h3>
                                    <div class="amount" id="totalUpiOut">₹0.00</div>
                                    <div class="sub-details">
                                        <div>Purchases: <span id="purchasesUpi">₹0.00</span></div>
                                        <div>Supp. Payments: <span id="supplierPaymentsUpi">₹0.00</span></div>
                                        <div>Expenses: <span id="expensesUpi">₹0.00</span></div>
                                    </div>
                                </div>
                                
                                <div class="summary-card net-upi">
                                    <h3><i class="fas fa-mobile-alt"></i> Net UPI Flow</h3>
                                    <div class="amount" id="netUpiFlow">₹0.00</div>
                                </div>
                                
                                <div class="summary-card total-transactions">
                                    <h3><i class="fas fa-list"></i> Total Transactions</h3>
                                    <div class="amount" id="totalTransactions">0</div>
                                </div>
                                
                                <div class="summary-card net-total highlight">
                                    <h3><i class="fas fa-chart-line"></i> Net Total Flow</h3>
                                    <div class="amount" id="netTotalFlow">₹0.00</div>
                                </div>
                            </div>
                            
                            <h2 class="section-title">
                                All Cash Flow Transactions 
                                <span id="transactionCount" 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%;">Date</th>
                                            <th style="width: 10%;">Type</th>
                                            <th style="width: 12%;">Reference</th>
                                            <th style="width: 15%;">Party</th>
                                            <th style="width: 20%;">Description</th>
                                            <th style="width: 8%;" class="text-right">Cash In</th>
                                            <th style="width: 8%;" class="text-right">Cash Out</th>
                                            <th style="width: 8%;" class="text-right">UPI In</th>
                                            <th style="width: 8%;" class="text-right">UPI Out</th>
                                        </tr>
                                    </thead>
                                    <tbody id="transactionsTableBody">
                                        <tr>
                                            <td colspan="10" 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="totalCashInFooter">₹0.00</td>
                                            <td class="text-right" id="totalCashOutFooter">₹0.00</td>
                                            <td class="text-right" id="totalUpiInFooter">₹0.00</td>
                                            <td class="text-right" id="totalUpiOutFooter">₹0.00</td>
                                        </tr>
                                        <tr class="net-total-row">
                                            <td colspan="6" class="text-right"><strong>NET FLOW:</strong></td>
                                            <td colspan="2" class="text-right" id="netCashFlowFooter">₹0.00</td>
                                            <td colspan="2" class="text-right" id="netUpiFlowFooter">₹0.00</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>
    <script src="js/cash-flow-report.js"></script>
</body>
</html>
