<?php
require_once '../config.php';
if (isLoggedIn() && $_SESSION['role'] === 'user') redirect(SITE_URL . '/admin/user/dashboard.php');

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitize($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password.';
    } else {
        $stmt = $conn->prepare("SELECT * FROM users WHERE username = ? LIMIT 1");
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $user = $stmt->get_result()->fetch_assoc();
        if ($user && password_verify($password, $user['password'])) {
            if ($user['status'] === 'hold') {
                $error = 'Your account is on hold. Please contact your Sub Admin.';
            } elseif ($user['status'] === 0 || $user['status'] === '0' || $user['status'] === 'inactive') {
                $error = 'Your account is inactive. Please contact your Sub Admin.';
            } else {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                $_SESSION['full_name'] = !empty($user['full_name']) ? $user['full_name'] : trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? ''));
                $_SESSION['role'] = 'user';
                $_SESSION['sub_admin_id'] = $user['sub_admin_id'] ?? $user['subadmin_id'] ?? 1;
                redirect(SITE_URL . '/admin/user/dashboard.php');
            }
        } else {
            $error = 'Invalid username or password.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Login - Cricket Exchange</title>
    <link rel="stylesheet" href="../assets/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
</head>
<body>
    <div class="login-bg-particles"></div>
    <div class="login-page">
        <div class="login-box">
            <div class="login-logo">
                <div class="logo-icon" style="background:linear-gradient(135deg,var(--primary),var(--primary-dark))">🙋</div>
                <h1>CRICKET EXCHANGE</h1>
                <p>User Portal</p>
            </div>
            <div class="role-selector">
                <a href="index.php" class="role-btn">🔐 Master Admin</a>
                <a href="subadmin_login.php" class="role-btn">👤 Sub Admin</a>
                <span class="role-btn active">🙋 User</span>
            </div>
            <?php if ($error): ?><div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> <?= htmlspecialchars($error) ?></div><?php endif; ?>
            <form method="POST" id="loginForm">
                <div class="form-group">
                    <label>Username</label>
                    <div class="input-wrapper">
                        <i class="fas fa-user input-icon"></i>
                        <input type="text" name="username" class="form-control" placeholder="Your username" required>
                    </div>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock input-icon"></i>
                        <input type="password" name="password" id="password" class="form-control" placeholder="Your password" required>
                        <button type="button" onclick="togglePass('password')" style="position:absolute;right:14px;top:50%;transform:translateY(-50%);background:none;border:none;color:var(--text-muted);cursor:pointer;font-size:16px;"><i class="fas fa-eye" id="password-eye"></i></button>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary btn-full" id="loginBtn">
                    <i class="fas fa-sign-in-alt"></i> Login
                </button>
            </form>
            <div style="text-align:center;margin-top:20px;font-size:12px;color:var(--text-dim);">
                Contact your Sub Admin for account access
            </div>
        </div>
    </div>
    <script>
    function togglePass(id) {
        const input = document.getElementById(id), eye = document.getElementById(id+'-eye');
        input.type = input.type==='password'?'text':'password';
        eye.className = input.type==='password'?'fas fa-eye':'fas fa-eye-slash';
    }
    document.getElementById('loginForm').addEventListener('submit',function(){
        const btn=document.getElementById('loginBtn');
        btn.innerHTML='<div class="spinner"></div> Logging in...';
        btn.disabled=true;
    });
    </script>
    <style>
    .role-selector{display:flex;gap:8px;margin-bottom:28px;background:var(--bg-card2);padding:4px;border-radius:10px;}
    .role-btn{flex:1;padding:8px;text-align:center;border-radius:8px;border:none;font-size:12px;font-weight:600;cursor:pointer;transition:all 0.2s;text-decoration:none;display:block;color:var(--text-muted);background:none;}
    .role-btn.active{background:linear-gradient(135deg,var(--primary),var(--primary-dark));color:#000;}
    </style>
</body>
</html>
