<!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;
        }

        /* 校对结果样式 - 按照第一个view.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;
        }
        .error-content h4 {
            margin: 0 0 10px 0;
            font-size: 14px;
            color: #333;
        }

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

        /* 响应式设计 */
        @media (max-width: 767px) {
            .file-check-container {
                flex-direction: column;
                height: auto;
            }
            .original-file {
                border-right: none;
                border-bottom: 1px solid #e6e6e6;
            }
        }
    </style>
</head>
<body class="white-bg">
<form id="form-filejiaodui-edit" th:object="${trsResultsLog}">
    <input type="hidden" name="content" th:field="*{content}">
    <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="${trsResultsLog.content}"></div>
            </div>

            <input type="hidden" th:value="${trsResultsLog.id}" id="logId"/>

            <!-- 右侧校对结果 -->
            <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>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "project/filejiaodui";
    var prefixjiaodui = ctx + "project/jiaodui";
    var prefixWordResult = ctx + "project/wordresults";

    $("#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="content"]').val(content);
        jiaodui_col();
    });

    function jiaodui_col() {
        var data = { "logId": $('#logId').val() };
        $.get(prefixWordResult + "/wordlist", 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) => {
                            errorTypeHtml += `
                                <div class="error-content">
                                    <h4>${index + 1}、${error.wrongWord}</h4>
                                    <table class="suggestion-table">
                                        <tr>
                                            <th>修改建议</th>
                                            <td>${error.correctWord}</td>
                                        </tr>
                                        <tr>
                                            <th>位置</th>
                                            <td>${error.content || error.context || ''}</td>
                                        </tr>
                                        ${error.source ? `<tr><th>参考依据</th><td>${error.source}</td></tr>` : ''}
                                        ${error.confidence ? `<tr><th>置信度</th><td>${error.confidence}</td></tr>` : ''}
                                    </table>
                                </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 {
                $.modal.alertError(result.msg);
            }
            $.modal.closeLoading();
        });
    }
</script>
</body>
</html>