<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$allowed_roles = ['SUPER_ADMIN', 'HOSPITAL_ADMIN', 'BILLING_CLERK', 'ACCOUNTANT'];
if (!in_array($_SESSION['role_code'], $allowed_roles)) {
    header('Location: index.php');
    exit();
}

$page_title = "Financial Dashboard";

// Get date range (default: current month)
$start_date = isset($_GET['start_date']) ? mysqli_real_escape_string($conn, $_GET['start_date']) : date('Y-m-01');
$end_date = isset($_GET['end_date']) ? mysqli_real_escape_string($conn, $_GET['end_date']) : date('Y-m-t');

// Fetch financial summary
$summary_query = "SELECT 
    COALESCE(SUM(CASE WHEN status IN ('PAID', 'PARTIAL_PAYMENT') THEN total_amount END), 0) as total_revenue,
    COALESCE(SUM(CASE WHEN status IN ('PAID', 'PARTIAL_PAYMENT') THEN amount_paid END), 0) as total_collected,
    COALESCE(SUM(balance), 0) as total_outstanding,
    COUNT(DISTINCT CASE WHEN status IN ('PAID', 'PARTIAL_PAYMENT') THEN invoice_id END) as paid_invoices,
    COUNT(DISTINCT CASE WHEN status = 'DRAFT' THEN invoice_id END) as draft_invoices,
    COUNT(DISTINCT CASE WHEN status = 'SENT' THEN invoice_id END) as pending_invoices,
    COUNT(DISTINCT CASE WHEN status = 'OVERDUE' THEN invoice_id END) as overdue_invoices,
    COUNT(DISTINCT CASE WHEN status = 'INSURANCE_PENDING' THEN invoice_id END) as insurance_pending
FROM invoices
WHERE invoice_date BETWEEN ? AND ?";

$stmt = mysqli_prepare($conn, $summary_query);
mysqli_stmt_bind_param($stmt, "ss", $start_date, $end_date);
mysqli_stmt_execute($stmt);
$summary = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
mysqli_stmt_close($stmt);

// Fetch payment statistics
$payment_stats_query = "SELECT 
    payment_method,
    COUNT(*) as total_payments,
    COALESCE(SUM(amount), 0) as total_amount,
    COUNT(CASE WHEN DATE(payment_date) = CURDATE() THEN 1 END) as today_count,
    COALESCE(SUM(CASE WHEN DATE(payment_date) = CURDATE() THEN amount END), 0) as today_amount
FROM payments
WHERE payment_date BETWEEN ? AND ? AND status = 'COMPLETED'
GROUP BY payment_method";

$stmt = mysqli_prepare($conn, $payment_stats_query);
mysqli_stmt_bind_param($stmt, "ss", $start_date, $end_date);
mysqli_stmt_execute($stmt);
$payment_stats_result = mysqli_stmt_get_result($stmt);
$payment_stats = [];
while ($row = mysqli_fetch_assoc($payment_stats_result)) {
    $payment_stats[] = $row;
}
mysqli_stmt_close($stmt);

// Calculate total payments
$total_payments = array_sum(array_column($payment_stats, 'total_payments'));
$total_payment_amount = array_sum(array_column($payment_stats, 'total_amount'));

// Fetch daily revenue trend (last 30 days)
$trend_query = "SELECT 
    DATE(payment_date) as payment_day,
    COUNT(*) as transaction_count,
    SUM(amount) as daily_revenue
FROM payments
WHERE payment_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND status = 'COMPLETED'
GROUP BY DATE(payment_date)
ORDER BY payment_day ASC";

$trend_result = mysqli_query($conn, $trend_query);
$revenue_trend = [];
while ($row = mysqli_fetch_assoc($trend_result)) {
    $revenue_trend[] = $row;
}

// Fetch top revenue sources
$sources_query = "SELECT 
    ii.item_type as source,
    COUNT(*) as count,
    SUM(ii.total_price) as revenue
