<?php
require_once 'conn.php';
checkLogin();
//检查是否管理员
checkPermissionDie(1,2);

// 计算日期范围
$current_month_start = date('Y-m-01');
$current_month_end = date('Y-m-t');
$last_month_start = date('Y-m-01', strtotime('-1 month'));
$last_month_end = date('Y-m-t', strtotime('-1 month'));
$current_year_start = date('Y-01-01');
$current_year_end = date('Y-12-31');

// 可选的日期范围筛选
$date_range = isset($_GET['date_range']) ? $_GET['date_range'] : 'current_month';
$custom_start = isset($_GET['start_date']) ? $_GET['start_date'] : '';
$custom_end = isset($_GET['end_date']) ? $_GET['end_date'] : '';

// 设置日期范围
if ($date_range == 'custom' && !empty($custom_start) && !empty($custom_end)) {
    $start_date = $custom_start;
    $end_date = $custom_end;
} else {
    switch ($date_range) {
        case 'last_month':
            $start_date = $last_month_start;
            $end_date = $last_month_end;
            break;
        case 'current_year':
            $start_date = $current_year_start;
            $end_date = $current_year_end;
            break;
        case 'last_30_days':
            $start_date = date('Y-m-d', strtotime('-30 days'));
            $end_date = date('Y-m-d');
            break;
        case 'last_90_days':
            $start_date = date('Y-m-d', strtotime('-90 days'));
            $end_date = date('Y-m-d');
            break;
        case 'current_month':
        default:
            $start_date = $current_month_start;
            $end_date = $current_month_end;
            break;
    }
}

// 格式化日期用于SQL查询
$start_date_sql = date('Y-m-d', strtotime($start_date));
$end_date_sql = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';

// 函数:获取销售概览数据
function getSalesOverview($conn, $start_date, $end_date) {
    $sql = "SELECT 
                COUNT(id) as total_orders,
                SUM(total_amount) as total_revenue,
                AVG(total_amount) as avg_order_value
            FROM orders 
            WHERE order_date BETWEEN ? AND ?";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $start_date, $end_date);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result->fetch_assoc();
}

// 函数:获取每月销售趋势
function getMonthlySalesTrend($conn, $start_date, $end_date) {
    $sql = "SELECT 
                DATE_FORMAT(order_date, '%Y-%m') as month,
                COUNT(id) as orders,
                SUM(total_amount) as revenue
            FROM orders 
            WHERE order_date BETWEEN ? AND ?
            GROUP BY DATE_FORMAT(order_date, '%Y-%m')
            ORDER BY month";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $start_date, $end_date);
    $stmt->execute();
    return $stmt->get_result();
}

// 函数:获取客户国家分布
function getCustomerCountryDistribution($conn) {
    $sql = "SELECT 
                c.countryName, 
                COUNT(cu.id) as customer_count
            FROM customer cu
            JOIN country c ON cu.cs_country = c.id
            GROUP BY cu.cs_country
            ORDER BY customer_count DESC
            LIMIT 10";
    
    return $conn->query($sql);
}

// 函数:获取客户类型分布
function getCustomerTypeDistribution($conn) {
    $sql = "SELECT 
                ct.businessType, 
                COUNT(c.id) as customer_count
            FROM customer c
            JOIN clienttype ct ON c.cs_type = ct.id
            GROUP BY c.cs_type";
    
    return $conn->query($sql);
}

// 函数:获取成交阶段分布
function getDealStageDistribution($conn) {
    $sql = "SELECT 
                cs_deal,
                CASE 
                    WHEN cs_deal = 1 THEN '背景调查'
                    WHEN cs_deal = 2 THEN '明确需求'
                    WHEN cs_deal = 3 THEN '已成交'
                    ELSE '其他'
                END as stage_name,
                COUNT(id) as customer_count
            FROM customer
            GROUP BY cs_deal";
    
    return $conn->query($sql);
}

