<!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: #fff;
        }
        .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;
        }
        .error-section {
            margin-top: 20px;
            padding: 10px;
            background-color: #fff8f8;
            border-left: 3px solid #ff4d4f;
        }
        .error-title {
            font-weight: bold;
            color: #ff4d4f;
            margin-bottom: 10px;
        }
        .error-item {
            margin-bottom: 8px;
            padding-left: 15px;
            position: relative;
        }
        .error-item:before {
            content: "•";
            position: absolute;
            left: 0;
            color: #ff4d4f;
        }
        .suggestion-table {
            width: 100%;
            margin-top: 5px;
            border-collapse: collapse;
        }
        .suggestion-table th, .suggestion-table td {
            padding: 5px;
            border: 1px solid #ddd;
        }
        .suggestion-table th {
            background-color: #f5f5f5;
            width: 80px;
        }

        /* 弹窗自适应样式 */
        .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>
            </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";
    $("#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();
            }
        });
    });

    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 errorHtml = '';

                if (errorData && errorData.length > 0) {
                    errorHtml += `
                        <div class="error-section">
                            <div class="error-title">字词错误（${errorData.length}）</div>`;

                    errorData.forEach(error => {
                        errorHtml += `
                            <div class="error-item">
                                <strong>${error.wrongWord}</strong> → <strong>${error.correctWord}</strong>
                                <table class="suggestion-table">
                                    <tr>
                                        <th>位置</th>
                                        <td>${error.content}</td>
                                    </tr>
                                    <tr>
                                        <th>建议</th>
                                        <td>${error.suggestion || error.correctWord}</td>
                                    </tr>
                                </table>
                            </div>`;
                    });

                    errorHtml += `</div>`;
                } else {
                    errorHtml = `<div class="no-errors">未发现错误</div>`;
                }

                $('#check-results-container').html(errorHtml);
            } 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>