<?php
session_start();
include "db.php";

$error = "";
$showPopup = false;

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $type     = $_POST['type']; // number | email
    $password = trim($_POST['password']);
    $mobile   = $_POST['mobile'] ?? '';
    $email    = $_POST['email'] ?? '';

    if ($password == "") {
        $error = "পাসওয়ার্ড দিন";
        $showPopup = true;
    } 
    elseif ($type == "number" && $mobile == "") {
        $error = "মোবাইল নাম্বার দিন";
        $showPopup = true;
    } 
    elseif ($type == "email" && $email == "") {
        $error = "ইমেইল দিন";
        $showPopup = true;
    } 
    else {

        if($type == "number"){
            $sql = "SELECT * FROM users WHERE mobile = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("s", $mobile);
        }else{
            $sql = "SELECT * FROM users WHERE email = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("s", $email);
        }

        $stmt->execute();
        $result = $stmt->get_result();

        if($result->num_rows == 1){
            $user = $result->fetch_assoc();

            if(password_verify($password, $user['password'])){
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['mobile']  = $user['mobile'];
                $_SESSION['email']   = $user['email'];

                header("Location: dashboard.php");
                exit;
            }else{
                $error = "ভুল পাসওয়ার্ড";
                $showPopup = true;
            }
        }else{
            $error = "এই তথ্যের কোনো অ্যাকাউন্ট নেই";
            $showPopup = true;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AMD Login</title>

<style>
*{box-sizing:border-box;font-family:Arial, Helvetica, sans-serif}
body{
    margin:0;
    min-height:100vh;
    background:linear-gradient(135deg,#120021,#2a003d);
    display:flex;
    justify-content:center;
    align-items:center;
}
.container{
    width:100%;
    max-width:380px;
    padding:24px;
    color:#fff;
    text-align:center;
}
.logo img{width:200px;margin-bottom:12px}
h2{margin:5px 0 6px}
.sub{font-size:14px;color:#00ffcc;margin-bottom:20px}

.tabs{display:flex;gap:6px;margin-bottom:20px}
.tabs button{
    flex:1;padding:12px;border:none;border-radius:14px;
    background:#3b1657;color:#fff;font-size:15px
}
.tabs button.active{background:#00ffcc;color:#000}

input{
    width:100%;padding:14px;margin-bottom:14px;
    border:none;border-radius:16px;
    background:#3b1657;color:#fff
}
button.submit{
    width:100%;padding:15px;background:#00b33c;
    border:none;border-radius:18px;color:#fff;font-size:16px
}
.hidden{display:none}

.register{
    margin-top:18px;
    font-size:14px;
    color:#00ffcc;
}
.register a{
    color:#00ffcc;
    text-decoration:none;
    font-weight:bold;
}

/* POPUP */
.popup{
    position:fixed;inset:0;background:rgba(0,0,0,.7);
    display:none;align-items:center;justify-content:center;z-index:999;
}
.popup-box{
    background:#ff0033;
    width:85%;max-width:300px;
    border-radius:18px;padding:22px;text-align:center
}
.popup-box h3{margin:0 0 10px}
.popup-box button{
    margin-top:15px;width:100%;padding:12px;
    border:none;border-radius:12px;background:#000;color:#fff
}
</style>
</head>
<body>

<div class="container">
    <div class="logo">
        <img src="https://i.postimg.cc/Vv89Mk8b/61710-removebg-preview.png">
    </div>

    <!-- ✅ NEW TEXT -->
    <h2>প্রবেশ করুন</h2>
    <div class="sub">আপনার একাউন্ট এ লগইন করুন</div>

    <div class="tabs">
        <button id="btnNumber" class="active" onclick="showNumber()">নাম্বার</button>
        <button id="btnEmail" onclick="showEmail()">ইমেইল</button>
    </div>

    <form method="post">
        <input type="hidden" name="type" id="type" value="number">

        <div id="numberBox">
            <input type="text" name="mobile" placeholder="মোবাইল নম্বর">
        </div>

        <div id="emailBox" class="hidden">
            <input type="email" name="email" placeholder="ইমেইল">
        </div>

        <input type="password" name="password" placeholder="পাসওয়ার্ড">

        <button class="submit" type="submit">সাইন ইন</button>
    </form>

    <!-- ✅ REGISTER -->
    <div class="register">
        একাউন্ট নেই?
        <a href="register.php">নিবন্ধন করুন</a>
    </div>
</div>

<!-- ❌ ERROR POPUP -->
<div class="popup" id="errorPopup">
    <div class="popup-box">
        <h3>⚠ লগইন ব্যর্থ</h3>
        <p><?= $error ?></p>
        <button onclick="closePopup()">ঠিক আছে</button>
    </div>
</div>

<script>
function showNumber(){
    numberBox.classList.remove("hidden");
    emailBox.classList.add("hidden");
    btnNumber.classList.add("active");
    btnEmail.classList.remove("active");
    type.value="number";
}
function showEmail(){
    emailBox.classList.remove("hidden");
    numberBox.classList.add("hidden");
    btnEmail.classList.add("active");
    btnNumber.classList.remove("active");
    type.value="email";
}
function closePopup(){
    document.getElementById("errorPopup").style.display="none";
}
<?php if($showPopup): ?>
document.getElementById("errorPopup").style.display="flex";
<?php endif; ?>
</script>

</body>
</html>