// 函数:获取热门产品
function getTopProducts($conn, $start_date, $end_date, $limit = 5) {
    $sql = "SELECT 
                p.ProductName, 
                SUM(oi.quantity) as total_quantity,
                SUM(oi.total_price) as total_revenue
            FROM order_items oi
            JOIN products p ON oi.product_id = p.id
            JOIN orders o ON oi.order_id = o.id
            WHERE o.order_date BETWEEN ? AND ?
            GROUP BY oi.product_id
            ORDER BY total_revenue DESC
            LIMIT ?";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssi", $start_date, $end_date, $limit);
    $stmt->execute();
    return $stmt->get_result();
}

// 函数:获取业务员销售业绩
function getEmployeeSalesPerformance($conn, $start_date, $end_date) {
    $sql = "SELECT 
                e.em_user as employee_name,
                COUNT(o.id) as order_count,
                SUM(o.total_amount) as total_sales
            FROM orders o
            JOIN employee e ON o.employee_id = e.id
            WHERE o.order_date BETWEEN ? AND ?
            GROUP BY o.employee_id
            ORDER BY total_sales DESC";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $start_date, $end_date);
    $stmt->execute();
    return $stmt->get_result();
}

// 函数:获取客户增长趋势
function getCustomerGrowthTrend($conn) {
    $sql = "SELECT 
                DATE_FORMAT(cs_addtime, '%Y-%m') as month,
                COUNT(id) as new_customers
            FROM customer
            WHERE cs_addtime >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
            GROUP BY DATE_FORMAT(cs_addtime, '%Y-%m')
            ORDER BY month";
    
    return $conn->query($sql);
}

// 函数:获取不同地区的订单数量
function getOrdersByRegion($conn, $start_date, $end_date) {
    $sql = "SELECT 
                c.countryName, 
                COUNT(o.id) as order_count,
                SUM(o.total_amount) as total_amount,
                SUM(oi.quantity) as total_quantity
            FROM orders o
            JOIN customer cu ON o.customer_id = cu.id
            JOIN country c ON cu.cs_country = c.id
            LEFT JOIN order_items oi ON o.id = oi.order_id
            WHERE o.order_date BETWEEN ? AND ?
            GROUP BY cu.cs_country
            ORDER BY total_quantity DESC
            LIMIT 10";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $start_date, $end_date);
    $stmt->execute();
    return $stmt->get_result();
}

// 函数:获取详细时间段订单数量
function getDetailedOrderTrend($conn, $start_date, $end_date, $period = 'day') {
    $groupFormat = '%Y-%m-%d';
    $intervalUnit = 'DAY';
    
    if ($period == 'week') {
        $groupFormat = '%x-W%v'; // ISO year and week number
        $intervalUnit = 'WEEK';
    } else if ($period == 'month') {
        $groupFormat = '%Y-%m';
        $intervalUnit = 'MONTH';
    }
    
    $sql = "SELECT 
                DATE_FORMAT(o.order_date, '$groupFormat') as time_period,
                COUNT(o.id) as order_count,
                SUM(oi.quantity) as total_quantity
            FROM orders o
            LEFT JOIN order_items oi ON o.id = oi.order_id
            WHERE o.order_date BETWEEN ? AND ?
            GROUP BY time_period
            ORDER BY MIN(o.order_date)";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $start_date, $end_date);
    $stmt->execute();
    return $stmt->get_result();
}

// 获取统计数据
$sales_overview = getSalesOverview($conn, $start_date_sql, $end_date_sql);
$monthly_sales = getMonthlySalesTrend($conn, $start_date_sql, $end_date_sql);
$country_distribution = getCustomerCountryDistribution($conn);
$customer_types = getCustomerTypeDistribution($conn);
$deal_stages = getDealStageDistribution($conn);
$top_products = getTopProducts($conn, $start_date_sql, $end_date_sql);
$employee_performance = getEmployeeSalesPerformance($conn, $start_date_sql, $end_date_sql);
$customer_growth = getCustomerGrowthTrend($conn);
$orders_by_region = getOrdersByRegion($conn, $start_date_sql, $end_date_sql);

// 获取详细时间段订单趋势 - 默认按日期
$period = isset($_GET['period']) ? $_GET['period'] : 'day';
$detailed_orders = getDetailedOrderTrend($conn, $start_date_sql, $end_date_sql, $period);

// 将月度销售数据转换为图表所需格式
$monthly_labels = [];
$monthly_orders = [];
$monthly_revenue = [];

