<?php
/**
 * 客户统计分析展示页面
 */
require_once 'conn.php';
require_once 'statistics_utils.php';
require_once 'statistics_customers.php';

// 检查登录状态
if (!isset($_SESSION['employee_id'])) {
    checkLogin();
}

// 获取日期范围参数
$date_params = getDateRangeParams();
$start_date = $date_params['start_date_sql'];
$end_date = $date_params['end_date_sql'];
$date_range = $date_params['date_range'];
$period = $date_params['period'];

// 页面头部
include('statistics_header.php');
?>

<div class="container">
    <div class="page-header">
        <h1 class="page-title">客户统计分析</h1>
    </div>
    
    <!-- 日期筛选 -->
    <div class="filter-form">
        <form method="get" class="filter-form-inline">
            <div class="form-group">
                <label for="date_range">选择日期范围</label>
                <select class="form-control" id="date_range" name="date_range" 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="current_year" <?php echo $date_range == 'current_year' ? '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="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
                </select>
            </div>
            <div class="form-group custom-date-inputs" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
                <label for="start_date">开始日期</label>
                <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
            </div>
            <div class="form-group custom-date-inputs" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
                <label for="end_date">结束日期</label>
                <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
            </div>
            <div class="form-group">
                <button type="submit" class="btn">应用筛选</button>
            </div>
        </form>
    </div>
    
    <div class="stats-grid">
        <!-- 客户类型分布 -->
        <div class="stat-card">
            <?php
            $customer_type_result = getCustomerTypeDistribution($conn);
            $type_labels = [];
            $type_data = [];
            
            while ($row = $customer_type_result->fetch_assoc()) {
                $type_labels[] = $row['businessType'];
                $type_data[] = $row['customer_count'];
            }
            renderCustomerTypeChart($type_labels, $type_data);
            ?>
        </div>
        
        <!-- 成交阶段分布 -->
        <div class="stat-card">
            <?php
            $deal_stage_result = getDealStageDistribution($conn);
            $stage_labels = [];
            $stage_data = [];
            
            while ($row = $deal_stage_result->fetch_assoc()) {
                $stage_labels[] = $row['stage_name'];
                $stage_data[] = $row['customer_count'];
            }
            renderDealStageChart($stage_labels, $stage_data);
            ?>
        </div>
    </div>
    
    <!-- 客户增长趋势 -->
    <div class="chart-container">
        <?php
        $growth_result = getCustomerGrowthTrend($conn);
        $growth_labels = [];
        $growth_data = [];
        
        while ($row = $growth_result->fetch_assoc()) {
            $growth_labels[] = $row['month'];
            $growth_data[] = $row['new_customers'];
        }
        renderCustomerGrowthChart($growth_labels, $growth_data);
        ?>
    </div>
    
    <!-- 新老客户分析 -->
    <div class="chart-container">
        <?php
        $new_vs_returning = getNewVsReturningCustomerOrders($conn, $start_date, $end_date);
        renderNewVsReturningCustomersChart($new_vs_returning);
        ?>
    </div>
</div>

<script>
function toggleCustomDates() {
    const dateRange = document.getElementById('date_range').value;
    const customDateInputs = document.querySelectorAll('.custom-date-inputs');
    
    if (dateRange === 'custom') {
        customDateInputs.forEach(el => el.style.display = 'inline-block');
    } else {
        customDateInputs.forEach(el => el.style.display = 'none');
    }
}
</script>

<?php
// 页面底部
include('statistics_footer.php');
?>