<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Translation result of {{ infile }}</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }
        .sentence { white-space: pre-wrap; }
        th, td {
            border: 1px solid #ccc;
            padding: 2px;
            text-align: left;
            word-wrap: break-word;
            position: relative;
        }
        th {
            cursor: col-resize;
        }
        textarea {
            width: 97%;
            height: auto;
//            max-height: 40px;
            resize: both;
            border: 1px solid #ccc;
            padding: 2px;
        }
        .highlight {
            background-color: yellow;
        }
        .resizer {
            position: absolute;
            display: inline-block;
            top: 0;
            right: 0;
            width: 15px;
            height: 100%;
            cursor: col-resize;
            background-color: transparent;
        }

        .save-button {
            margin-bottom: 10px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .save-button:hover {
            background-color: #45a049;
        }

        .custom-dialog {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 50%;
            min-width: 300px;
            height: 50%;
            min-height: 200px;
            background: #fff;
            border: 1px solid #ccc;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            padding: 20px;
            display: none;
            resize: both;
            overflow: auto;
        }
        .textarea-container {
            display: flex;
            flex-direction: column;
            gap: 10px;
            height: calc(100% - 50px);
        }
        .close-button {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            background: #f44336;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 5px;
        }
    </style>
</head>

<body>
<H2>Translation result of {{ infile }}</H2>
<b>Condition:</b><br>
<ul>
  <li>API : {{ api }}</li>
  <li>openai_model: {{ openai_model }}</li>
  <li>mode: {{ mode }}</li>
  <li>Role: {{ role }}</li>
  <li>Prompt: {{ prompt }}</li>
  <li>Process unit: {{ process_unit }}</li>
  <li>use markdown: {{ use_md }}</li>
  <li>translate only multibyte strings: {{ limit_to_multibyte_str }}</li>
  <li>minimum translate length        : {{ min_translate_length }}</li>
  <li>allowed translation length ratio: {{ allowed_translation_length_ratio }}</li>
  <li>GPT param: temperature: {{ temperature }}</li>
  <li>GPT param: max_tokens : {{ max_tokens }}</li>
</ul>

<button class="save-button" id="save-button">Save (+Javascript)</button>
<button class="save-button" id="save-simple-button">Save (simple HTML)</button>

<br>
保存したファイルを開く際、セキュリティの問題から開けない場合があります。<br>
Windowsの場合、ファイルのプロパティダイアログを表示し、下部にセキュリティの警告が出ている場合、<br>
「許可する」にチェックを入れてから開いてください<br>

    <div class="custom-dialog" id="dialog">
        <button class="close-button" id="close-dialog">Close</button>
        <div class="textarea-container">
            <textarea id="original-text" readonly></textarea>
            <textarea id="translated-text" readonly></textarea>
        </div>
    </div>

    <table>
        <tr>
            <th>Original<div class="resizer"></div></th>
            <th>Translated<div class="resizer"></div></th>
            <th>Corrected<div class="resizer"></div></th>
        </tr>
        {% for item in data %}
        <tr>
            <td class="sentence" data-pair="{{ loop.index }}">{{ item.original }}</td>
            <td class="sentence" data-pair="{{ loop.index }}">{{ item.translated }}</td>
            <td><textarea class="corrected" data-pair="{{ loop.index }}">{{ item.translated }}</textarea></td>
        </tr>
        {% endfor %}
    </table>

    <script>
        // Highlight and dialog setup
        const sentences = document.querySelectorAll('.sentence');
        const correctedAreas = document.querySelectorAll('.corrected');
        const dialog = document.getElementById('dialog');
        const originalTextarea = document.getElementById('original-text');
        const translatedTextarea = document.getElementById('translated-text');
        const closeDialogButton = document.getElementById('close-dialog');
        const saveButton = document.getElementById('save-button');
        const saveSimpleButton = document.getElementById('save-simple-button');

        sentences.forEach(sentence => {
            sentence.addEventListener('mouseover', () => {
                const pairId = sentence.getAttribute('data-pair');
                document.querySelectorAll(`[data-pair="${pairId}"]`).forEach(pair => {
                    pair.classList.add('highlight');
                });
            });

            sentence.addEventListener('mouseout', () => {
                const pairId = sentence.getAttribute('data-pair');
                document.querySelectorAll(`[data-pair="${pairId}"]`).forEach(pair => {
                    pair.classList.remove('highlight');
                });
            });
/*
            sentence.addEventListener('click', () => {
                const pairId = sentence.getAttribute('data-pair');
                const originalText = document.querySelector(`td[data-pair="${pairId}"]:first-child`).textContent;
                const translatedText = document.querySelector(`td[data-pair="${pairId}"]:nth-child(2)`).textContent;

                originalTextarea.value = originalText;
                translatedTextarea.value = translatedText;

                dialog.style.display = 'block';
            });
*/
        });

        closeDialogButton.addEventListener('click', () => {
            dialog.style.display = 'none';
        });

        // Save button functionality
        saveButton.addEventListener('click', () => {
            const htmlContent = document.documentElement.outerHTML;

            // 保存用ファイルの作成
            const blob = new Blob([htmlContent], { type: 'text/html' });
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = 'corrected_js.html';
            link.click();
            });

        saveSimpleButton.addEventListener('click', () => {
            let htmlOutput = '<table border="1">\n';
            htmlOutput += '    <tr>\n';
            htmlOutput += '        <th>Original</th>\n';
            htmlOutput += '        <th>Translated</th>\n';
            htmlOutput += '        <th>Corrected</th>\n';
            htmlOutput += '    </tr>\n';

            correctedAreas.forEach(area => {
                const pairId = area.getAttribute('data-pair');
                const originalText = document.querySelector(`td[data-pair="${pairId}"]:first-child`).textContent;
                const translatedText = document.querySelector(`td[data-pair="${pairId}"]:nth-child(2)`).textContent;
                const correctedText = area.value;

                htmlOutput += `    <tr>\n`;
                htmlOutput += `        <td>${originalText}</td>\n`;
                htmlOutput += `        <td>${translatedText}</td>\n`;
                htmlOutput += `        <td>${correctedText}</td>\n`;
                htmlOutput += `    </tr>\n`;
            });

            htmlOutput += '</table>';

            const blob = new Blob([htmlOutput], { type: 'text/html' });
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = 'corrected_simple.html';
            link.click();
        });

        document.addEventListener('DOMContentLoaded', () => {
            // autoResizeクラスを持つすべてのtextareaを対象にする
            const textareas = document.querySelectorAll('.corrected');

            textareas.forEach(textarea => {
                // 初期内容に応じた高さを設定
                textarea.style.height = 'auto'; // 高さをリセット
                textarea.style.height = textarea.scrollHeight + 'px'; // 内容に応じた高さに設定

                // 入力時にも高さを調整
                textarea.addEventListener('input', () => {
                    textarea.style.height = 'auto';
                    textarea.style.height = textarea.scrollHeight + 'px';
                });
            });
        });

        // Resizable columns
        const resizers = document.querySelectorAll('.resizer');
        resizers.forEach(resizer => {
            resizer.addEventListener('mousedown', (e) => {
                const th = resizer.parentElement;
                const startX = e.clientX;
                const startWidth = th.offsetWidth;

                const onMouseMove = (e) => {
                    const newWidth = startWidth + (e.clientX - startX);
                    th.style.width = `${newWidth}px`;
                };

                const onMouseUp = () => {
                    document.removeEventListener('mousemove', onMouseMove);
                    document.removeEventListener('mouseup', onMouseUp);
                };

                document.addEventListener('mousemove', onMouseMove);
                document.addEventListener('mouseup', onMouseUp);
            });
        });
    </script>
</body>
</html>