FROM invoice_items ii
INNER JOIN invoices i ON ii.invoice_id = i.invoice_id
WHERE i.invoice_date BETWEEN ? AND ?
    AND i.status IN ('PAID', 'PARTIAL_PAYMENT')
GROUP BY ii.item_type
ORDER BY revenue DESC
LIMIT 5";

$stmt = mysqli_prepare($conn, $sources_query);
mysqli_stmt_bind_param($stmt, "ss", $start_date, $end_date);
mysqli_stmt_execute($stmt);
$revenue_sources_result = mysqli_stmt_get_result($stmt);
$revenue_sources = [];
while ($row = mysqli_fetch_assoc($revenue_sources_result)) {
    $revenue_sources[] = $row;
}
mysqli_stmt_close($stmt);

// Fetch recent transactions
$recent_query = "SELECT 
    p.payment_id,
    p.payment_number,
    p.amount,
    p.payment_method,
    p.payment_date,
    p.transaction_id,
    u.full_name as patient_name,
    pat.patient_number
FROM payments p
INNER JOIN patients pat ON p.patient_id = pat.patient_id
INNER JOIN users u ON pat.user_id = u.user_id
WHERE p.payment_date BETWEEN ? AND ? AND p.status = 'COMPLETED'
ORDER BY p.payment_date DESC
LIMIT 10";

$stmt = mysqli_prepare($conn, $recent_query);
mysqli_stmt_bind_param($stmt, "ss", $start_date, $end_date);
mysqli_stmt_execute($stmt);
$recent_transactions_result = mysqli_stmt_get_result($stmt);
$recent_transactions = [];
while ($row = mysqli_fetch_assoc($recent_transactions_result)) {
    $recent_transactions[] = $row;
}
mysqli_stmt_close($stmt);

// Fetch insurance claims summary
$insurance_query = "SELECT 
    COUNT(*) as total_claims,
    COALESCE(SUM(insurance_covered), 0) as total_claimed,
    COUNT(CASE WHEN status = 'INSURANCE_PENDING' THEN 1 END) as pending_claims,
    COALESCE(SUM(CASE WHEN status = 'INSURANCE_PENDING' THEN insurance_covered END), 0) as pending_amount
FROM invoices
WHERE invoice_date BETWEEN ? AND ?
    AND bill_to = 'INSURANCE'";

