<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('政治常识监测列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" name="sitetag" value="site"/>
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>站点名称：</label>
                                <select class="type" name="sitename" style="width: 200px;">
                                    <option value="">全部站点</option>
                                    <option th:each="site : ${sites}" th:value="${site.sitename}" th:text="${site.sitename}"></option>
                                </select>
                            </li>
                            <li>
                                <label>词条：</label>
                                <input type="text" name="wrongWord"/>
                            </li>
                            <li>
                                <label>错误规则：</label>
                                <input type="text" name="wrongRule"/>
                            </li>
                            <li>
                                <label>错误类型：</label>
                                <select name="errorType" th:with="type=${@dict.getType('trs_policy_error_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>错误级别：</label>
                                <select name="errorGrade" th:with="type=${@dict.getType('trs_error_grade')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li class="select-time">
                                <label>时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status" th:with="type=${@dict.getType('trs_oper_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm btn-search" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm btn-reset" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="col-sm-12 search-collapse">
                <div class="dashboard-stat">
                    <!-- 第一组 - 蓝色背景 -->
                    <div class="stat-group group-blue">
                        <div class="stat-item">
                            <div class="stat-number" id="errorWordCount">0</div>
                            <div class="stat-title">政治常识</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="processedCount">0</div>
                            <div class="stat-title">已处理数</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="noNeedProcessCount">0</div>
                            <div class="stat-title">不必处理数</div>
                        </div>
                    </div>

                    <!-- 第二组 - 绿色背景 -->
                    <div class="stat-group group-green">
                        <div class="stat-item">
                            <div class="stat-number" id="errorWordCount2">0</div>
                            <div class="stat-title">严重表述错误</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="processedCount2">0</div>
                            <div class="stat-title">已处理数</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="noNeedProcessCount2">0</div>
                            <div class="stat-title">不必处理数</div>
                        </div>
                    </div>

                    <!-- 第三组 - 橙色背景 -->
                    <div class="stat-group group-orange">
                        <div class="stat-item">
                            <div class="stat-number" id="errorWordCount3">0</div>
                            <div class="stat-title">一般性错误</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="processedCount3">0</div>
                            <div class="stat-title">已处理数</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-number" id="noNeedProcessCount3">0</div>
                            <div class="stat-title">不必处理数</div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <!--<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="project:wxpolicyresults:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="project:wxpolicyresults:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>-->
                <a class="btn btn-success" onclick="biaoji()" shiro:hasPermission="project:wxpolicyresults:edit">
                    <i class="fa fa-edit"></i> 批量标记
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="project:wxpolicyresults:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="project:wxpolicyresults:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <style>
         /* 仪表盘统计容器 */
         .dashboard-stat {
             display: flex;
             justify-content: space-between;
             align-items: stretch; /* 使子元素高度一致 */
             background: #f8f9fa;
             border-radius: 8px;
             overflow: hidden; /* 确保圆角效果 */
             box-shadow: 0 2px 8px rgba(0,0,0,0.1);
         }

         /* 每组统计样式 */
         .stat-group {
             display: flex;
             flex: 1;
             justify-content: space-around;
             padding: 15px 0;
         }

         /* 不同组的背景颜色 */
         .group-blue {
             /*background: #e7f5ff; !* 浅蓝色 *!*/
             background: #deebfd; /* 浅蓝色 */
         }
         .group-green {
             /*background: #ebfbee; !* 浅绿色 *!*/
             background: #f2f2f2; /* 浅绿色 */
         }
         .group-orange {
             /*background: #fff4e6; !* 浅橙色 *!*/
             background: #e8eaff; /* 浅橙色 */
         }

         /* 统计项之间的分隔线 */
         .stat-group:not(:last-child) {
             border-right: 1px solid rgba(0,0,0,0.05);
         }

         /* 单个统计项 */
         .stat-item {
             text-align: center;
             padding: 0 15px;
             min-width: 80px;
         }

         .stat-number {
             font-size: 22px;
             font-weight: bold;
             line-height: 1.2;
         }

         /* 不同组的数字颜色 */
         .group-blue .stat-number {
             color: #228be6; /* 蓝色 */
         }
         .group-green .stat-number {
             color: #FF6B6B;

         }
         .group-orange .stat-number {
             color: #fd7e14; /* 橙色 */
         }

         .stat-title {
             font-size: 13px;
             color: #495057;
             margin-top: 6px;
         }
     </style>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('project:wxpolicyresults:edit')}]];
        var removeFlag = [[${@permission.hasPermi('project:wxpolicyresults:remove')}]];
        var wrongRuleDatas = [[${@dict.getType('trs_policy_rule_type')}]];
        var errorTypeDatas = [[${@dict.getType('trs_policy_error_type')}]];
        var errorGradeDatas = [[${@dict.getType('trs_error_grade')}]];
        var statusDatas = [[${@dict.getType('trs_oper_type')}]];
        var prefix = ctx + "project/policyresults";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "政治常识监测",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'sitename',
                    title: '站点名称'
                },
                {
                    field: 'page',
                    title: '所在页',
                    formatter: function(value, row, index) {
                        // 假设 row 对象中有 title 字段存储页面标题
                        var title = row.title || '无标题';  // 如果没有标题，显示"无标题"
                        var url = value;  // value 是页面URL

                        // 在新标签页中打开链接，显示标题但链接到URL
                        return '<a href="' + url + '" target="_blank" title="' + url + '">' + title + '</a>';
                    }
                },
                {
                    field: 'wrongWord',
                    title: '词条',
                    width: '100px'
                },
                {
                    field: 'wrongRule',
                    title: '错误规则',
                    width: '70px',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(wrongRuleDatas, value);
                    }
                },
                {
                    field: 'errorType',
                    title: '错误类型',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(errorTypeDatas, value);
                    }
                },
                {
                    field: 'errorGrade',
                    title: '错误级别',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(errorGradeDatas, value);
                    }
                },
                {
                    field: 'context',
                    title: '上下文',
                    escape: true  // 关键设置，禁用HTML转义
                },
                {
                    field: 'time',
                    width: '100px',
                    title: '发布时间'
                },
                {
                    field: 'scantime',
                    width: '100px',
                    title: '审核时间'
                },
                {
                    field: 'status',
                    title: '状态',
                    width: '70px',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    width: '180px',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="mark(\'' + row.id + '\')"><i class="fa fa-anchor"></i>定位</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="biaoji(\'' + row.id + '\')"><i class="fa fa-edit"></i>标记</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        // 在表格加载和搜索时刷新统计
        $(function() {
            // 初始化时加载
            loadStatistics();
            // 搜索按钮点击时重新加载
            $('.btn-search').click(function() {
                // 先执行原有搜索逻辑
                $.table.search();
                // 然后刷新统计
                setTimeout(loadStatistics, 300); // 延迟确保搜索完成
            });

            // 重置按钮点击时重新加载
            $('.btn-reset').click(function() {
                // 先执行原有重置逻辑
                $.form.reset();
                // 然后刷新统计
                setTimeout(loadStatistics, 300); // 延迟确保重置完成
            });
        });

        // 加载统计数据
        function loadStatistics() {
            // 1. 扩展jQuery方法（如果尚未定义）
            if (typeof $.fn.serializeObject !== 'function') {
                $.fn.serializeObject = function() {
                    var obj = {};
                    var arr = this.serializeArray();
                    $.each(arr, function() {
                        if (obj[this.name] !== undefined) {
                            if (!obj[this.name].push) {
                                obj[this.name] = [obj[this.name]];
                            }
                            obj[this.name].push(this.value || '');
                        } else {
                            obj[this.name] = this.value || '';
                        }
                    });
                    return obj;
                };
            }

            // 2. 获取表单数据并处理特殊字段
            var formData = $('#formId').serializeObject();
            $.ajax({
                url: prefix + "/statistics",
                type: "GET",
                data: formData,  // 将表单数据作为查询参数
                success: function(res) {
                    $('#errorWordCount').text(res.data.errorWordCount || 0);
                    $('#processedCount').text(res.data.processedCount || 0);
                    $('#noNeedProcessCount').text(res.data.noNeedProcessCount || 0);

                    $('#errorWordCount2').text(res.data.errorWordCount2 || 0);
                    $('#processedCount2').text(res.data.processedCount2 || 0);
                    $('#noNeedProcessCount2').text(res.data.noNeedProcessCount2 || 0);

                    $('#errorWordCount3').text(res.data.errorWordCount3 || 0);
                    $('#processedCount3').text(res.data.processedCount3 || 0);
                    $('#noNeedProcessCount3').text(res.data.noNeedProcessCount3 || 0);
                }
            });
        }

        function mark(id) {
            window.open('policyresults/mark/'+id, '_blank');
        }

        function biaoji(id) {
            // 判断是单个标记还是批量标记
            var rows;
            if (id) {
                // 单个标记，构造一个只包含当前行的数组
                rows = [{id: id}];
            } else {
                // 批量标记，获取选中的行
                rows = $('#bootstrap-table').bootstrapTable('getSelections');
                if (rows.length == 0) {
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }
            }

            // 准备单选按钮HTML
            var radioOptions = statusDatas.map(item => `
                <label style="margin-left: ${item.dictValue === '1' ? '0' : '20px'};">
                    <input type="radio" name="errorType" value="${item.dictValue}" ${item.dictValue === '1' ? 'checked' : ''}>
                    ${item.dictLabel}
                </label>
            `).join('');

            var radioHtml = `
            <div class="form-horizontal" style="padding: 20px;">
                <div class="form-group">
                    <div class="col-sm-10">
                        <div class="radio-box">${radioOptions}</div>
                    </div>
                </div>
            </div>
            `;

            layer.open({
                type: 1,
                title: '请选择状态',
                content: radioHtml,
                area: ['600px', '200px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var selectedValue = $('input[name="errorType"]:checked').val();
                    if (selectedValue) {
                        // 获取选中行的ID数组
                        var ids = rows.map(row => row.id);

                        $.ajax({
                            url: prefix + "/biaoji",
                            type: "POST",
                            data: {
                                ids: ids.join(","),
                                status: selectedValue
                            },
                            success: function(res) {
                                layer.msg("标记成功");
                                $('#bootstrap-table').bootstrapTable('refresh');
                            },
                            error: function() {
                                layer.msg("操作失败");
                            }
                        });
                    }
                    layer.close(index);
                },
                btn2: function(index) {
                    layer.close(index);
                }
            });
        }
        $(function() {
            // 监听表单的 Enter 键按下事件
            $('#formId').on('keydown', function(e) {
                if (e.key === 'Enter') {
                    e.preventDefault(); // 阻止默认提交行为
                    $.table.search();   // 触发搜索
                    loadStatistics();   // 刷新统计（可选）
                }
            });
        });
    </script>
</body>
</html>