<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="/cite_practica/">
    <title>AETERNA - Вход</title>
    <link rel="stylesheet/less" type="text/css" href="style_for_cite.less">
    <script src="https://cdn.jsdelivr.net/npm/less"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>

        .user-status {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            background: white;
            padding: 10px 15px;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .user-status.admin {
            border-left: 4px solid #617365;
        }

        .user-status.user {
            border-left: 4px solid #28a745;
        }

        .status-icon {
            font-size: 20px;
        }

        .admin-icon {
            color: #617365;
        }

        .user-icon {
            color: #28a745;
        }
    </style>
    <script src="check_auth.js"></script>
    <style>

        .link-disabled {
            cursor: not-allowed !important;
            opacity: 0.7 !important;
            pointer-events: none !important;
        }

        .auth-required {
            position: relative;
        }

        .auth-required::after {
            content: "🔒";
            position: absolute;
            top: -5px;
            right: -10px;
            font-size: 10px;
        }
    </style>
</head>
<body>

<div class="user-status" id="userStatus" style="display: none;">
    <i class="fas fa-user status-icon" id="statusIcon"></i>
    <div>
        <div id="statusText"></div>
        <div id="userEmail" style="font-size: 12px; color: #666;"></div>
    </div>
    <button onclick="logout()" style="background: none; border: none; color: #666; cursor: pointer;">
        <i class="fas fa-sign-out-alt"></i>
    </button>
</div>

<header class="header">
    <div class="header__top">
        <div class="container header__top-content">
            <div class="logo">AETERNA</div>

            <div class="search-catalog">
                <div class="catalog-dropdown">
                    Все категории <span>&#9660;</span>
                    <div class="catalog-dropdown__menu">
                        <ul>
                            <li>Диваны</li>
                            <li>Кровати</li>
                            <li>Шкафы</li>
                            <li>Стулья</li>
                            <li>Столы</li>
                            <li>Комоды</li>
                            <li>Тумбы</li>
                            <li>Полки</li>
                            <li>Стенки</li>
                            <li>Аксессуары</li>
                        </ul>
                    </div>
                </div>
                <div class="search-box">
                    <input type="text" placeholder="Поиск товаров">
                    <span class="search-icon"><i class="fas fa-search"></i></span>
                </div>
            </div>

            <div class="header__icons header__icons--top">
                <a href="checkout.php" class="icon"><i class="fas fa-shopping-cart"></i></a>
                <a href="register.php" class="icon"><i class="far fa-user"></i></a>
            </div>
        </div>
    </div>

    <div class="header__bottom">
        <div class="container header__bottom-content">
            <div class="catalog-menu">
                <a href="catalog.php" class="catalog-link">
                    <div class="catalog-icon">
                        <span class="line"></span>
                        <span class="line"></span>
                        <span class="line"></span>
                    </div>
                    <span class="catalog-lines">☰</span>
                    Каталог
                </a>
            </div>

            <nav class="nav">
                <ul class="nav-list">
                    <li><a href="cite_mebel.php">Главная</a></li>
                    <li><a href="services.php">Услуги</a></li>
                    <li><a href="delivery.php">Доставка и оплата</a></li>
                    <li><a href="warranty.php">Гарантия</a></li>
                    <li><a href="#footer">Контакты</a></li>
                </ul>
            </nav>
            <div class="header-phone">+7(912)999-12-23</div>
        </div>
    </div>
</header>

<main class="profile-page-main">
    <div class="profile-container">
        <div class="profile-left-col">
            <div class="logo">AETERNA</div>
        </div>

        <div class="profile-right-col">
            <div class="profile-form-block">
                <h2>ВХОД В АККАУНТ</h2>
                <form class="profile-form" action="#" method="POST" id="loginForm">
                    <div class="input-group">
                        <label for="login-email">E-mail</label>
                        <input type="email" id="login-email" name="email" placeholder="Ваш электронный адрес" required>
                        <div class="error-message" id="email-error">
                            Введите корректный email адрес
                        </div>
                    </div>

                    <div class="input-group">
                        <label for="login-password">Пароль</label>
                        <input type="password" id="login-password" name="password" placeholder="Введите пароль" required>
                        <div class="error-message" id="password-error">Неверный пароль</div>
                    </div>

                    <div class="form-options">
                        <label class="remember-me">
                            <input type="checkbox" id="remember" name="remember">
                            Запомнить меня
                        </label>
                        <a href="#" class="forgot-password">Забыли пароль?</a>
                    </div>

                    <button type="submit" class="btn primary-btn save-btn">Войти</button>

                    <div class="auth-actions">
                        <span class="auth-text">Нет аккаунта?</span>
                        <a href="register.php" class="login-btn">Зарегистрироваться</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</main>

<div class="page-messages">
    <div class="message error" id="errorMessage"></div>
    <div class="message success" id="successMessage"></div>
    <div class="message warning" id="warningMessage"></div>
</div>

<footer class="footer" id="footer">
    <div class="container footer__content">
        <div class="footer__col footer__col--logo">
            <div class="logo">AETERNA</div>
        </div>

        <div class="footer__col">
            <h5>ПОКУПАТЕЛЮ</h5>
            <ul>
                <li><a href="catalog.php">Каталог</a></li>
                <li><a href="services.php">Услуги</a></li>
            </ul>
        </div>

        <div class="footer__col">
            <h5>ПОМОЩЬ</h5>
            <ul>
                <li><a href="delivery.php">Доставка и оплата</a></li>
                <li><a href="warranty.php">Гарантия и возврат</a></li>
                <li><a href="cite_mebel.php#faq">Ответы на вопросы</a></li>
                <li><a href="#footer">Контакты</a></li>
            </ul>
        </div>

        <div class="footer__col">
            <h5>КОНТАКТЫ</h5>
            <p>aeterna@mail.ru</p>
            <p>+7(912)999-12-23</p>
            <div class="social-icons">
                <span class="icon"><i class="fab fa-telegram"></i></span>
                <span class="icon"><i class="fab fa-instagram"></i></span>
                <span class="icon"><i class="fab fa-vk"></i></span>
            </div>
        </div>

        <div class="footer__col">
            <h5>ПРИНИМАЕМ К ОПЛАТЕ</h5>
            <div class="payment-icons">
                <span class="pay-icon"><i class="fab fa-cc-visa"></i></span>
                <span class="pay-icon"><i class="fab fa-cc-mastercard"></i></span>
            </div>
        </div>
    </div>
    <div class="copyright">
        <p>© 2025 AETERNA. Все права защищены.</p>
    </div>
</footer>

<script>
    $(document).ready(function() {

        const urlParams = new URLSearchParams(window.location.search);
        const redirectUrl = urlParams.get('redirect');

        if (redirectUrl) {
            sessionStorage.setItem('redirectAfterLogin', redirectUrl);
        }

        $('#loginForm').on('submit', function(e) {
            e.preventDefault();

            const email = $('#login-email').val();
            const password = $('#login-password').val();

            if (!email || !password) {
                alert('Заполните все поля');
                return;
            }

            $.ajax({
                url: 'api/auth.php',
                method: 'POST',
                data: {
                    email: email,
                    password: password
                },
                dataType: 'json',
                success: function(result) {
                    if (result.success) {

                        const savedRedirect = sessionStorage.getItem('redirectAfterLogin') || 'catalog.php';
                        sessionStorage.removeItem('redirectAfterLogin');
                        window.location.href = savedRedirect;
                    } else {
                        alert(result.message || 'Ошибка авторизации');
                    }
                },
                error: function() {
                    alert('Ошибка сервера. Попробуйте позже.');
                }
            });
        });
    });
</script>

</body>
</html>