$stmt = mysqli_prepare($conn, $insurance_query);
mysqli_stmt_bind_param($stmt, "ss", $start_date, $end_date);
mysqli_stmt_execute($stmt);
$insurance_summary = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
mysqli_stmt_close($stmt);
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => $page_title)); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet">
    
    <style>
        .page-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 2rem;
            border-radius: 12px;
            color: white;
            margin-bottom: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .stat-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            transition: transform 0.2s;
            margin-bottom: 1.5rem;
        }
        
        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.15);
        }
        
        .stat-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .stat-icon.revenue {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .stat-icon.collected {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            color: white;
        }
        
        .stat-icon.outstanding {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            color: white;
        }
        
        .stat-icon.invoices {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
        }
        
        .stat-value {
            font-size: 1.875rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 0.25rem;
        }
        
        .stat-label {
            font-size: 0.875rem;
            color: #64748b;
            font-weight: 500;
        }
        
        .stat-change {
            font-size: 0.75rem;
            font-weight: 600;
            margin-top: 0.5rem;
        }
        
        .stat-change.positive { color: #059669; }
        .stat-change.negative { color: #dc2626; }
        
        .chart-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 1.5rem;
        }
        
        .chart-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
            margin-bottom: 1rem;
        }
        
        .payment-mode-badge {
            padding: 0.375rem 0.75rem;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
        }
        
        .payment-mode-badge.cash { background: #dcfce7; color: #166534; }
        .payment-mode-badge.mobilemoney { background: #dbeafe; color: #1e40af; }
        .payment-mode-badge.card { background: #fef3c7; color: #92400e; }
        .payment-mode-badge.insurancedirect { background: #f3e8ff; color: #6b21a8; }
        .payment-mode-badge.cheque { background: #e0f2fe; color: #075985; }
        .payment-mode-badge.banktransfer { background: #fce7f3; color: #831843; }
        .payment-mode-badge.corporate { background: #fef9c3; color: #854d0e; }
        
        .transaction-list {
            max-height: 400px;
            overflow-y: auto;
        }
        
        .transaction-item {
            padding: 1rem;
            border-bottom: 1px solid #f1f5f9;
            transition: background 0.2s;
        }
        
        .transaction-item:hover {
            background: #f8fafc;
        }
        
        .transaction-item:last-child {
            border-bottom: none;
        }
        
        .quick-action-btn {
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            transition: all 0.2s;
        }
        
        .quick-action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .revenue-source-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.75rem;
            border-radius: 8px;
            margin-bottom: 0.5rem;
            background: #f8fafc;
            border: 1px solid #e2e8f0;
        }
        
        .date-range-picker {
            background: white;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 1.5rem;
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Financial Dashboard</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item">Financial Management</li>
                                        <li class="breadcrumb-item active">Dashboard</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Page Header -->
                    <div class="page-header">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h4><i class="ri-money-dollar-circle-line me-2"></i>Financial Overview</h4>
                                <p class="mb-0">Monitor revenue, payments, and financial performance</p>
                            </div>
                        </div>
                    </div>

                    <!-- Date Range Filter -->
                    <div class="row">
                        <div class="col-12">
                            <div class="date-range-picker">
                                <form method="GET" class="row g-3 align-items-end">
                                    <div class="col-md-3">
                                        <label class="form-label">Start Date</label>
                                        <input type="date" class="form-control" name="start_date" value="<?php echo $start_date; ?>">
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">End Date</label>
                                        <input type="date" class="form-control" name="end_date" value="<?php echo $end_date; ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <button type="submit" class="btn btn-primary me-2">
                                            <i class="ri-filter-line me-1"></i> Apply Filter
                                        </button>
                                        <a href="financial_dashboard.php" class="btn btn-outline-secondary">
                                            <i class="ri-refresh-line me-1"></i> Reset
                                        </a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Financial Summary Cards -->
                    <div class="row">
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card">
                                <div class="stat-icon revenue">
                                    <i class="ri-currency-line"></i>
                                </div>
                                <div class="stat-value">KES <?php echo number_format($summary['total_revenue'], 2); ?></div>
                                <div class="stat-label">Total Revenue</div>
                                <div class="stat-change positive">
                                    <i class="ri-arrow-up-line me-1"></i>
                                    <?php echo $summary['paid_invoices']; ?> Paid Invoices
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card">
                                <div class="stat-icon collected">
                                    <i class="ri-check-double-line"></i>
                                </div>
                                <div class="stat-value">KES <?php echo number_format($summary['total_collected'], 2); ?></div>
                                <div class="stat-label">Amount Collected</div>
                                <div class="stat-change positive">
                                    <i class="ri-check-line me-1"></i>
                                    <?php echo $total_payments; ?> Payments
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card">
                                <div class="stat-icon outstanding">
                                    <i class="ri-time-line"></i>
                                </div>
                                <div class="stat-value">KES <?php echo number_format($summary['total_outstanding'], 2); ?></div>
                                <div class="stat-label">Outstanding Balance</div>
                                <div class="stat-change negative">
                                    <i class="ri-alert-line me-1"></i>
                                    <?php echo $summary['overdue_invoices']; ?> Overdue
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card">
                                <div class="stat-icon invoices">
                                    <i class="ri-file-list-3-line"></i>
                                </div>
                                <div class="stat-value"><?php echo $summary['paid_invoices'] + $summary['pending_invoices']; ?></div>
                                <div class="stat-label">Total Invoices</div>
                                <div class="stat-change">
                                    <i class="ri-hourglass-line me-1"></i>
                                    <?php echo $summary['pending_invoices']; ?> Pending
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Actions -->
                    <div class="row mb-4">
                        <div class="col-12">
                            <div class="chart-card">
                                <h5 class="mb-3">Quick Actions</h5>
                                <div class="d-flex flex-wrap gap-2">
                                    <a href="create_invoice.php" class="btn btn-primary quick-action-btn">
                                        <i class="ri-file-add-line"></i> Create Invoice
                                    </a>
                                    <a href="record_payment.php" class="btn btn-success quick-action-btn">
                                        <i class="ri-money-dollar-circle-line"></i> Record Payment
                                    </a>
                                    <a href="pending_payments.php" class="btn btn-warning quick-action-btn">
                                        <i class="ri-hourglass-line"></i> Pending Payments
                                    </a>
                                    <a href="financial_reports.php" class="btn btn-info quick-action-btn">
                                        <i class="ri-file-chart-line"></i> Generate Report
                                    </a>
                                    <a href="mpesa_transactions.php" class="btn btn-outline-primary quick-action-btn">
                                        <i class="ri-smartphone-line"></i> M-Pesa Transactions
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- Revenue Trend Chart -->
                        <div class="col-xl-8">
                            <div class="chart-card">
                                <div class="d-flex justify-content-between align-items-center mb-3">
                                    <h5 class="chart-title mb-0">Revenue Trend (Last 30 Days)</h5>
                                    <div class="btn-group btn-group-sm">
                                        <button type="button" class="btn btn-outline-primary active">Daily</button>
                                        <button type="button" class="btn btn-outline-primary">Weekly</button>
                                        <button type="button" class="btn btn-outline-primary">Monthly</button>
                                    </div>
                                </div>
                                <canvas id="revenueTrendChart" height="80"></canvas>
                            </div>

                            <!-- Payment Methods Distribution -->
                            <div class="chart-card">
                                <h5 class="chart-title">Payment Methods Distribution</h5>
                                <div class="row">
                                    <?php foreach ($payment_stats as $stat): ?>
                                    <div class="col-md-6 mb-3">
                                        <div style="padding: 1rem; border: 1px solid #e2e8f0; border-radius: 8px;">
                                            <div class="d-flex justify-content-between align-items-center mb-2">
                                                <span class="payment-mode-badge <?php echo strtolower(str_replace('_', '', $stat['payment_method'])); ?>">
                                                    <?php echo strtoupper(str_replace('_', ' ', $stat['payment_method'])); ?>
                                                </span>
                                                <strong><?php echo $stat['total_payments']; ?> txns</strong>
                                            </div>
                                            <div style="font-size: 1.25rem; font-weight: 700; color: #1e293b;">
                                                KES <?php echo number_format($stat['total_amount'], 2); ?>
                                            </div>
                                            <div style="font-size: 0.75rem; color: #64748b; margin-top: 0.25rem;">
                                                Today: <?php echo $stat['today_count']; ?> txns (KES <?php echo number_format($stat['today_amount'], 2); ?>)
                                            </div>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        </div>

                        <!-- Right Sidebar -->
                        <div class="col-xl-4">
                            <!-- Revenue Sources -->
                            <div class="chart-card">
                                <h5 class="chart-title mb-3">Top Revenue Sources</h5>
                                <?php if (!empty($revenue_sources)): ?>
                                    <?php foreach ($revenue_sources as $source): ?>
                                    <div class="revenue-source-item">
                                        <div>
                                            <div style="font-weight: 600; color: #1e293b;">
                                                <?php echo ucfirst(strtolower(str_replace('_', ' ', $source['source']))); ?>
                                            </div>
                                            <div style="font-size: 0.75rem; color: #64748b;">
                                                <?php echo $source['count']; ?> transactions
                                            </div>
                                        </div>
                                        <div style="font-weight: 700; color: #059669;">
                                            KES <?php echo number_format($source['revenue'], 2); ?>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <p class="text-muted">No revenue data available</p>
                                <?php endif; ?>
                            </div>

                            <!-- Insurance Claims Summary -->
                            <div class="chart-card">
                                <h5 class="chart-title mb-3">Insurance Claims</h5>
                                <div class="mb-3">
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>Total Claims</span>
                                        <strong><?php echo $insurance_summary['total_claims']; ?></strong>
                                    </div>
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>Total Amount</span>
                                        <strong class="text-primary">KES <?php echo number_format($insurance_summary['total_claimed'], 2); ?></strong>
                                    </div>
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>Pending Claims</span>
                                        <strong class="text-warning"><?php echo $insurance_summary['pending_claims']; ?></strong>
                                    </div>
                                    <div class="d-flex justify-content-between">
                                        <span>Pending Amount</span>
                                        <strong class="text-warning">KES <?php echo number_format($insurance_summary['pending_amount'], 2); ?></strong>
                                    </div>
                                </div>
                                <a href="track_insurance_claims.php" class="btn btn-outline-primary btn-sm w-100">
                                    View All Claims <i class="ri-arrow-right-line ms-1"></i>
                                </a>
                            </div>

                            <!-- Recent Transactions -->
                            <div class="chart-card">
                                <h5 class="chart-title mb-3">Recent Transactions</h5>
                                <div class="transaction-list">
                                    <?php if (!empty($recent_transactions)): ?>
                                        <?php foreach ($recent_transactions as $txn): ?>
                                        <div class="transaction-item">
                                            <div class="d-flex justify-content-between align-items-start mb-1">
                                                <div style="flex: 1;">
                                                    <div style="font-weight: 600; color: #1e293b; font-size: 0.875rem;">
                                                        <?php echo htmlspecialchars($txn['patient_name']); ?>
                                                    </div>
                                                    <div style="font-size: 0.75rem; color: #64748b;">
                                                        <?php echo $txn['patient_unique_id']; ?> • 
                                                        <?php echo $txn['payment_number']; ?>
                                                    </div>
                                                </div>
                                                <div style="text-align: right;">
                                                    <div style="font-weight: 700; color: #059669; font-size: 0.875rem;">
                                                        KES <?php echo number_format($txn['amount'], 2); ?>
                                                    </div>
                                                    <span class="payment-mode-badge <?php echo strtolower(str_replace('_', '', $txn['payment_method'])); ?>">
                                                        <?php echo str_replace('_', ' ', $txn['payment_method']); ?>
                                                    </span>
                                                </div>
                                            </div>
                                            <div style="font-size: 0.75rem; color: #94a3b8;">
                                                <i class="ri-time-line me-1"></i>
                                                <?php echo date('M d, Y H:i', strtotime($txn['payment_date'])); ?>
                                            </div>
                                        </div>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <p class="text-muted text-center py-3">No recent transactions</p>
                                    <?php endif; ?>
                                </div>
                                <a href="list_payments.php" class="btn btn-outline-secondary btn-sm w-100 mt-2">
                                    View All Transactions <i class="ri-arrow-right-line ms-1"></i>
                                </a>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="assets/libs/sweetalert2/sweetalert2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
    <script src="assets/js/app.js"></script>
    
    <script>
        // Revenue Trend Chart
        const revenueTrendData = <?php echo json_encode($revenue_trend); ?>;
        const labels = revenueTrendData.map(item => {
            const date = new Date(item.payment_day);
            return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
        });
        const data = revenueTrendData.map(item => parseFloat(item.daily_revenue));
        
        const ctx = document.getElementById('revenueTrendChart');
        new Chart(ctx, {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Daily Revenue (KES)',
                    data: data,
                    borderColor: 'rgb(102, 126, 234)',
                    backgroundColor: 'rgba(102, 126, 234, 0.1)',
                    tension: 0.4,
                    fill: true
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: true,
                plugins: {
                    legend: {
                        display: false
                    },
                    tooltip: {
                        callbacks: {
                            label: function(context) {
                                return 'KES ' + context.parsed.y.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});
                            }
                        }
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            callback: function(value) {
                                return 'KES ' + value.toLocaleString();
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>
<?php mysqli_close($conn); ?>