{% extends "base.html" %}
{% block content %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/statistics.css') }}">
<div class="statistics_container">
    <!-- 本周统计 -->
    <div class="statistics_section">
        <h2 class="statistics_title">本周统计
            <a href="{{ url_for('static', filename='file/关于政企交付运营中心2025年项目运维及交付实施激励方案.pdf') }}" 
            class="incentive_button" target="_blank">📄 关于政企交付运营中心2025年项目运维及交付实施激励方案</a>
        </h2>
        <!-- 普通工单统计 -->
        <div class="statistics_card">
            <h3>普通工单</h3>
            {% if current_user.role == '统计员' %}
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">派单总数</span>
                    <span class="statistics_value">{{ week_work_stats.total }}</span>
                </div>
                <div class="statistics_item">
                    <span class="statistics_label">已处理</span>
                    <span class="statistics_value">{{ week_work_stats.completed }}</span>
                </div>
                <div class="statistics_item">
                    <span class="statistics_label">未处理</span>
                    <span class="statistics_value">{{ week_work_stats.pending }}</span>
                </div>
            </div>
            {% else %}
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">已处理工单</span>
                    <span class="statistics_value">{{ week_work_stats.completed }}</span>
                </div>
            </div>
            {% endif %}
        </div>
        
        <!-- 手工故障单统计 -->
        <div class="statistics_card">
            <h3>手工故障单</h3>
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">处理完成数量</span>
                    <span class="statistics_value">{{ week_manual_stats }}</span>
                </div>
            </div>
        </div>
        
        <!-- 运维奖励统计 -->
        <div class="statistics_card">
            <h3>运维奖励（元）</h3>
            <div class="statistics_grid">
                {% if week_rewards is mapping %}
                {% for project_type, reward in week_rewards.items() %}
                <div class="statistics_item">
                    <span class="statistics_label">{{ project_type }}</span>
                    <span class="statistics_value">{{ "%.2f"|format(reward) }}</span>
                </div>
                {% endfor %}
                {% endif %}
                <div class="statistics_item total_reward">
                    <span class="statistics_label">总计</span>
                    <span class="statistics_value">{{ "%.2f"|format(week_rewards.values()|sum) if week_rewards is mapping else "%.2f"|format(week_rewards|default(0)) }}</span>
                </div>
            </div>
        </div>
    </div>

    <!-- 本月统计 -->
    <div class="statistics_section">
        <h2 class="statistics_title">本月统计
            {% if current_user.role == '统计员' %}
            <button onclick="showMonthPicker()" class="export_button">📥︎ 导出往月明细</button>
            {% endif %}
        </h2>
        
        <!-- 普通工单统计 -->
        <div class="statistics_card">
            <h3>普通工单</h3>
            {% if current_user.role == '统计员' %}
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">派单总数</span>
                    <span class="statistics_value">{{ month_work_stats.total }}</span>
                </div>
                <div class="statistics_item">
                    <span class="statistics_label">已处理</span>
                    <span class="statistics_value">{{ month_work_stats.completed }}</span>
                </div>
                <div class="statistics_item">
                    <span class="statistics_label">未处理</span>
                    <span class="statistics_value">{{ month_work_stats.pending }}</span>
                </div>
            </div>
            {% else %}
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">已处理工单</span>
                    <span class="statistics_value">{{ month_work_stats.completed }}</span>
                </div>
            </div>
            {% endif %}
        </div>
        
        <!-- 手工故障单统计 -->
        <div class="statistics_card">
            <h3>手工故障单</h3>
            <div class="statistics_grid">
                <div class="statistics_item">
                    <span class="statistics_label">处理完成数量</span>
                    <span class="statistics_value">{{ month_manual_stats }}</span>
                </div>
            </div>
        </div>
        
        <!-- 运维奖励统计 -->
        <div class="statistics_card">
            <h3>运维奖励（元）</h3>
            <div class="statistics_grid">
                {% if month_rewards is mapping %}
                {% for project_type, reward in month_rewards.items() %}
                <div class="statistics_item">
                    <span class="statistics_label">{{ project_type }}</span>
                    <span class="statistics_value">{{ "%.2f"|format(reward) }}</span>
                </div>
                {% endfor %}
                {% endif %}
                <div class="statistics_item total_reward">
                    <span class="statistics_label">总计</span>
                    <span class="statistics_value">{{ "%.2f"|format(month_rewards.values()|sum) if month_rewards is mapping else "%.2f"|format(month_rewards|default(0)) }}</span>
                </div>
            </div>
        </div>
        {% if current_user.role == '统计员' %}
    <div class="statistics_section">
        <h2 class="statistics_title">本周奖励统计</h2>
        <div class="statistics_card">
            <div class="handler_stats_table">
                <table>
                    <thead>
                        <tr>
                            <th>处理人</th>
                            <th>普通工单</th>
                            <th>手工工单</th>
                            <th>校园安防"4+N"项目</th>
                            <th>综治视联网</th>
                            <th>教育城域网</th>
                            <th>其他</th>
                            <th>预计奖励</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for handler, stats in week_handler_stats.items() %}
                        <tr>
                            <td>{{ handler }}</td>
                            <td>{{ stats.work_orders }}</td>
                            <td>{{ stats.manual_orders }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['校园安防"4+N"项目']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['综治视联网']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['教育城域网']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['其他']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards.values()|sum) }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="statistics_section">
        <h2 class="statistics_title">本月奖励统计</h2>
        <div class="statistics_card">
            <div class="handler_stats_table">
                <table>
                    <thead>
                        <tr>
                            <th>处理人</th>
                            <th>普通工单</th>
                            <th>手工工单</th>
                            <th>校园安防"4+N"项目</th>
                            <th>综治视联网</th>
                            <th>教育城域网</th>
                            <th>其他</th>
                            <th>预计奖励</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for handler, stats in month_handler_stats.items() %}
                        <tr>
                            <td>{{ handler }}</td>
                            <td>{{ stats.work_orders }}</td>
                            <td>{{ stats.manual_orders }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['校园安防"4+N"项目']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['综治视联网']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['教育城域网']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards['其他']|default(0)) }}</td>
                            <td>{{ "%.2f"|format(stats.rewards.values()|sum) }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}

    </div>
</div>
<!-- 月份选择器模态框 -->
<div id="monthPickerModal" class="modal" style="display: none;">
    <div class="modal-content">
        <h3>选择导出月份</h3>
        <div class="month-picker-form">
            <input type="month" id="monthPicker" class="month-input">
            <div class="modal-buttons">
                <button onclick="exportMonthlyStats()" class="export_button">导出</button>
                <button onclick="closeModal()" class="cancel_button">取消</button>
            </div>
        </div>
    </div>
</div>


<script>
function showMonthPicker() {
    document.getElementById('monthPickerModal').style.display = 'flex';
}

function closeModal() {
    document.getElementById('monthPickerModal').style.display = 'none';
}

function exportMonthlyStats() {
    const monthPicker = document.getElementById('monthPicker');
    const selectedMonth = monthPicker.value; // 格式：YYYY-MM
    if (!selectedMonth) {
        alert('请选择月份');
        return;
    }
    // 将 YYYY-MM 格式拆分为年和月
    const [year, month] = selectedMonth.split('-');
    window.location.href = `/export_monthly_stats/${year}/${month}`;
    closeModal();
}

// 点击模态框外部关闭
document.getElementById('monthPickerModal').addEventListener('click', function(e) {
    if (e.target === this) {
        closeModal();
    }
});
</script>
{% endblock %}