File size: 9,312 Bytes
04d0e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10bbf04
 
04d0e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e98c5b3
f20fdec
e98c5b3
 
 
 
 
 
 
 
 
 
 
 
04d0e70
 
f20fdec
04d0e70
c0a3c1e
 
 
 
 
 
 
 
 
 
 
 
04d0e70
 
 
 
 
 
 
 
 
 
 
f20fdec
04d0e70
e98c5b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
document.addEventListener("DOMContentLoaded", function () {
    const form = document.getElementById('process-form');
    const loadingOverlay = document.getElementById('loading-overlay');
    const downloadSection = document.getElementById('download-section');
    const downloadLink = document.getElementById('download-link');
    const fileInput = document.getElementById('images');
    const fileError = document.getElementById('file-error');
    const videoError = document.getElementById('video-error');
    const operationSelect = document.getElementById('operation');
    const operationError = document.getElementById('operation-error');
    const languageError = document.getElementById('language-error');
    const sourceLang = document.getElementById("source-lang");
    const targetLang = document.getElementById("target-lang");
    const languageSelection = document.getElementById("language-selection");
    const dubbingOptions = document.getElementById("dubbing-options");
    const dubbingType = document.getElementById("dubbing-type");
    const dubbingLanguage = document.getElementById("dubbing-language");
    const srtFileInput = document.getElementById("srt-file");
    const videoFileInput = document.getElementById("video-file");
    const fileLabel = document.getElementById("file-label");
    const srtFileLabel = document.querySelector('label[for="srt-file"]');
    const videoFileLabel = document.querySelector('label[for="video-file"]');

    languageSelection.style.display = "none";
    dubbingOptions.style.display = "none";

    operationSelect.addEventListener('change', function () {
        let selectedOperation = operationSelect.value;
        resetFormFields();

        if (selectedOperation === "translate" || selectedOperation === "both") {
            languageSelection.style.display = "flex";
            fileLabel.innerText = "Upload File (Max 5 images)";
            fileInput.setAttribute("accept", "image/*");
            fileInput.setAttribute("multiple", "multiple");
            fileInput.style.display = "block";
            fileLabel.style.display = "block";
            sourceLang.required = true;
            targetLang.required = true;
            fileInput.required = true;
        } else if (selectedOperation === "subtitle") {
            languageSelection.style.display = "flex";
            fileLabel.innerText = "Upload File (Max 1 video)";
            fileInput.setAttribute("accept", "video/*");
            fileInput.removeAttribute("multiple");
            fileInput.style.display = "block";
            fileLabel.style.display = "block";
            sourceLang.required = true;
            targetLang.required = true;
            fileInput.required = true;
        } else if (selectedOperation === "dubbing") {
            dubbingOptions.style.display = "flex";
            fileLabel.style.display = "none";
            fileInput.style.display = "none";
            dubbingType.required = true;
        } else {
            fileLabel.innerText = "Upload File (Max 5 images)";
            fileInput.setAttribute("accept", "image/*");
            fileInput.setAttribute("multiple", "multiple");
            fileInput.style.display = "block";
            fileLabel.style.display = "block";
            fileInput.required = true;
        }
    });

    dubbingType.addEventListener('change', function () {
        if (dubbingType.value === "manual") {
            dubbingLanguage.style.display = "flex";
            srtFileInput.style.display = "block";
            videoFileInput.style.display = "block";
            srtFileLabel.style.display = "block";
            videoFileLabel.style.display = "block";
            videoFileInput.required = true;
            srtFileInput.required = true;
            document.getElementById('dubbing-lang').required = true;
        } else {
            dubbingLanguage.style.display = "none";
            srtFileInput.style.display = "none";
            videoFileInput.style.display = "none";
            srtFileLabel.style.display = "none";
            videoFileLabel.style.display = "none";
            videoFileInput.required = false;
            srtFileInput.required = false;
            document.getElementById('dubbing-lang').required = false;
        }
    });

    fileInput.addEventListener('change', function () {
        const files = fileInput.files;
        const selectedOperation = operationSelect.value;
        let hasVideo = false;
        let hasImage = false;

        for (const file of files) {
            if (file.type.startsWith('video/')) {
                hasVideo = true;
            } else if (file.type.startsWith('image/')) {
                hasImage = true;
            }
        }

        if ((selectedOperation === 'subtitle') && !hasVideo) {
            videoError.style.display = 'block';
            fileInput.value = '';
        } else {
            videoError.style.display = 'none';
        }

        if (files.length > 5 && !hasVideo) {
            fileError.style.display = 'block';
            fileInput.value = '';
        } else {
            fileError.style.display = 'none';
        }
    });

    form.addEventListener('submit', async (e) => {
        e.preventDefault();
        
        if (!validateForm()) {
            return;
        }

        loadingOverlay.style.display = 'flex';
        const formData = new FormData(form);
        
        try {
            const response = await fetch('/process', {
                method: 'POST',
                body: formData,
            });
            const data = await response.json();
            if (data.status === 'ready') {
                downloadLink.href = data.download_url;
                downloadSection.style.display = 'block';
                form.style.display = 'none';
            }
        } catch (error) {
            console.error('Error:', error);
        } finally {
            loadingOverlay.style.display = 'none';
        }
    });

    function validateForm() {
        let isValid = true;
        let operation = operationSelect.value;
        let sourceLangValue = sourceLang.value;
        let targetLangValue = targetLang.value;

        if (!operation) {
            operationError.innerText = "Please select an operation.";
            operationError.style.display = "block";
            isValid = false;
        } else {
            operationError.style.display = "none";
        }

        if ((operation === "translate" || operation === "both" || operation === "subtitle") &&
            (!sourceLangValue || !targetLangValue)) {
            languageError.innerText = "Both language fields must be selected.";
            languageError.style.display = "block";
            isValid = false;
        } else if (sourceLangValue === targetLangValue && (operation === "translate" || operation === "both")) {
            languageError.innerText = "Source and Target languages cannot be the same.";
            languageError.style.display = "block";
            isValid = false;
        } else {
            languageError.style.display = "none";
        }

        return isValid;
    }

    function resetFormFields() {
        languageSelection.style.display = "none";
        dubbingOptions.style.display = "none";
        dubbingLanguage.style.display = "none";
        srtFileInput.style.display = "none";
        videoFileInput.style.display = "none";
        srtFileLabel.style.display = "none";
        videoFileLabel.style.display = "none";
        fileInput.style.display = "none";
        fileLabel.style.display = "none";
        fileInput.value = '';
        sourceLang.value = '';
        targetLang.value = '';
        dubbingType.value = '';
        document.getElementById('dubbing-lang').value = '';
        sourceLang.required = false;
        targetLang.required = false;
        fileInput.required = false;
        dubbingType.required = false;
        videoFileInput.required = false;
        srtFileInput.required = false;
        document.getElementById('dubbing-lang').required = false;
    }

    // Daily info note
    const note = document.getElementById("daily-note");
    const closeBtn = document.getElementById("close-note");
    const today = new Date().toISOString().split("T")[0];

    if (localStorage.getItem("noteClosedDate") !== today) {
        note.style.display = "block";
    }

    closeBtn.addEventListener("click", function() {
        note.style.display = "none";
        localStorage.setItem("noteClosedDate", today);
    });
});

// Reset form
function resetForm() {
    fetch('/delete-uploads', {
        method: 'POST'
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            document.getElementById('process-form').reset();
            document.getElementById('download-section').style.display = 'none';
            document.getElementById('process-form').style.display = 'block';
        }
    })
    .catch(error => console.error('Error:', error));
}

function purchase() {
    document.getElementById('purchase-modal').style.display = 'flex';
}

function closePurchaseModal() {
    document.getElementById('purchase-modal').style.display = 'none';
}

function confirmPurchase() {
    alert("Purchase completed successfully!");
    closePurchaseModal();
}