<?php
session_start();
require 'includes/db.php';
$currency = $_COOKIE['currency_selected'] ?? 'USD';
$_SESSION['currency'] = $currency;
$error = "";

if ($_POST) {

    $customer_id = mysqli_real_escape_string($conn, $_POST['customer_id']);
    $password = $_POST['password'];

    // 🔍 GET USERS
    $res = mysqli_query($conn, "
        SELECT * FROM users WHERE customer_id='$customer_id'
    ");

    if (mysqli_num_rows($res) == 0) {
        $error = "Customer ID not found";
    } else {

        $login_user = null;

        while ($user = mysqli_fetch_assoc($res)) {

            // 🔍 GET PASSWORDS
            $pass_res = mysqli_query($conn, "
                SELECT * FROM user_passwords 
                WHERE user_id = '{$user['id']}'
            ");

            // ⚠️ If no password record, skip
            if (mysqli_num_rows($pass_res) == 0) {
                continue;
            }

            while ($row = mysqli_fetch_assoc($pass_res)) {

                if (password_verify($password, $row['password_hash'])) {
                    $login_user = $user;
                    break 2;
                }
            }
        }

        // ✅ LOGIN SUCCESS
        if ($login_user) {

            $current_ip = $_SERVER['REMOTE_ADDR'];
            $current_agent = $_SERVER['HTTP_USER_AGENT'];

            $_SESSION['user_id'] = $login_user['id'];

            // 🚨 DEVICE CHECK
            if ($login_user['last_ip'] !== $current_ip || $login_user['last_agent'] !== $current_agent) {

    $_SESSION['new_device'] = true;

    mysqli_query($conn, "
        UPDATE users 
        SET last_ip='$current_ip',
            last_agent='$current_agent'
        WHERE id='{$login_user['id']}'
    ");

    // 👉 NEW PAGE
    header("Location: device_alert.php");
    exit;
} else {

                mysqli_query($conn, "
                    UPDATE users 
                    SET last_ip='$current_ip',
                        last_agent='$current_agent'
                    WHERE id='{$login_user['id']}'
                ");

                header("Location: includes/auth_check.php");
                exit;
            }

        } else {
            $error = "Invalid password for this account";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Geneva & Exeter Coin</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html {
            scroll-behavior: smooth;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #4f46e5 0%, #3730a3 100%);
            min-height: 100vh;
            position: relative;
            overflow-x: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        /* Animated crypto background */
        .crypto-bg {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            overflow: hidden;
            pointer-events: none;
            z-index: 0;
        }

        .crypto-icon {
            position: absolute;
            opacity: 0.05;
            color: white;
            font-size: 48px;
            font-weight: bold;
            animation: float 8s ease-in-out infinite;
        }

        .crypto-icon:nth-child(1) { top: 10%; left: 5%; transform: rotate(-15deg); font-size: 64px; animation-delay: 0s; }
        .crypto-icon:nth-child(2) { top: 70%; left: 15%; transform: rotate(10deg); font-size: 56px; animation-delay: 2s; }
        .crypto-icon:nth-child(3) { top: 30%; right: 10%; transform: rotate(-5deg); font-size: 72px; animation-delay: 4s; }
        .crypto-icon:nth-child(4) { bottom: 20%; right: 25%; transform: rotate(20deg); font-size: 48px; animation-delay: 1s; }
        .crypto-icon:nth-child(5) { top: 50%; left: 40%; transform: rotate(-10deg); font-size: 60px; animation-delay: 3s; }
        .crypto-icon:nth-child(6) { top: 20%; left: 25%; transform: rotate(5deg); font-size: 52px; animation-delay: 5s; }
        .crypto-icon:nth-child(7) { top: 60%; right: 5%; transform: rotate(-10deg); font-size: 60px; animation-delay: 6s; }
        .crypto-icon:nth-child(8) { bottom: 30%; left: 8%; transform: rotate(15deg); font-size: 44px; animation-delay: 7s; }

        @keyframes float {
            0%, 100% { transform: translateY(0) rotate(var(--rotation, 0deg)); }
            50% { transform: translateY(-20px) rotate(var(--rotation, 0deg)); }
        }

        /* Login Container */
        .login-container {
            position: relative;
            z-index: 10;
            width: 100%;
            max-width: 440px;
        }

        /* Logo Section */
        .logo-section {
            text-align: center;
            margin-bottom: 32px;
        }

        .logo {
            display: inline-flex;
            align-items: center;
            gap: 12px;
            color: white;
            font-size: 28px;
            font-weight: 700;
            text-decoration: none;
            margin-bottom: 16px;
        }

        .logo-icon {
            width: 48px;
            height: 48px;
            background: white;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #4f46e5;
            font-weight: 800;
            font-size: 24px;
        }

        .welcome-text {
            color: rgba(255, 255, 255, 0.9);
            font-size: 16px;
        }

        /* Login Card */
        .login-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
        }

        .card-header {
            text-align: center;
            margin-bottom: 32px;
        }

        .card-title {
            font-size: 24px;
            font-weight: 800;
            color: #1e1b4b;
            margin-bottom: 8px;
        }

        .card-subtitle {
            font-size: 14px;
            color: #6b7280;
        }

        /* Form Styles */
        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #374151;
            margin-bottom: 8px;
        }

        .form-input {
            width: 100%;
            padding: 14px 16px;
            border: 2px solid #e5e7eb;
            border-radius: 12px;
            font-size: 15px;
            color: #374151;
            background: white;
            transition: all 0.2s;
            font-family: inherit;
        }

        .form-input:focus {
            outline: none;
            border-color: #4f46e5;
            box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
        }

        .form-input::placeholder {
            color: #9ca3af;
        }

        /* Submit Button */
        .btn-submit {
            width: 100%;
            padding: 16px 24px;
            background: #22c55e;
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            margin-top: 8px;
        }

        .btn-submit:hover {
            background: #16a34a;
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(34, 197, 94, 0.3);
        }

        .btn-submit:active {
            transform: translateY(0);
        }

        /* Card Footer */
        .card-footer {
            text-align: center;
            margin-top: 24px;
            padding-top: 24px;
            border-top: 1px solid #e5e7eb;
        }

        .footer-text {
            font-size: 14px;
            color: #6b7280;
        }

        .footer-link {
            color: #4f46e5;
            text-decoration: none;
            font-weight: 600;
            transition: color 0.2s;
        }

        .footer-link:hover {
            color: #3730a3;
            text-decoration: underline;
        }

        /* Back Link */
        .back-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            margin-top: 24px;
            transition: color 0.2s;
        }

        .back-link:hover {
            color: white;
        }

        .back-link svg {
            width: 16px;
            height: 16px;
        }

        /* Responsive */
        @media (max-width: 480px) {
            .login-card {
                padding: 28px;
            }

            .logo {
                font-size: 24px;
            }

            .logo-icon {
                width: 40px;
                height: 40px;
                font-size: 20px;
            }

            .card-title {
                font-size: 22px;
            }
        }
		.error-box {
    background: #fee2e2;
    color: #b91c1c;
    padding: 12px;
    border-radius: 10px;
    margin-bottom: 15px;
    font-size: 14px;
    text-align: center;
}
/* ================= LOGIN RESPONSIVE ================= */

/* LOGO IMAGE */
.logo-img{
    width:180px;
    height:auto;
    max-width:100%;
}

/* CONTAINER FIX */
.login-container{
    width:100%;
    max-width:420px;
    padding:0 10px;
}

/* CARD */
.login-card{
    width:100%;
}

/* INPUT FIX */
.form-input{
    font-size:16px; /* prevents zoom on iPhone */
}

/* BUTTON */
.btn-submit{
    width:100%;
}

/* 📱 MOBILE */
@media (max-width:768px){

    body{
        padding:15px;
    }

    .logo-img{
        width:140px;
    }

    .login-card{
        padding:25px;
        border-radius:16px;
    }

    .card-title{
        font-size:20px;
    }

    .welcome-text{
        font-size:14px;
    }

}

/* 📱 SMALL PHONES */
@media (max-width:480px){

    .logo-img{
        width:120px;
    }

    .login-card{
        padding:20px;
    }

    .form-input{
        padding:12px;
    }

    .btn-submit{
        padding:14px;
        font-size:15px;
    }
}
    </style>
</head>
<body>
    <!-- Crypto Background -->
    <div class="crypto-bg">
        <div class="crypto-icon">₿</div>
        <div class="crypto-icon">₿</div>
        <div class="crypto-icon">₿</div>
        <div class="crypto-icon">₿</div>
        <div class="crypto-icon">◊</div>
        <div class="crypto-icon">◊</div>
        <div class="crypto-icon">◎</div>
        <div class="crypto-icon">◎</div>
    </div>

    <!-- Login Container -->
    <div class="login-container">
        <!-- Logo Section -->
        <div class="logo-section">
            <a href="index.php" class="logo">
                <img src="img/logo_white.png" class="logo-img">
            </a>
            <p class="welcome-text">Welcome back! Please sign in to continue.</p>
        </div>

        <!-- Login Card -->
        <div class="login-card">
            <div class="card-header">
                <h1 class="card-title">Sign In</h1>
                <p class="card-subtitle">Enter your credentials to access your account</p>
                <?php if (!empty($error)): ?>
    <div class="error-box"><?php echo $error; ?></div>
<?php endif; ?>
            </div>

            <form method="POST">
                <div class="form-group">
                    <label class="form-label" for="id">Customer ID</label>
                    <input type="text" name="customer_id" placeholder="Customer ID" class="form-input" required>
                </div>

                <div class="form-group">
                    <label class="form-label" for="password">Password</label>
                    <input type="password" name="password" id="password" class="form-input" placeholder="••••••••" required>
                </div>

                <button type="submit" class="btn-submit">Sign In</button>
            </form>

            <div class="card-footer">
                <p class="footer-text">
                    Don't have an account? 
                    <a href="register.php" class="footer-link">Create one</a>
                </p>
            </div>
        </div>

        <!-- Back Link -->
        <div style="text-align: center;">
            <a href="index.php" class="back-link">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <line x1="19" y1="12" x2="5" y2="12"></line>
                    <polyline points="12 19 5 12 12 5"></polyline>
                </svg>
                Back to Home
            </a>
        </div>
    </div>


</body>
</html>