<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('文件校对')" />
    <style>
        .file-check-container {
            display: flex;
            width: 100%;
            height: 600px;
            border: 1px solid #e6e6e6;
            border-radius: 4px;
        }
        .original-file {
            flex: 1.5;
            padding: 15px;
            /*overflow-y: auto;*/
            border-right: 1px solid #e6e6e6;
            background-color: #fafafa;
        }
        .check-result {
            flex: 1;
            padding: 15px;
            /*overflow-y: auto;*/
            background-color: #f9f9f9;
        }
        .file-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }
        .file-content {
            line-height: 1.6;
            white-space: pre-wrap;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #eee;
            border-radius: 4px;
            height: calc(100% - 40px);
            overflow-y: auto;
        }

        /* 校对结果样式 - 按照main.html修改 */
        .error-item {
            margin-bottom: 20px;
            padding: 15px;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .error-title {
            font-weight: bold;
            margin-bottom: 12px;
            font-size: 16px;
            color: #ed5565;
        }
        .suggestion-table {
            width: 100%;
            margin-bottom: 10px;
            border-collapse: collapse;
            word-break: break-all;
            word-wrap: break-word;
            white-space: normal;
        }
        .suggestion-table th, .suggestion-table td {
            padding: 8px;
            border: 1px solid #ddd;
            text-align: left;
        }
        .suggestion-table th {
            width: 80px;
            background-color: #f9f9f9;
        }
        .confidence {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
        }
        .political-warning {
            font-weight: bold;
        }
        .error-highlight {
            background-color: #ffcccc;
            color: #ed5565;
            border-bottom: 1px dashed #ed5565;
            padding: 0 2px;
        }
        .error-content a {
            display: inline-block;
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            vertical-align: bottom;
        }

        /* 新增误报按钮样式 */
        .false-positive-btn {
            font-size: 12px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 3px;
            color: #6c757d;
            cursor: pointer;
            transition: all 0.2s;
            display: block;
            text-align: center;
            padding: 4px 8px;
            margin-top: 8px;
        }
        .false-positive-btn:hover {
            background-color: #e9ecef;
            color: #495057;
        }
        .false-positive-btn.reported {
            color: #155724;
            border-color: #c3e6cb;
            cursor: default;
        }
        .false-positive-btn i {
            margin-right: 4px;
        }
        .error-actions {
            margin-bottom: 10px;
            text-align: right;
        }

        .error-type-divider {
            margin: 15px 0;
            border-top: 1px dashed #e7eaec;
        }

        /* 弹窗自适应样式 */
        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }
        .modal-content {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
            display: flex;
            flex-direction: column;
            max-height: 90vh;
            animation: modalFadeIn 0.3s;
        }
        @keyframes modalFadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .modal-header {
            padding: 15px 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-title {
            margin: 0;
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }
        .modal-close {
            background: none;
            border: none;
            font-size: 22px;
            cursor: pointer;
            color: #999;
            padding: 0;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: background-color 0.2s;
        }
        .modal-close:hover {
            background-color: #f5f5f5;
            color: #333;
        }
        .modal-body {
            padding: 20px;
            overflow-y: auto;
            flex: 1;
        }
        .modal-footer {
            padding: 15px 20px;
            border-top: 1px solid #eee;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        /* 响应式设计 */
        /* 大屏幕 */
        @media (min-width: 1200px) {
            .modal-content {
                width: 900px;
            }
        }
        /* 中等屏幕 */
        @media (min-width: 992px) and (max-width: 1199px) {
            .modal-content {
                width: 800px;
            }
        }
        /* 小屏幕 */
        @media (min-width: 768px) and (max-width: 991px) {
            .modal-content {
                width: 700px;
            }
        }
        /* 超小屏幕 */
        @media (max-width: 767px) {
            .modal-content {
                width: 95%;
                margin: 10px;
            }
            .file-check-container {
                flex-direction: column;
                height: auto;
            }
            .original-file {
                border-right: none;
                border-bottom: 1px solid #e6e6e6;
            }
        }

        /* 高屏幕调整 */
        @media (max-height: 700px) {
            .modal-content {
                max-height: 85vh;
            }
        }
        @media (max-height: 500px) {
            .modal-content {
                max-height: 80vh;
            }
        }
    </style>
</head>
<body class="white-bg">
<form id="form-filejiaodui-edit" th:object="${trsFileInfo}">
    <input type="hidden" name="doccontent" th:field="*{doccontent}">
    <input type="hidden" name="docname" id="docname" th:field="*{docname}">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <div class="file-check-container">
            <!-- 左侧原始文件内容 -->
            <div class="original-file">
                <div class="file-title">文件内容</div>
                <div class="file-content" id="file-content-display" th:text="${trsFileInfo.doccontent}"></div>
            </div>

            <!-- 右侧校对结果 -->
            <div class="check-result">
                <div class="file-title">校对结果</div>
                <div id="check-results-container" style="height: calc(100% - 40px); overflow-y: auto;">
                    <!-- 动态内容将在这里显示 -->
                    <div class="text-muted" style="text-align: center; padding: 20px;">
                        <i class="fa fa-info-circle fa-2x"></i>
                        <p>正在检查文档内容...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<!-- 示例弹窗结构 -->
<div id="exampleModal" class="modal" style="display: none;">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">示例弹窗</h3>
            <button class="modal-close">&times;</button>
        </div>
        <div class="modal-body">
            <p>这是一个自适应弹窗示例，会根据屏幕分辨率自动调整大小。</p>
            <p>在移动设备上，弹窗会占据大部分屏幕宽度，并适当调整内容布局。</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary">取消</button>
            <button type="button" class="btn btn-primary">确认</button>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "project/filejiaodui";
    var prefixjiaodui = ctx + "project/jiaodui";
    var wordResults = ctx + "project/wordresults";
    var falsePositiveErrors = []; // 存储被标记为误报的错误

    $("#form-filejiaodui-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            //确定按钮
            $.modal.close();
            //$.operate.save(prefix + "/edit", $('#form-filejiaodui-edit').serialize());
        }
    }

    $(function() {
        var content = $('#file-content-display').text();
        $('input[name="doccontent"]').val(content);
        jiaodui();

        // 弹窗关闭事件
        $('.modal-close, .modal .btn-secondary').on('click', function() {
            $(this).closest('.modal').hide();
        });

        // ESC键关闭弹窗
        $(document).on('keyup', function(e) {
            if (e.key === "Escape") {
                $('.modal').hide();
            }
        });

        // 使用事件委托处理误报按钮点击
        $(document).on('click', '.false-positive-btn', function(e) {
            e.preventDefault();

            // 如果按钮已被禁用，则不执行操作
            if ($(this).prop('disabled')) {
                return;
            }

            // 获取错误信息
            var wrongWord = $(this).data('wrong');
            var correctWord = $(this).data('correct');
            var id = $(this).data('id');
            // 调用标记函数
            markAsFalsePositive(id, wrongWord, correctWord, this);
        });
    });

    // 标记误报
    function markAsFalsePositive(id, wrongWord, correctWord, button) {
        // 添加到误报列表
        falsePositiveErrors.push(wrongWord + '-' + correctWord);

        // 更新按钮状态
        $(button).addClass('reported').html('<i class="fa fa-check"></i> 已标记').prop('disabled', true);

        var data = { "id": id, "res": 1 };
        $.get(wordResults + "/edit", data, function(result) {
            if (result.code == web_status.SUCCESS) {
                let errorData = result.data;
            }
        });
    }

    function jiaodui() {
        var content = $('#file-content-display').text();
        var title = $('#docname').val();
        var data = { "content": content, "title" : title, "type": "1" };
        $.get(prefixjiaodui + "/jiaodui", data, function(result) {
            if (result.code == web_status.SUCCESS) {
                let errorData = result.data;
                let resultHtml = '';

                if (errorData && errorData.length > 0) {
                    // 按错误类型分组
                    const errorsByType = {};
                    errorData.forEach(error => {
                        if (!errorsByType[error.errorType]) {
                            errorsByType[error.errorType] = [];
                        }
                        errorsByType[error.errorType].push(error);
                    });

                    // 生成结果HTML
                    Object.keys(errorsByType).forEach(errorType => {
                        const errors = errorsByType[errorType];
                        let errorTypeHtml = '';

                        errors.forEach((error, index) => {
                            // 检查是否是误报
                            const isFalsePositive = falsePositiveErrors.includes(error.wrongWord + '-' + error.correctWord);

                            errorTypeHtml += `
                                <div class="error-content">
                                    <h4>${index + 1}、${error.wrongWord}</h4>
                                    <table class="suggestion-table">
                                        <tr>
                                            <th>修改建议</th>
                                            <td>${error.correctWord}</td>
                                        </tr>
                                        ${error.source ? `<tr><th>参考依据</th><td>${error.source}</td></tr>` : ''}
                                        ${error.confidence ? `<tr><th>置信度</th><td>${error.confidence}</td></tr>` : ''}
                                    </table>
                                    <div class="error-actions">
                                        <button class="false-positive-btn" data-id="${error.id}" data-wrong="${error.wrongWord}" data-correct="${error.correctWord}" ${isFalsePositive ? 'disabled' : ''}>
                                            <i class="fa ${isFalsePositive ? 'fa-check' : 'fa-flag'}"></i> ${isFalsePositive ? '已标记' : '误报'}
                                        </button>
                                    </div>
                                </div>
                            `;
                        });

                        resultHtml += `
                            <div class="error-item">
                                <div class="error-title">${errorType}（${errors.length}）</div>
                                ${errorTypeHtml}
                            </div>
                            <div class="error-type-divider"></div>
                        `;
                    });

                    // 移除最后一个分隔线
                    resultHtml = resultHtml.replace(/<div class="error-type-divider"><\/div>$/, '');
                } else {
                    resultHtml = `
                        <div class="text-muted" style="text-align: center; padding: 20px;">
                            <i class="fa fa-check-circle fa-2x" style="color:#1ab394"></i>
                            <p>未发现需要修改的内容</p>
                        </div>
                    `;
                }

                $('#check-results-container').html(resultHtml);
            } else {
                // 使用自适应弹窗显示错误
                showModal('错误', result.msg);
            }
            $.modal.closeLoading();
        });
    }

    // 显示自适应弹窗
    function showModal(title, content) {
        // 创建或获取弹窗元素
        let modal = $('#customModal');
        if (modal.length === 0) {
            // 如果不存在，创建弹窗
            $('body').append(`
                <div id="customModal" class="modal">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h3 class="modal-title">${title}</h3>
                            <button class="modal-close">&times;</button>
                        </div>
                        <div class="modal-body">
                            <p>${content}</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary">确定</button>
                        </div>
                    </div>
                </div>
            `);

            // 绑定事件
            $('#customModal .modal-close, #customModal .btn-primary').on('click', function() {
                $('#customModal').hide();
            });
        } else {
            // 更新现有弹窗内容
            modal.find('.modal-title').text(title);
            modal.find('.modal-body').html(`<p>${content}</p>`);
        }

        // 显示弹窗
        $('#customModal').show();

        // 动态调整弹窗尺寸（如果需要）
        adjustModalSize();
    }

    // 根据屏幕尺寸调整弹窗
    function adjustModalSize() {
        const modal = $('.modal:visible .modal-content');
        if (modal.length === 0) return;

        const windowWidth = $(window).width();
        const windowHeight = $(window).height();

        // 根据窗口大小调整
        if (windowWidth < 768) {
            modal.css({
                'width': '95%',
                'max-width': 'none'
            });
        } else {
            // 移除内联样式，让CSS媒体查询生效
            modal.css('width', '');
        }

        // 确保弹窗不超过视口高度
        const maxHeight = windowHeight * 0.85;
        modal.css('max-height', maxHeight + 'px');
    }

    // 窗口大小改变时调整弹窗
    $(window).on('resize', function() {
        adjustModalSize();
    });
</script>
</body>
</html>