<?php
/**
 * SMM Paket Satış Scripti - Giriş Sayfası
 * @version 1.1.0
 */

require_once __DIR__ . '/config.php';

// Zaten giriş yapmışsa ana sayfaya yönlendir
if (isLoggedIn()) {
    redirect(SITE_URL);
    exit;
}

// CSRF Token
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrfToken = $_SESSION['csrf_token'];

// POST işlemi
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // CSRF kontrolü
    if (!hash_equals($csrfToken, $_POST['csrf_token'] ?? '')) {
        setFlash('error', 'Güvenlik hatası. Lütfen tekrar deneyin.');
        redirect(SITE_URL . '/giris.php');
        exit;
    }
    
    $login = trim($_POST['login'] ?? ''); // E-posta veya kullanıcı adı
    $password = $_POST['password'] ?? '';
    $remember = isset($_POST['remember']);
    
    // Validasyon
    $errors = [];
    
    if (empty($login)) {
        $errors[] = 'E-posta veya kullanıcı adı gereklidir.';
    }
    
    if (empty($password)) {
        $errors[] = 'Şifre gereklidir.';
    }
    
    if (empty($errors)) {
        try {
            // E-posta mı kullanıcı adı mı kontrol et
            $isEmail = filter_var($login, FILTER_VALIDATE_EMAIL);
            
            if ($isEmail) {
                $stmt = $db->prepare("SELECT * FROM users WHERE email = ? LIMIT 1");
            } else {
                $stmt = $db->prepare("SELECT * FROM users WHERE username = ? LIMIT 1");
            }
            
            $stmt->execute([$login]);
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($user && password_verify($password, $user['password'])) {
                // Hesap durumu kontrolü
                if ($user['status'] === 'banned') {
                    setFlash('error', 'Hesabınız engellenmiştir. Lütfen destek ile iletişime geçin.');
                    redirect(SITE_URL . '/giris.php');
                    exit;
                }
                
                if ($user['status'] === 'inactive') {
                    setFlash('error', 'Hesabınız aktif değil. Lütfen e-postanızı kontrol edin.');
                    redirect(SITE_URL . '/giris.php');
                    exit;
                }
                
                // Session'ı başlat
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_email'] = $user['email'];
                $_SESSION['user_name'] = $user['full_name'] ?: $user['username'];
                $_SESSION['user_role'] = $user['role'];
                
                // Beni hatırla
                if ($remember) {
                    $token = bin2hex(random_bytes(32));
                    $expires = time() + (30 * 24 * 60 * 60); // 30 gün
                    setcookie('remember_token', $token, $expires, '/', '', true, true);
                }
                
                setFlash('success', 'Hoş geldiniz, ' . clean($user['full_name'] ?: $user['username']) . '!');
                
                // Admin ise admin panele, değilse ana sayfaya yönlendir
                if ($user['role'] === 'admin') {
                    redirect(SITE_URL . '/admin/');
                } else {
                    redirect(SITE_URL);
                }
                exit;
                
            } else {
                setFlash('error', 'E-posta/kullanıcı adı veya şifre hatalı.');
                redirect(SITE_URL . '/giris.php');
                exit;
            }
            
        } catch (PDOException $e) {
            setFlash('error', 'Bir hata oluştu. Lütfen tekrar deneyin.');
            redirect(SITE_URL . '/giris.php');
            exit;
        }
    } else {
        setFlash('error', implode('<br>', $errors));
        redirect(SITE_URL . '/giris.php');
        exit;
    }
}

$pageTitle = 'Giriş Yap';
require_once __DIR__ . '/header.php';
?>

<style>
.auth-section {
    min-height: calc(100vh - 80px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    position: relative;
    overflow: hidden;
}

.auth-section::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle at 30% 50%, rgba(var(--primary-rgb), 0.08) 0%, transparent 50%),
                radial-gradient(circle at 70% 80%, rgba(var(--secondary-rgb), 0.06) 0%, transparent 40%);
    animation: bgMove 20s ease-in-out infinite;
    z-index: -1;
}

@keyframes bgMove {
    0%, 100% { transform: translate(0, 0); }
    50% { transform: translate(-20px, -20px); }
}

.auth-container {
    width: 100%;
    max-width: 460px;
}

.auth-card {
    background: rgba(30, 41, 59, 0.8);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 24px;
    padding: 45px 40px;
    box-shadow: 0 25px 80px rgba(0, 0, 0, 0.4);
    position: relative;
    overflow: hidden;
}

.auth-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--gradient);
}

.auth-header {
    text-align: center;
    margin-bottom: 35px;
}

.auth-icon {
    width: 70px;
    height: 70px;
    background: var(--gradient);
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #fff;
    margin: 0 auto 20px;
    box-shadow: 0 10px 30px rgba(var(--primary-rgb), 0.4);
}

.auth-header h1 {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 8px;
    color: #fff;
}

.auth-header p {
    color: rgba(255, 255, 255, 0.6);
    font-size: 15px;
    margin: 0;
}

.form-group {
    margin-bottom: 22px;
}

.form-label {
    display: block;
    font-size: 14px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.9);
    margin-bottom: 10px;
}

.form-label i {
    margin-right: 8px;
    color: var(--primary);
}

.input-wrapper {
    position: relative;
}

.form-control {
    width: 100%;
    padding: 16px 20px;
    background: rgba(15, 23, 42, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    color: #fff;
    font-size: 15px;
    transition: all 0.3s ease;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    background: rgba(15, 23, 42, 0.8);
    box-shadow: 0 0 0 4px rgba(var(--primary-rgb), 0.15);
}

.form-control::placeholder {
    color: rgba(255, 255, 255, 0.35);
}

.input-wrapper .toggle-password {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.4);
    cursor: pointer;
    padding: 5px;
    font-size: 16px;
    transition: color 0.3s;
}

