File size: 7,998 Bytes
ced0f86
d150164
 
 
 
 
 
 
 
ced0f86
d150164
 
 
 
ced0f86
 
 
d150164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced0f86
 
 
 
 
 
 
 
 
d150164
ced0f86
 
 
d150164
 
 
 
 
 
 
 
 
ced0f86
d150164
 
ced0f86
d150164
 
 
 
 
ced0f86
 
 
 
 
 
 
 
 
d150164
 
ced0f86
 
d150164
ced0f86
 
 
 
 
 
 
 
 
 
 
 
 
d150164
 
ced0f86
 
 
 
d150164
ced0f86
4b103b3
ced0f86
 
 
 
 
 
 
ea9661e
 
 
ced0f86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9661e
ced0f86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d150164
ced0f86
 
 
 
d150164
 
 
 
ced0f86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d150164
 
ced0f86
 
 
 
 
 
 
 
 
 
 
 
d150164
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

// OzonAuth MiniApp - Main JavaScript
class OzonAuthApp {
    constructor() {
        this.init();
    }

    init() {
        this.bindEvents();
        this.setCurrentYear();
    }

    bindEvents() {
        const form = document.getElementById('authForm');
        if (form) {
            form.addEventListener('submit', this.handleSubmit.bind(this));
        }
    }

    async handleSubmit(event) {
        event.preventDefault();
        
        const formData = new FormData(event.target);
        const clientId = formData.get('clientId');
        const apiKey = formData.get('apiKey');

        // Show loading state
        this.showLoading();

        try {
            // Simulate API validation (replace with actual Ozon API call)
            const isValid = await this.validateOzonCredentials(clientId, apiKey);
            
            if (isValid) {
                this.showSuccess('Авторизация успешна! Данные проверены.');
            } else {
                this.showError('Ошибка авторизации. Проверьте Client ID и API Key.');
            }
        } catch (error) {
            this.showError('Произошла ошибка при подключении: ' + error.message);
        }
    }

    async validateOzonCredentials(clientId, apiKey) {
        // Simulate API validation delay
        await new Promise(resolve => setTimeout(resolve, 1500));
        
        // Basic validation (replace with actual Ozon API endpoint)
        if (clientId && apiKey && clientId.length >= 5 && apiKey.length >= 10) {
            // In a real app, you would make a request to Ozon API here
            // For demo purposes, we'll simulate success for valid-looking credentials
            return clientId.startsWith('ozon') || apiKey.includes('api');
        }
        return false;
    }

    showLoading() {
        const resultDiv = document.getElementById('result');
        const submitBtn = document.querySelector('button[type="submit"]');

        // Disable button and show loading
        if (submitBtn) {
            submitBtn.disabled = true;
            submitBtn.innerHTML = `
                <div class="flex items-center justify-center space-x-2">
                    <div class="w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin"></div>
                    <span>Проверка...</span>
                </div>
            `;
        }

        if (resultDiv) {
            resultDiv.classList.add('hidden');
        }
    }

    showSuccess(message) {
        this.showResult('success', 'Успех!', message);
    }

    showError(message) {
        this.showResult('error', 'Ошибка', message);
    }

    showResult(type, title, message) {
        const resultDiv = document.getElementById('result');
        const resultContainer = resultDiv?.querySelector('div');
        const resultTitle = document.getElementById('resultTitle');
        const resultMessage = document.getElementById('resultMessage');
        const submitBtn = document.querySelector('button[type="submit"]');

        // Reset button
        if (submitBtn) {
            submitBtn.disabled = false;
            submitBtn.innerHTML = `
                <div class="flex items-center justify-center space-x-3">
                    <i data-feather="send" class="w-5 h-5"></i>
                    <span class="text-lg">Авторизоваться</span>
                </div>
            `;
        }

        // Update result display
        if (resultTitle) resultTitle.textContent = title;
        if (resultMessage) resultMessage.textContent = message;

        if (resultContainer) {
            // Remove existing classes and apply modern styles
            resultContainer.className = 'p-5 rounded-2xl border-2 glass';
            
            if (type === 'success') {
                resultContainer.classList.add('success-border', 'success-bg');
                if (resultTitle) resultTitle.classList.add('success-text', 'font-semibold');
                if (resultMessage) resultMessage.classList.add('success-text');
            } else {
                resultContainer.classList.add('error-border', 'error-bg');
                if (resultTitle) resultTitle.classList.add('error-text', 'font-semibold');
                if (resultMessage) resultMessage.classList.add('error-text');
            }
        }

        if (resultDiv) {
            resultDiv.classList.remove('hidden');
            resultDiv.classList.add('floating');
            feather.replace();

            // Auto-hide after 5 seconds with fade out
            setTimeout(() => {
                resultDiv.style.opacity = '0';
                resultDiv.style.transform = 'translateY(-10px)';
                setTimeout(() => {
                    if (resultDiv) {
                        resultDiv.classList.add('hidden');
                        resultDiv.style.opacity = '1';
                        resultDiv.style.transform = 'translateY(0)';
                    }, 300);
                }, 5000);
            }
        }
    }

    setCurrentYear() {
        const yearElement = document.getElementById('currentYear');
        if (yearElement) {
            yearElement.textContent = new Date().getFullYear();
        }
    }
}

// Theme management
class ThemeManager {
    static init() {
        const themeToggle = document.getElementById('themeToggle');
        if (themeToggle) {
            themeToggle.addEventListener('click', (event) => {
                this.handleThemeToggle(event);
            });
        }
        
        this.updateThemeIcon();
    }

    static handleThemeToggle(event) {
        const isDark = document.documentElement.classList.contains('dark');
        
        // Toggle theme immediately
        document.documentElement.classList.toggle('dark');
        localStorage.setItem('theme', !isDark ? 'dark' : 'light');
        this.updateThemeIcon();
    }

    static updateThemeIcon() {
        const themeToggle = document.getElementById('themeToggle');
        const themeIcon = themeToggle?.querySelector('i');
        
        if (themeIcon) {
            const isDark = document.documentElement.classList.contains('dark');
            themeIcon.setAttribute('data-feather', isDark ? 'sun' : 'moon');
            feather.replace();
        }
    }
}

// Utility function for Telegram MiniApp integration
class TelegramIntegration {
    static init() {
        // This would be replaced with actual Telegram MiniApp SDK integration
        if (window.Telegram && window.Telegram.WebApp) {
            const tg = window.Telegram.WebApp;
            
            // Expand the app to full height
            tg.expand();
            
            // Set theme based on Telegram's theme
            if (tg.colorScheme === 'dark') {
                document.documentElement.classList.add('dark');
            } else {
                document.documentElement.classList.remove('dark');
            }
            
            // Handle theme changes from Telegram
            tg.onEvent('themeChanged', () => {
                if (tg.colorScheme === 'dark') {
                    document.documentElement.classList.add('dark');
                } else {
                    document.documentElement.classList.remove('dark');
                }
                ThemeManager.updateThemeIcon();
            });
        }
    }
}

// Initialize everything when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    new OzonAuthApp();
    ThemeManager.init();
    TelegramIntegration.init();

    // Load saved theme
    const savedTheme = localStorage.getItem('theme');
    const systemPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
    
    if (savedTheme === 'dark' || (!savedTheme && systemPrefersDark)) {
        document.documentElement.classList.add('dark');
    }
    
    ThemeManager.updateThemeIcon();
});