while ($row = $monthly_sales->fetch_assoc()) {
    $monthly_labels[] = $row['month'];
    $monthly_orders[] = $row['orders'];
    $monthly_revenue[] = $row['revenue'];
}

// 将国家分布数据转换为图表所需格式
$country_labels = [];
$country_data = [];

while ($row = $country_distribution->fetch_assoc()) {
    $country_labels[] = $row['countryName'];
    $country_data[] = $row['customer_count'];
}

// 将客户类型数据转换为图表所需格式
$type_labels = [];
$type_data = [];

while ($row = $customer_types->fetch_assoc()) {
    $type_labels[] = $row['businessType'];
    $type_data[] = $row['customer_count'];
}

// 将成交阶段数据转换为图表所需格式
$stage_labels = [];
$stage_data = [];

while ($row = $deal_stages->fetch_assoc()) {
    $stage_labels[] = $row['stage_name'];
    $stage_data[] = $row['customer_count'];
}

// 将客户增长数据转换为图表所需格式
$growth_labels = [];
$growth_data = [];

while ($row = $customer_growth->fetch_assoc()) {
    $growth_labels[] = $row['month'];
    $growth_data[] = $row['new_customers'];
}

// 将地区订单数据转换为图表所需格式
$region_labels = [];
$region_orders = [];
$region_quantities = [];

while ($row = $orders_by_region->fetch_assoc()) {
    $region_labels[] = $row['countryName'];
    $region_orders[] = $row['order_count'];
    $region_quantities[] = $row['total_quantity'];
}

// 将详细时间订单数据转换为图表所需格式
$time_labels = [];
$time_orders = [];
$time_quantities = [];

