<?php
session_start();
include 'config/db.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];
    
    $result = $conn->query("SELECT * FROM users WHERE email='$email'");
    if ($result->num_rows) {
        $user = $result->fetch_assoc();
        if (password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['user_name'] = $user['name'];
            
            if ($user['role'] == 'admin') {
                header("Location: admin/dashboard.php");
            } else {
                header("Location: user/dashboard.php");
            }
            exit();
        } else {
            $error = "Invalid password! Please try again.";
        }
    } else {
        $error = "User not found! Please check your email or register.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>Login - Bishnupriya Manipuri Sheldang</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-container {
            width: 100%;
            max-width: 450px;
        }

        .login-card {
            background: white;
            border-radius: 24px;
            padding: 40px 32px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
            position: relative;
        }

        .login-card:hover {
            transform: translateY(-5px);
        }

        .back-home-btn {
            position: absolute;
            top: 20px;
            left: 20px;
            background: #f0f0f0;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            color: #666;
            transition: all 0.3s;
        }

        .back-home-btn:hover {
            background: #667eea;
            color: white;
            transform: translateX(-3px);
        }

        .logo-section {
            text-align: center;
            margin-bottom: 32px;
        }

        .logo-image {
            width: 80px;
            height: 80px;
            object-fit: contain;
            margin: 0 auto 16px;
            display: block;
        }

        .logo-section h2 {
            font-size: 1.5rem;
            color: #333;
            font-weight: 600;
        }

        .logo-section p {
            font-size: 0.85rem;
            color: #666;
            margin-top: 5px;
        }

        .alert {
            padding: 12px 16px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 0.85rem;
            animation: slideIn 0.3s ease;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .alert-error {
            background: #fee2e2;
            border-left: 4px solid #dc2626;
            color: #991b1b;
        }

        .alert-success {
            background: #d1fae5;
            border-left: 4px solid #10b981;
            color: #065f46;
        }

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

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 0.85rem;
            font-weight: 500;
            color: #333;
        }

        .form-group label i {
            margin-right: 6px;
            color: #667eea;
        }

        /* Password Field Container - Simplified */
        .password-container {
            position: relative;
            width: 100%;
        }

        .password-container input {
            width: 100%;
            padding: 12px 45px 12px 42px;
            border: 1px solid #e0e0e0;
            border-radius: 12px;
            font-size: 0.9rem;
            font-family: 'Poppins', sans-serif;
            transition: all 0.3s;
            outline: none;
        }

        .password-container input:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        /* Left Icon */
        .input-icon-left {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
            font-size: 1rem;
            z-index: 1;
            pointer-events: none;
        }

        /* Password Toggle Button - Right Side */
        .password-toggle-btn {
            position: absolute;
            right: 14px;
            top: 50%;
            transform: translateY(-50%);
            background: transparent;
            border: none;
            cursor: pointer;
            color: #999;
            font-size: 1rem;
            padding: 5px;
            z-index: 2;
            transition: color 0.3s;
        }

        .password-toggle-btn:hover {
            color: #667eea;
        }

        /* Email Field Container */
        .input-container {
            position: relative;
            width: 100%;
        }

        .input-container input {
            width: 100%;
            padding: 12px 12px 12px 42px;
            border: 1px solid #e0e0e0;
            border-radius: 12px;
            font-size: 0.9rem;
            font-family: 'Poppins', sans-serif;
            transition: all 0.3s;
            outline: none;
        }

        .input-container input:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .input-icon {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
            font-size: 1rem;
            pointer-events: none;
        }

        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;
            font-size: 0.8rem;
        }

        .checkbox {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            color: #666;
        }

        .checkbox input {
            width: 16px;
            height: 16px;
            cursor: pointer;
        }

        .forgot-link {
            color: #667eea;
            text-decoration: none;
            transition: color 0.3s;
        }

        .forgot-link:hover {
            color: #5a67d8;
            text-decoration: underline;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }

        .register-link {
            text-align: center;
            margin-top: 24px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
        }

        .register-link p {
            color: #666;
            font-size: 0.85rem;
        }

        .register-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        .loading {
            display: inline-block;
            width: 18px;
            height: 18px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 0.6s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        @media (max-width: 480px) {
            .login-card {
                padding: 32px 24px;
            }
            
            .logo-image {
                width: 65px;
                height: 65px;
            }
            
            .logo-section h2 {
                font-size: 1.3rem;
            }
            
            .back-home-btn {
                top: 15px;
                left: 15px;
                width: 32px;
                height: 32px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <a href="index.php" class="back-home-btn" title="Back to Home">
                <i class="fas fa-arrow-left"></i>
            </a>

            <div class="logo-section">
                <img src="https://bmyouth.org/fund/assets/logo.png" 
                     alt="BM Fund Logo" 
                     class="logo-image"
                     onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 100 100%27%3E%3Ccircle cx=%2750%27 cy=%2750%27 r=%2745%27 fill=%27%23667eea%27/%3E%3Ctext x=%2750%27 y=%2770%27 text-anchor=%27middle%27 fill=%27white%27 font-size=%2740%27 font-weight=%27bold%27%3EB%3C/text%3E%3C/svg%3E'">
                <h2>Welcome Back</h2>
                <p>Login to your Bishnupriya Manipuri Sheldang account</p>
            </div>

            <?php if($error): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <span><?= $error ?></span>
            </div>
            <?php endif; ?>

            <?php if($success): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <span><?= $success ?></span>
            </div>
            <?php endif; ?>

            <form method="POST" id="loginForm">
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address</label>
                    <div class="input-container">
                        <i class="fas fa-envelope input-icon"></i>
                        <input type="email" name="email" id="email" placeholder="Enter your email" required>
                    </div>
                </div>

                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Password</label>
                    <div class="password-container">
                        <i class="fas fa-lock input-icon-left"></i>
                        <input type="password" name="password" id="password" placeholder="Enter your password" required>
                        <button type="button" class="password-toggle-btn" id="togglePassword">
                            <i class="fas fa-eye"></i>
                        </button>
                    </div>
                </div>

                <div class="form-options">
                    <label class="checkbox">
                        <input type="checkbox" name="remember" id="remember">
                        <span>Remember me</span>
                    </label>
                    <a href="forgot_password.php" class="forgot-link">Forgot Password?</a>
                </div>

                <button type="submit" class="login-btn" id="loginBtn">
                    <i class="fas fa-sign-in-alt"></i>
                    <span>Sign In</span>
                </button>
            </form>

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

    <div style="position: fixed; bottom: 10px; right: 10px; background: rgba(0,0,0,0.7); color: white; padding: 8px 12px; border-radius: 8px; font-size: 11px; font-family: monospace; z-index: 999;">
        <i class="fas fa-info-circle"></i> Help: +91 80993 91473
    </div>

    <script>
        // Simple and direct password toggle function
        (function() {
            // Get elements
            const toggleBtn = document.getElementById('togglePassword');
            const passwordField = document.getElementById('password');
            
            if (toggleBtn && passwordField) {
                console.log('Password toggle elements found');
                
                // Add click event
                toggleBtn.addEventListener('click', function() {
                    console.log('Toggle button clicked');
                    
                    // Check current type
                    const currentType = passwordField.type;
                    
                    // Toggle between password and text
                    if (currentType === 'password') {
                        passwordField.type = 'text';
                        this.innerHTML = '<i class="fas fa-eye-slash"></i>';
                        console.log('Password shown');
                    } else {
                        passwordField.type = 'password';
                        this.innerHTML = '<i class="fas fa-eye"></i>';
                        console.log('Password hidden');
                    }
                });
            } else {
                console.log('Toggle button or password field not found');
                console.log('Toggle button:', toggleBtn);
                console.log('Password field:', passwordField);
            }
        })();
        
        // Remember me functionality
        const rememberCheckbox = document.getElementById('remember');
        const emailInput = document.getElementById('email');
        
        if (localStorage.getItem('rememberedEmail') && rememberCheckbox && emailInput) {
            emailInput.value = localStorage.getItem('rememberedEmail');
            rememberCheckbox.checked = true;
        }
        
        if (rememberCheckbox && emailInput) {
            rememberCheckbox.addEventListener('change', function() {
                if (this.checked) {
                    localStorage.setItem('rememberedEmail', emailInput.value);
                } else {
                    localStorage.removeItem('rememberedEmail');
                }
            });
            
            emailInput.addEventListener('change', function() {
                if (rememberCheckbox.checked) {
                    localStorage.setItem('rememberedEmail', this.value);
                }
            });
        }
        
        // Form submission loading state
        const loginForm = document.getElementById('loginForm');
        const loginBtn = document.getElementById('loginBtn');
        
        if (loginForm && loginBtn) {
            loginForm.addEventListener('submit', function(e) {
                if (loginForm.checkValidity()) {
                    const originalText = loginBtn.innerHTML;
                    loginBtn.innerHTML = '<span class="loading"></span> Signing in...';
                    loginBtn.disabled = true;
                    
                    setTimeout(() => {
                        if (loginBtn.disabled) {
                            loginBtn.innerHTML = originalText;
                            loginBtn.disabled = false;
                        }
                    }, 30000);
                }
            });
        }
        
        // Animation on load
        document.querySelectorAll('.form-group, .form-options, .login-btn, .register-link').forEach((el, index) => {
            el.style.opacity = '0';
            el.style.transform = 'translateY(10px)';
            el.style.animation = `slideIn 0.4s ease forwards ${index * 0.05}s`;
        });
    </script>
</body>
</html>