.input-wrapper .toggle-password:hover {
    color: var(--primary);
}

.form-options {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 25px;
    flex-wrap: wrap;
    gap: 10px;
}

.remember-me {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.remember-me input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
}

.remember-me span {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.7);
}

.forgot-link {
    font-size: 14px;
    color: var(--primary);
    font-weight: 500;
    transition: all 0.3s;
}

.forgot-link:hover {
    color: var(--secondary);
    text-decoration: underline;
}

.btn-auth {
    width: 100%;
    padding: 18px 30px;
    background: var(--gradient);
    border: none;
    border-radius: 14px;
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    box-shadow: 0 10px 30px rgba(var(--primary-rgb), 0.4);
}

.btn-auth:hover {
    transform: translateY(-3px);
    box-shadow: 0 15px 40px rgba(var(--primary-rgb), 0.5);
}

.btn-auth:active {
    transform: translateY(-1px);
}

.btn-auth i {
    font-size: 18px;
}

.auth-divider {
    display: flex;
    align-items: center;
    margin: 30px 0;
    gap: 15px;
}

.auth-divider::before,
.auth-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: rgba(255, 255, 255, 0.1);
}

.auth-divider span {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.4);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.auth-footer {
    text-align: center;
}

.auth-footer p {
    color: rgba(255, 255, 255, 0.6);
    font-size: 15px;
    margin: 0;
}

.auth-footer a {
    color: var(--primary);
    font-weight: 600;
    margin-left: 5px;
    transition: all 0.3s;
}

.auth-footer a:hover {
    color: var(--secondary);
    text-decoration: underline;
}

.auth-features {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin-top: 35px;
    padding-top: 25px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.auth-feature {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.5);
}

.auth-feature i {
    color: var(--primary);
    font-size: 14px;
}

.input-hint {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.4);
    margin-top: 8px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.input-hint i {
    font-size: 11px;
}

@media (max-width: 576px) {
    .auth-card {
        padding: 35px 25px;
    }
    
    .auth-header h1 {
        font-size: 24px;
    }
    
    .auth-features {
        flex-direction: column;
        align-items: center;
        gap: 12px;
    }
    
    .form-options {
        flex-direction: column;
        align-items: flex-start;
    }
}
</style>

<section class="auth-section">
    <div class="auth-container" data-aos="fade-up" data-aos-duration="800">
        <div class="auth-card">
            <div class="auth-header">
                <div class="auth-icon">
                    <i class="fas fa-sign-in-alt"></i>
                </div>
                <h1>Hoş Geldiniz!</h1>
                <p>Hesabınıza giriş yaparak devam edin</p>
            </div>
            
            <form method="POST" action="" id="loginForm">
                <input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
                
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-user"></i> E-posta veya Kullanıcı Adı
                    </label>
                    <div class="input-wrapper">
                        <input type="text" name="login" class="form-control" 
                               placeholder="ornek@email.com veya kullaniciadi" required autocomplete="username"
                               value="<?php echo isset($_POST['login']) ? clean($_POST['login']) : ''; ?>">
                    </div>
                    <div class="input-hint">
                        <i class="fas fa-info-circle"></i>
                        E-posta adresiniz veya kullanıcı adınızla giriş yapabilirsiniz
                    </div>
                </div>
                
                <div class="form-group">
                    <label class="form-label">
                        <i class="fas fa-lock"></i> Şifre
                    </label>
                    <div class="input-wrapper">
                        <input type="password" name="password" id="password" class="form-control" 
                               placeholder="••••••••" required autocomplete="current-password">
                        <button type="button" class="toggle-password" onclick="togglePassword()">
                            <i class="fas fa-eye" id="toggleIcon"></i>
                        </button>
                    </div>
                </div>
                
                <div class="form-options">
                    <label class="remember-me">
                        <input type="checkbox" name="remember">
                        <span>Beni hatırla</span>
                    </label>
                    <a href="<?php echo SITE_URL; ?>/sifremi-unuttum.php" class="forgot-link">
                        Şifremi unuttum
                    </a>
                </div>
                
                <button type="submit" class="btn-auth">
                    <i class="fas fa-arrow-right"></i>
                    Giriş Yap
                </button>
            </form>
            
            <div class="auth-divider">
                <span>veya</span>
            </div>
            
            <div class="auth-footer">
                <p>Henüz hesabınız yok mu?<a href="<?php echo SITE_URL; ?>/kayit.php">Kayıt Ol</a></p>
            </div>
            
            <div class="auth-features">
                <div class="auth-feature">
                    <i class="fas fa-shield-alt"></i>
                    <span>Güvenli Giriş</span>
                </div>
                <div class="auth-feature">
                    <i class="fas fa-bolt"></i>
                    <span>Hızlı İşlem</span>
                </div>
                <div class="auth-feature">
                    <i class="fas fa-headset"></i>
                    <span>7/24 Destek</span>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
function togglePassword() {
    var passwordInput = document.getElementById('password');
    var toggleIcon = document.getElementById('toggleIcon');
    
    if (passwordInput.type === 'password') {
        passwordInput.type = 'text';
        toggleIcon.classList.remove('fa-eye');
        toggleIcon.classList.add('fa-eye-slash');
    } else {
        passwordInput.type = 'password';
        toggleIcon.classList.remove('fa-eye-slash');
        toggleIcon.classList.add('fa-eye');
    }
}

document.getElementById('loginForm').addEventListener('submit', function() {
    var btn = this.querySelector('.btn-auth');
    btn.disabled = true;
    btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Giriş yapılıyor...';
});
</script>

<?php require_once __DIR__ . '/footer.php'; ?>