while ($row = $detailed_orders->fetch_assoc()) {
    $time_labels[] = $row['time_period'];
    $time_orders[] = $row['order_count'];
    $time_quantities[] = $row['total_quantity'];
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>统计分析</title>
    <link rel="stylesheet" href="css/common.css" type="text/css" />
    <script src="system/js/jquery-1.7.2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background: #fff;
            font-family: Arial, sans-serif;
        }
        
        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .page-header {
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .page-title {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin: 0;
        }
        
        .export-btn {
            padding: 8px 15px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        
        .export-btn:hover {
            background: #45a049;
        }
        
        .filter-form {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: center;
        }
        
        .form-group {
            margin-right: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        
        .form-control {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            min-width: 150px;
        }
        
        .btn {
            padding: 8px 15px;
            background: #337ab7;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        
        .btn:hover {
            background: #286090;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background: white;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 20px;
        }
        
        .stat-card h3 {
            margin-top: 0;
            color: #555;
            font-size: 16px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        
        .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin: 15px 0;
        }
        
        .chart-container {
            margin-bottom: 30px;
            background: white;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 20px;
        }
        
        .chart-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .chart-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin: 0;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .data-table th, .data-table td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        .data-table th {
            background: #f5f5f5;
            font-weight: bold;
        }
        
        .data-table tr:hover {
            background: #f9f9f9;
        }
        
        .custom-date-inputs {
            display: none;
        }
        
        #date_range[value="custom"]:checked ~ .custom-date-inputs {
            display: block;
        }
        
        .download-btn {
            padding: 5px 10px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            font-size: 14px;
            cursor: pointer;
        }
        
        .download-btn:hover {
            background: #45a049;
        }
        
        /* 确保筛选表单元素在同一行显示 */
        .filter-form form {
            display: flex;
            flex-direction: row;
            align-items: flex-end;
            gap: 15px;
            flex-wrap: nowrap;
            width: 100%;
        }
        .filter-form .form-group {
            margin-right: 0;
            flex: 0 0 auto;
            white-space: nowrap;
        }
        .filter-form label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .filter-form #custom_dates {
            display: flex;
            flex-direction: row;
            gap: 10px;
            flex: 0 0 auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="page-header">
            <h1 class="page-title">统计分析</h1>
            <a href="export_statistics.php?date_range=<?php echo $date_range; ?>&start_date=<?php echo $custom_start; ?>&end_date=<?php echo $custom_end; ?>&period=<?php echo $period; ?>" class="export-btn">导出数据</a>
        </div>
        
        <div class="filter-form">
            <form method="get" action="">
                <div class="form-group">
                    <label>日期范围</label>
                    <select name="date_range" id="date_range" class="form-control" onchange="toggleCustomDates()">
                        <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
                        <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
                        <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>过去30天</option>
                        <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>过去90天</option>
                        <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
                        <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义</option>
                    </select>
                </div>
                
                <div id="custom_dates" style="display: <?php echo $date_range == 'custom' ? 'flex' : 'none'; ?>;">
                    <div class="form-group">
                        <label>开始日期</label>
                        <input type="date" name="start_date" class="form-control" value="<?php echo $custom_start; ?>">
                    </div>
                    <div class="form-group">
                        <label>结束日期</label>
                        <input type="date" name="end_date" class="form-control" value="<?php echo $custom_end; ?>">
                    </div>
                </div>
                
                <div class="form-group">
                    <label>时间粒度</label>
                    <select name="period" class="form-control">
                        <option value="day" <?php echo $period == 'day' ? 'selected' : ''; ?>>按日</option>
                        <option value="week" <?php echo $period == 'week' ? 'selected' : ''; ?>>按周</option>
                        <option value="month" <?php echo $period == 'month' ? 'selected' : ''; ?>>按月</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <input type="submit" class="btn" value="应用筛选">
                </div>
            </form>
        </div>
        
        <!-- 销售概览卡片 -->
        <div class="stats-grid">
            <div class="stat-card">
                <h3>总订单数</h3>
                <div class="stat-value"><?php echo number_format($sales_overview['total_orders']); ?></div>
            </div>
            
            <div class="stat-card">
                <h3>总收入</h3>
                <div class="stat-value">¥<?php echo number_format($sales_overview['total_revenue'], 2); ?></div>
            </div>
            
            <div class="stat-card">
                <h3>平均订单金额</h3>
                <div class="stat-value">¥<?php echo number_format($sales_overview['avg_order_value'], 2); ?></div>
            </div>
        </div>
        
        <!-- 月度销售趋势图 -->
        <div class="chart-container">
            <div class="chart-header">
                <h2 class="chart-title">销售趋势</h2>
            </div>
            <canvas id="salesTrendChart"></canvas>
        </div>
        
        <!-- 客户分布图 -->
        <div class="stats-grid">
            <div class="chart-container">
                <div class="chart-header">
                    <h2 class="chart-title">客户国家分布</h2>
                </div>
                <canvas id="countryDistributionChart"></canvas>
            </div>
            
            <div class="chart-container">
                <div class="chart-header">
                    <h2 class="chart-title">客户类型分布</h2>
                </div>
                <canvas id="customerTypeChart"></canvas>
            </div>
        </div>
        
        <div class="stats-grid">
            <div class="chart-container">
                <div class="chart-header">
                    <h2 class="chart-title">成交阶段分布</h2>
                </div>
                <canvas id="dealStageChart"></canvas>
            </div>
            
            <div class="chart-container">
                <div class="chart-header">
                    <h2 class="chart-title">客户增长趋势</h2>
                </div>
                <canvas id="customerGrowthChart"></canvas>
            </div>
        </div>
        
        <!-- 地区订单分析 -->
        <div class="chart-container">
            <div class="chart-header">
                <h2 class="chart-title">地区订单分析</h2>
            </div>
            <canvas id="regionOrdersChart"></canvas>
        </div>
        
        <!-- 详细时间段订单趋势 -->
        <div class="chart-container">
            <div class="chart-header">
                <h2 class="chart-title">详细订单趋势 (<?php echo $period == 'day' ? '日' : ($period == 'week' ? '周' : '月'); ?>)</h2>
            </div>
            <canvas id="detailedOrdersChart"></canvas>
        </div>
        
        <!-- 热门产品表格 -->
        <div class="chart-container">
            <div class="chart-header">
                <h2 class="chart-title">热门产品</h2>
            </div>
            <table class="data-table">
                <thead>
                    <tr>
                        <th>产品名称</th>
                        <th>销售数量</th>
                        <th>销售收入</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $top_products->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['ProductName']); ?></td>
                        <td><?php echo number_format($row['total_quantity']); ?></td>
                        <td>¥<?php echo number_format($row['total_revenue'], 2); ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        
        <!-- 业务员销售业绩表格 -->
        <div class="chart-container">
            <div class="chart-header">
                <h2 class="chart-title">业务员销售业绩</h2>
            </div>
            <table class="data-table">
                <thead>
                    <tr>
                        <th>业务员姓名</th>
                        <th>订单数量</th>
                        <th>销售总额</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $employee_performance->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
                        <td><?php echo number_format($row['order_count']); ?></td>
                        <td>¥<?php echo number_format($row['total_sales'], 2); ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <script>
        // 切换自定义日期区域显示
        function toggleCustomDates() {
            var dateRange = document.getElementById('date_range').value;
            var customDates = document.getElementById('custom_dates');
            
            if (dateRange === 'custom') {
                customDates.style.display = 'flex';
            } else {
                customDates.style.display = 'none';
            }
        }
        
        // 销售趋势图
        var salesTrendCtx = document.getElementById('salesTrendChart').getContext('2d');
        var salesTrendChart = new Chart(salesTrendCtx, {
            type: 'line',
            data: {
                labels: <?php echo json_encode($monthly_labels); ?>,
                datasets: [
                    {
                        label: '订单数量',
                        data: <?php echo json_encode($monthly_orders); ?>,
                        backgroundColor: 'rgba(54, 162, 235, 0.2)',
                        borderColor: 'rgba(54, 162, 235, 1)',
                        borderWidth: 2,
                        yAxisID: 'y-orders'
                    },
                    {
                        label: '销售收入',
                        data: <?php echo json_encode($monthly_revenue); ?>,
                        backgroundColor: 'rgba(255, 99, 132, 0.2)',
                        borderColor: 'rgba(255, 99, 132, 1)',
                        borderWidth: 2,
                        yAxisID: 'y-revenue'
                    }
                ]
            },
            options: {
                responsive: true,
                scales: {
                    'y-orders': {
                        type: 'linear',
                        position: 'left',
                        title: {
                            display: true,
                            text: '订单数量'
                        }
                    },
                    'y-revenue': {
                        type: 'linear',
                        position: 'right',
                        title: {
                            display: true,
                            text: '销售收入'
                        },
                        grid: {
                            drawOnChartArea: false
                        }
                    }
                }
            }
        });
        
        // 客户国家分布图
        var countryDistributionCtx = document.getElementById('countryDistributionChart').getContext('2d');
        var countryDistributionChart = new Chart(countryDistributionCtx, {
            type: 'pie',
            data: {
                labels: <?php echo json_encode($country_labels); ?>,
                datasets: [{
                    data: <?php echo json_encode($country_data); ?>,
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.7)',
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(255, 206, 86, 0.7)',
                        'rgba(75, 192, 192, 0.7)',
                        'rgba(153, 102, 255, 0.7)',
                        'rgba(255, 159, 64, 0.7)',
                        'rgba(199, 199, 199, 0.7)',
                        'rgba(83, 102, 255, 0.7)',
                        'rgba(40, 159, 64, 0.7)',
                        'rgba(210, 199, 199, 0.7)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'right',
                    }
                }
            }
        });
        
        // 客户类型分布图
        var customerTypeCtx = document.getElementById('customerTypeChart').getContext('2d');
        var customerTypeChart = new Chart(customerTypeCtx, {
            type: 'doughnut',
            data: {
                labels: <?php echo json_encode($type_labels); ?>,
                datasets: [{
                    data: <?php echo json_encode($type_data); ?>,
                    backgroundColor: [
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(255, 99, 132, 0.7)',
                        'rgba(255, 206, 86, 0.7)',
                        'rgba(75, 192, 192, 0.7)',
                        'rgba(153, 102, 255, 0.7)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'right',
                    }
                }
            }
        });
        
        // 成交阶段分布图
        var dealStageCtx = document.getElementById('dealStageChart').getContext('2d');
        var dealStageChart = new Chart(dealStageCtx, {
            type: 'bar',
            data: {
                labels: <?php echo json_encode($stage_labels); ?>,
                datasets: [{
                    label: '客户数量',
                    data: <?php echo json_encode($stage_data); ?>,
                    backgroundColor: [
                        'rgba(255, 206, 86, 0.7)',
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(255, 99, 132, 0.7)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                scales: {
                    y: {
                        beginAtZero: true,
                        title: {
                            display: true,
                            text: '客户数量'
                        }
                    }
                }
            }
        });
        
        // 客户增长趋势图
        var customerGrowthCtx = document.getElementById('customerGrowthChart').getContext('2d');
        var customerGrowthChart = new Chart(customerGrowthCtx, {
            type: 'line',
            data: {
                labels: <?php echo json_encode($growth_labels); ?>,
                datasets: [{
                    label: '新增客户',
                    data: <?php echo json_encode($growth_data); ?>,
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 2,
                    tension: 0.1
                }]
            },
            options: {
                responsive: true,
                scales: {
                    y: {
                        beginAtZero: true,
                        title: {
                            display: true,
                            text: '客户数量'
                        }
                    }
                }
            }
        });
        
        // 地区订单分析图
        var regionOrdersCtx = document.getElementById('regionOrdersChart').getContext('2d');
        var regionOrdersChart = new Chart(regionOrdersCtx, {
            type: 'bar',
            data: {
                labels: <?php echo json_encode($region_labels); ?>,
                datasets: [
                    {
                        label: '订单数量',
                        data: <?php echo json_encode($region_orders); ?>,
                        backgroundColor: 'rgba(54, 162, 235, 0.6)',
                        borderColor: 'rgba(54, 162, 235, 1)',
                        borderWidth: 1,
                        yAxisID: 'y-orders'
                    },
                    {
                        label: '产品订购数量',
                        data: <?php echo json_encode($region_quantities); ?>,
                        backgroundColor: 'rgba(255, 99, 132, 0.6)',
                        borderColor: 'rgba(255, 99, 132, 1)',
                        borderWidth: 1,
                        yAxisID: 'y-quantity'
                    }
                ]
            },
            options: {
                responsive: true,
                scales: {
                    x: {
                        title: {
                            display: true,
                            text: '地区'
                        }
                    },
                    'y-orders': {
                        type: 'linear',
                        position: 'left',
                        title: {
                            display: true,
                            text: '订单数量'
                        },
                        beginAtZero: true
                    },
                    'y-quantity': {
                        type: 'linear',
                        position: 'right',
                        title: {
                            display: true,
                            text: '产品订购数量'
                        },
                        beginAtZero: true,
                        grid: {
                            drawOnChartArea: false
                        }
                    }
                }
            }
        });
        
        // 详细时间段订单趋势图
        var detailedOrdersCtx = document.getElementById('detailedOrdersChart').getContext('2d');
        var detailedOrdersChart = new Chart(detailedOrdersCtx, {
            type: 'line',
            data: {
                labels: <?php echo json_encode($time_labels); ?>,
                datasets: [
                    {
                        label: '订单数量',
                        data: <?php echo json_encode($time_orders); ?>,
                        backgroundColor: 'rgba(75, 192, 192, 0.2)',
                        borderColor: 'rgba(75, 192, 192, 1)',
                        borderWidth: 2,
                        yAxisID: 'y-orders',
                        tension: 0.1
                    },
                    {
                        label: '产品订购数量',
                        data: <?php echo json_encode($time_quantities); ?>,
                        backgroundColor: 'rgba(255, 159, 64, 0.2)',
                        borderColor: 'rgba(255, 159, 64, 1)',
                        borderWidth: 2,
                        yAxisID: 'y-quantity',
                        tension: 0.1
                    }
                ]
            },
            options: {
                responsive: true,
                scales: {
                    x: {
                        title: {
                            display: true,
                            text: '时间'
                        }
                    },
                    'y-orders': {
                        type: 'linear',
                        position: 'left',
                        title: {
                            display: true,
                            text: '订单数量'
                        },
                        beginAtZero: true
                    },
                    'y-quantity': {
                        type: 'linear',
                        position: 'right',
                        title: {
                            display: true,
                            text: '产品订购数量'
                        },
                        beginAtZero: true,
                        grid: {
                            drawOnChartArea: false
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>