<?php
/**
 * صفحة تسجيل الدخول - Roz Skin
 * تصميم عصري مستوحى من تطبيقات الموبايل
 */

session_start();
require_once '../config/database.php';
require_once '../models/user.php';

if (isset($_SESSION['user_id'])) {
    header('Location: index.php');
    exit;
}

$database = new Database();
$conn = $database->getConnection();

$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $phone = trim($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($phone) || empty($password)) {
        $message = 'رقم الهاتف وكلمة المرور مطلوبان';
        $message_type = 'error';
    } else {
        $stmt = $conn->prepare("SELECT id, name, phone, password FROM users WHERE phone = ?");
        $stmt->execute([$phone]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_phone'] = $user['phone'];
            
            header('Location: index.php');
            exit;
        } else {
            $message = 'رقم الهاتف أو كلمة المرور غير صحيحة';
            $message_type = 'error';
        }
    }
}

try {
    $settings_query = $conn->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('site_name', 'logo_text')");
    $settings = $settings_query->fetchAll(PDO::FETCH_KEY_PAIR);
    $site_name = $settings['site_name'] ?? 'Roz Skin';
    $logo_text = $settings['logo_text'] ?? 'Roz Skin';
} catch (Exception $e) {
    $site_name = 'Roz Skin';
    $logo_text = 'Roz Skin';
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تسجيل الدخول - <?php echo htmlspecialchars($site_name); ?></title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Tajawal', sans-serif;
            min-height: 100vh;
            background: linear-gradient(135deg, #FFB6C1 0%, #FFA6B5 50%, #FF96A9 100%);
            position: relative;
            overflow-x: hidden;
        }
        
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-image: 
                repeating-linear-gradient(45deg, transparent, transparent 35px, rgba(255,255,255,.05) 35px, rgba(255,255,255,.05) 70px),
                repeating-linear-gradient(-45deg, transparent, transparent 35px, rgba(255,255,255,.03) 35px, rgba(255,255,255,.03) 70px);
            pointer-events: none;
            z-index: 0;
        }
        
        .page-wrapper {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
            z-index: 1;
        }
        
        /* Top Pink Section - 50% */
        .top-section {
            flex: 1;
            min-height: 50vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 60px 30px 100px;
            position: relative;
        }
        
        .logo-text {
            font-size: 52px;
            font-weight: 700;
            color: white;
            letter-spacing: -1px;
            text-shadow: 0 4px 20px rgba(0,0,0,0.15);
            margin-bottom: 15px;
            animation: fadeInDown 0.6s ease-out;
        }
        
        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .welcome-text {
            color: rgba(255,255,255,0.95);
            font-size: 17px;
            line-height: 1.7;
            text-align: center;
            animation: fadeInDown 0.6s ease-out 0.2s both;
        }
        
        /* Wave at 50% */
        .wave {
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 100%;
            height: 120px;
        }
        
        /* Bottom White Section - 50% */
        .bottom-section {
            flex: 1;
            min-height: 50vh;
            background: white;
            padding: 50px 30px 40px;
            display: flex;
            align-items: flex-start;
            justify-content: center;
        }
        
        .form-container {
            width: 100%;
            max-width: 450px;
            animation: fadeInUp 0.6s ease-out 0.4s both;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .form-title {
            font-size: 32px;
            font-weight: 700;
            color: #333;
            margin-bottom: 5px;
            position: relative;
            display: inline-block;
        }
        
        .form-title::after {
            content: '';
            position: absolute;
            bottom: -4px;
            right: 0;
            width: 70%;
            height: 3px;
            background: #FF96A9;
            border-radius: 2px;
        }
        
        .form-subtitle {
            color: #999;
            font-size: 14px;
            margin-bottom: 30px;
        }
        
        .input-group {
            margin-bottom: 20px;
        }
        
        .input-group label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #666;
            margin-bottom: 8px;
        }
        
        .form-input-modern {
            width: 100%;
            padding: 18px 20px;
            border: none;
            border-bottom: 3px solid #f0f0f0;
            border-radius: 0;
            font-size: 18px;
            font-family: 'Tajawal', sans-serif;
            transition: all 0.3s;
            background: transparent;
            font-weight: 500;
        }
        
        .form-input-modern:focus {
            outline: none;
            border-bottom-color: #FF96A9;
            background: rgba(255, 150, 169, 0.03);
        }
        
        .form-input-modern::placeholder {
            color: #ccc;
            font-size: 16px;
            font-weight: 400;
        }
        
        .forgot-link {
            text-align: left;
            margin-bottom: 25px;
        }
        
        .forgot-password {
            color: #FF96A9;
            text-decoration: none;
            font-weight: 600;
            transition: color 0.3s;
        }
        
        .forgot-password:hover {
            color: #FF7A8F;
        }
        
        .btn-submit {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #FFB6C1 0%, #FF96A9 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 17px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 15px rgba(255, 150, 169, 0.4);
        }
        
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 150, 169, 0.5);
        }
        
        .btn-submit:active {
            transform: translateY(0);
        }
        
        .form-footer {
            text-align: center;
            margin-top: 25px;
            font-size: 14px;
            color: #666;
        }
        
        .form-footer a {
            color: #FF96A9;
            text-decoration: none;
            font-weight: 600;
        }
        
        .message {
            padding: 14px 18px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .message.error {
            background: #ffe8e8;
            color: #d63031;
            border: 1px solid #ffcccc;
        }
        
        .back-link {
            text-align: center;
            margin-top: 15px;
        }
        
        .back-link a {
            color: #999;
            text-decoration: none;
            font-size: 13px;
        }
        
        .back-link a:hover {
            color: #FF96A9;
        }
        
        @media (max-width: 640px) {
            .logo-text {
                font-size: 42px;
            }
            
            .welcome-text {
                font-size: 15px;
            }
            
            .top-section {
                padding: 50px 20px 80px;
            }
            
            .bottom-section {
                padding: 40px 20px 30px;
            }
            
            .form-title {
                font-size: 28px;
            }
        }
    </style>
</head>
<body>
    <div class="page-wrapper">
        <!-- Top Pink Section with Logo -->
        <div class="top-section">
            <div class="logo-text"><?php echo htmlspecialchars($logo_text); ?></div>
            <div class="welcome-text">مرحباً بعودتك!<br>سجلي الدخول للمتابعة</div>
            
            <!-- Wave SVG -->
            <svg class="wave" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" preserveAspectRatio="none">
                <path fill="#ffffff" fill-opacity="1" d="M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,122.7C672,117,768,139,864,138.7C960,139,1056,117,1152,101.3C1248,85,1344,75,1392,69.3L1440,64L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
            </svg>
        </div>
        
        <!-- Bottom White Section with Form -->
        <div class="bottom-section">
            <div class="form-container">
                <h1 class="form-title">تسجيل الدخول</h1>
                <p class="form-subtitle">أدخل بياناتك للوصول لحسابك</p>
                
                <?php if ($message): ?>
                    <div class="message <?php echo $message_type; ?>">
                        <?php echo htmlspecialchars($message); ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST">
                    <div class="input-group">
                        <label for="phone">رقم الهاتف</label>
                        <input type="tel" id="phone" name="phone" class="form-input-modern" placeholder="01234567890" required autocomplete="off">
                    </div>
                    
                    <div class="input-group">
                        <label for="password">كلمة المرور</label>
                        <input type="password" id="password" name="password" class="form-input-modern" placeholder="أدخل كلمة المرور" required autocomplete="off">
                    </div>
                    
                    <div class="forgot-link">
                        <a href="#" class="forgot-password">نسيت كلمة المرور؟</a>
                    </div>
                    
                    <button type="submit" class="btn-submit">تسجيل الدخول</button>
                </form>
                
                <div class="form-footer">
                    ليس لديك حساب؟ <a href="register.php">إنشاء حساب جديد</a>
                </div>
                
                <div class="back-link">
                    <a href="index.php">← العودة للمتجر</a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
