<?php
session_start();
require("config.php");

/* ================= GET SETTINGS ================= */
$set = $pdo->query("SELECT * FROM settings ORDER BY id DESC LIMIT 1");
$site = $set->fetch(PDO::FETCH_ASSOC);

/* ================= SITE DATA ================= */
$title        = $site['title'];
$domain_name  = $site['domain_name'];
$developa     = $site['developa'];
$logo         = $site['LOGO'];
$favicon      = $site['favicon'];
$url          = $site['url'];

/* ================= LOGIN ================= */
if(isset($_POST['login'])){

    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    $stmt = $pdo->prepare("SELECT * FROM logins WHERE email=?");
    $stmt->execute([$email]);

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if($user && password_verify($password, $user['password'])){

        $_SESSION['user_id'] = $user['id'];
        $_SESSION['email']   = $user['email'];

        header("Location: user/dashboard.php");
        exit();

    }else{
        $error = "Invalid login details!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title><?php echo htmlspecialchars($title); ?></title>

<meta name="title" content="<?php echo htmlspecialchars($domain_name); ?> - Login">

<link rel="icon" type="image/png"
href="user/uploads/<?php echo htmlspecialchars($favicon); ?>">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

/* =====================================
   BODY
===================================== */

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{
    min-height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
    background:
    linear-gradient(rgba(8,12,20,0.88),rgba(8,12,20,0.88)),
    url('assets/styles/images/bannerbg.jpg');
    background-size:cover;
    background-position:center;
    font-family:'Segoe UI',sans-serif;
    padding:20px;
}

/* =====================================
   LOGIN BOX
===================================== */

.auth-box{
    width:100%;
    max-width:450px;
    background:rgba(20,25,38,0.92);
    backdrop-filter:blur(18px);
    border-radius:24px;
    padding:40px 35px;
    border:1px solid rgba(255,255,255,0.05);
    box-shadow:0 15px 60px rgba(0,0,0,0.45);
    animation:fadeIn 0.8s ease;
    overflow:hidden;
}

/* =====================================
   LOGO
===================================== */

.logo-box{
    text-align:center;
    margin-bottom:25px;
}

.logo-box img{
    max-width:180px;
    max-height:70px;
    object-fit:contain;
}

/* =====================================
   TITLE
===================================== */

.auth-title{
    text-align:center;
    margin-bottom:30px;
}

.auth-title h2{
    color:#fff;
    font-size:30px;
    font-weight:700;
    margin-bottom:8px;
}

.auth-title h2 span{
    color:#CCA354;
}

.auth-title p{
    color:#bdbdbd;
    font-size:14px;
    line-height:24px;
}

/* =====================================
   ALERT
===================================== */

.alert{
    background:#ff4d4d;
    color:#fff;
    padding:14px;
    border-radius:12px;
    margin-bottom:20px;
    text-align:center;
    font-size:14px;
}

/* =====================================
   INPUTS
===================================== */

.input-group{
    position:relative;
    margin-bottom:22px;
}

.input-group i{
    position:absolute;
    top:50%;
    left:16px;
    transform:translateY(-50%);
    color:#9a9a9a;
    font-size:15px;
}

.input-group input{
    width:100%;
    height:55px;
    border:none;
    border-radius:14px;
    background:#1d2330;
    color:#fff;
    padding:0 18px 0 48px;
    font-size:15px;
    transition:0.3s;
}

.input-group input:focus{
    outline:none;
    border:1px solid #CCA354;
    box-shadow:0 0 12px rgba(204,163,84,0.18);
}

/* =====================================
   BUTTON
===================================== */

.btn-custom{
    width:100%;
    height:55px;
    border:none;
    border-radius:14px;
    background:linear-gradient(135deg,#CCA354,#8b6c28);
    color:#fff;
    font-size:16px;
    font-weight:700;
    cursor:pointer;
    transition:0.3s;
    margin-top:5px;
}

.btn-custom:hover{
    transform:translateY(-2px);
}

/* =====================================
   FOOTER
===================================== */

.auth-footer{
    text-align:center;
    margin-top:25px;
    color:#bdbdbd;
    font-size:14px;
}

.auth-footer a{
    color:#CCA354;
    text-decoration:none;
    font-weight:600;
}

/* =====================================
   COPYRIGHT
===================================== */

.copyright{
    margin-top:20px;
    text-align:center;
    color:#777;
    font-size:12px;
}

/* =====================================
   ANIMATION
===================================== */

@keyframes fadeIn{

    from{
        opacity:0;
        transform:translateY(25px);
    }

    to{
        opacity:1;
        transform:translateY(0);
    }
}

/* =====================================
   MOBILE
===================================== */

@media(max-width:600px){

    .auth-box{
        padding:30px 22px;
        border-radius:20px;
    }

    .auth-title h2{
        font-size:24px;
    }

    .logo-box img{
        max-width:150px;
    }

}

/* =====================================
   FORGOT PASSWORD
===================================== */

.forgot-password{
    text-align:right;
    margin-top:-10px;
    margin-bottom:20px;
}

.forgot-password a{
    color:#CCA354;
    text-decoration:none;
    font-size:14px;
    transition:0.3s;
}

.forgot-password a:hover{
    color:#fff;
}

</style>

</head>

<body>

<div class="auth-box">

    <!-- LOGO -->
    <div class="logo-box">

        <?php if(!empty($logo)){ ?>

            <img
            src="user/uploads/<?php echo htmlspecialchars($logo); ?>"
            alt="<?php echo htmlspecialchars($domain_name); ?>">

        <?php } ?>

    </div>

    <!-- TITLE -->
    <div class="auth-title">

        <h2>
            Investor <span>Login</span>
        </h2>

        <p>
            Secure access to your investment dashboard
        </p>

    </div>

    <!-- ERROR -->
    <?php if(!empty($error)){ ?>

        <div class="alert">
            <?php echo $error; ?>
        </div>

    <?php } ?>

    <!-- FORM -->
    <form method="POST">

        <!-- EMAIL -->
        <div class="input-group">

            <i class="fa fa-envelope"></i>

            <input
            type="email"
            name="email"
            placeholder="Email Address"
            required>

        </div>

        <!-- PASSWORD -->
        <div class="input-group">

            <i class="fa fa-lock"></i>

            <input
            type="password"
            name="password"
            placeholder="Password"
            required>

        </div>

<!-- FORGOT PASSWORD -->
<div class="forgot-password">

    <a href="forgot-password.php">
        <i class="fa fa-key"></i>
        Forgot Password?
    </a>

</div>
        <!-- BUTTON -->
        <button type="submit" name="login" class="btn-custom">

            <i class="fa fa-sign-in-alt"></i>
            Login Account

        </button>

    </form>

    <!-- FOOTER -->
    <div class="auth-footer">

        Don't have an account?

        <a href="signup.php">
            Create Account
        </a>

    </div>

    <!-- COPYRIGHT -->
    <div class="copyright">

        © <?php echo date('Y'); ?>
        <?php echo htmlspecialchars($domain_name); ?>

    </div>

</div>

<?php echo $developa; ?>

</body>
</html>