<?php
if (!defined('BASEPATH')) {
    exit('Direct access not allowed');
}

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (isset($_SESSION['logout_success'])) {
    $logout_message = $_SESSION['logout_success'];
    unset($_SESSION['logout_success']);
}

if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Secure Admin Login</title>

<link href="https://cdn.rentalpanelbd.com/cd7f76/main.649f08559dd1a6f53f02.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<style>
body{
    margin:0;
    min-height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
    font-family:'Segoe UI',sans-serif;
    transition:.3s;
}
body.day-mode{
    background:linear-gradient(135deg,#eaf2ff,#ffffff);
    color:#000;
}
body.night-mode{
    background:linear-gradient(135deg,#020617,#0f172a);
    color:#fff;
}

.login-card{
    width:100%;
    max-width:380px;
    background:#fff;
    border-radius:18px;
    padding:30px 25px 25px;
    box-shadow:0 15px 40px rgba(0,0,0,.15);
}
.night-mode .login-card{
    background:#020617;
    box-shadow:0 15px 40px rgba(0,0,0,.6);
}

.login-avatar{
    width:95px;
    height:95px;
    border-radius:50%;
    margin:-80px auto 10px;
    background:#0d6efd;
    box-shadow:0 10px 30px rgba(13,110,253,.5);
    overflow:hidden;
    transition:.3s;
}
.login-avatar:hover{
    transform:scale(1.07);
}
.login-avatar img{
    width:100%;
    height:100%;
    object-fit:cover;
}

.login-title{
    text-align:center;
    font-weight:700;
    font-size:20px;
    margin-bottom:18px;
}

.form-control{
    border-radius:10px;
    height:44px;
}

.input-group-addon{
    border-radius:0 10px 10px 0;
    cursor:pointer;
}

.login-btn{
    border-radius:12px;
    height:46px;
    font-weight:600;
    font-size:15px;
}

#dayNightToggle{
    position:fixed;
    top:16px;
    right:16px;
    border:none;
    background:#fff;
    width:42px;
    height:42px;
    border-radius:50%;
    box-shadow:0 4px 12px rgba(0,0,0,.2);
    font-size:18px;
    cursor:pointer;
}
.night-mode #dayNightToggle{
    background:#020617;
    color:#fff;
}
</style>
</head>

<body class="day-mode" id="body">

<button onclick="toggleDayNightMode()" id="dayNightToggle">
    <i class="fas fa-sun"></i>
</button>

<div class="login-card">

<div class="login-avatar">
    <a href="https://wa.me/918416916861?text=mujhe%20website%20banvana%20hai" target="_blank" title="Chat on WhatsApp">
        <img src="https://cdn.myrentalpanel.com/files/files/images/6965f2e2e5fef_1768288994.jpg" alt="Admin">
    </a>
</div>

<div class="login-title">Secure Admin Login</div>

<form action="admin" method="post" autocomplete="off">

<?php if (!empty($success)) : ?>
<div class="alert alert-success"><?= htmlspecialchars($successText,ENT_QUOTES,'UTF-8') ?></div>
<?php elseif (!empty($error)) : ?>
<div class="alert alert-danger"><?= htmlspecialchars($errorText,ENT_QUOTES,'UTF-8') ?></div>
<?php endif; ?>

<?php if (!empty($logout_message)): ?>
<div class="alert alert-success"><?= htmlspecialchars($logout_message,ENT_QUOTES,'UTF-8') ?></div>
<?php endif; ?>

<div class="form-group">
<label>Username</label>
<input class="form-control" type="text" name="username" required>
</div>

<div class="form-group">
<label>Password</label>
<div class="input-group">
<input id="password" class="form-control" type="password" name="password" required>
<span class="input-group-addon btn-primary" onclick="showPassword(this)">
<i class="fas fa-eye"></i>
</span>
</div>
</div>

<div class="form-group">
<label>Two-Factor Code</label>
<input class="form-control" type="number" name="two_factor_code" placeholder="If enabled">
</div>

<?php if (!empty($_SESSION["recaptcha"])) : ?>
<div class="form-group text-center">
<div class="g-recaptcha" data-sitekey="<?= htmlspecialchars($settings["recaptcha_key"],ENT_QUOTES,'UTF-8') ?>"></div>
</div>
<?php endif; ?>

<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token'],ENT_QUOTES,'UTF-8') ?>">

<button type="submit" class="btn btn-primary btn-block login-btn">
<i class="fas fa-lock"></i> Login Securely
</button>

</form>
</div>

<script>
function showPassword(el){
    let p=document.getElementById("password");
    if(p.type==="password"){
        p.type="text";
        el.innerHTML='<i class="fas fa-eye-slash"></i>';
    }else{
        p.type="password";
        el.innerHTML='<i class="fas fa-eye"></i>';
    }
}

function toggleDayNightMode(){
    let body=document.getElementById("body");
    let btn=document.getElementById("dayNightToggle");
    if(body.classList.contains("day-mode")){
        body.className="night-mode";
        btn.innerHTML='<i class="fas fa-moon"></i>';
        localStorage.setItem("theme","night");
    }else{
        body.className="day-mode";
        btn.innerHTML='<i class="fas fa-sun"></i>';
        localStorage.setItem("theme","day");
    }
}

window.onload=function(){
    if(localStorage.getItem("theme")==="night"){
        document.getElementById("body").className="night-mode";
        document.getElementById("dayNightToggle").innerHTML='<i class="fas fa-moon"></i>';
    }
}
</script>

</body>
</html>l