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

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

// Get all customers for dropdown
$customersQuery = "SELECT ID, CustomerID, CustomerName, ContactPerson, Mobile, City FROM tblcustomers WHERE IsActive = 1 ORDER BY CustomerName";
$customersResult = mysqli_query($con, $customersQuery);
$availableCustomers = [];
if ($customersResult) {
    while ($customer = mysqli_fetch_assoc($customersResult)) {
        $availableCustomers[] = $customer;
    }
}
?>
<!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>Customer Accounts 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="css/customer-accounts-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>Customer Accounts Report</h1>
                            <div class="report-meta">
                                <span id="reportPeriod">Select dates to generate 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 $customer['ID']; ?>">
                                                <?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" style="flex: 0.5;">
                                    <label>&nbsp;</label>
                                    <button type="button" class="btn-filter" onclick="loadReport()">
                                        <i class="fas fa-search"></i> Generate Report
                                    </button>
                                </div>
                                <div class="filter-group" style="flex: 0.5;">
                                    <label>&nbsp;</label>
                                    <button type="button" class="btn-print" onclick="openPrintView()">
                                        <i class="fas fa-print"></i> Print
                                    </button>
                                </div>
                                <div class="filter-group" style="flex: 0.5;">
                                    <label>&nbsp;</label>
                                    <button type="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;">
                            <h2 class="section-title">Account Ledger <span id="ledgerCount"></span></h2>
                            
                            <div class="table-responsive">
                                <table class="data-table">
                                    <thead>
                                        <tr>
                                            <th style="width: 8%;">Date</th>
                                            <th style="width: 18%;">Customer</th>
                                            <th style="width: 10%;">Type</th>
                                            <th style="width: 12%;">Invoice/Ref</th>
                                            <th style="width: 20%;">Description</th>
                                            <th style="width: 10%;">Payment Mode</th>
                                            <th style="width: 11%;" class="text-right">Debit (₹)</th>
                                            <th style="width: 11%;" class="text-right">Credit (₹)</th>
                                        </tr>
                                    </thead>
                                    <tbody id="ledgerTableBody">
                                        <tr>
                                            <td colspan="8" class="no-data">No transactions found. Please select filters and click Generate Report.</td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <td colspan="6" class="text-right"><strong>PERIOD TOTALS:</strong></td>
                                            <td class="text-right amount-negative" id="totalDebitFooter">₹0.00</td>
                                            <td class="text-right amount-positive" id="totalCreditFooter">₹0.00</td>
                                        </tr>
                                        <tr>
                                            <td colspan="7" class="text-right"><strong>OUTSTANDING BALANCE:</strong></td>
                                            <td class="text-right" id="closingBalanceFooter">₹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>
    
    <!-- Select2 JS -->
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    
    <script src="js/customer-accounts-report.js"></script>
</body>
</html>
