<?php
// Set agar cookie session tidak mati saat browser ditutup
// 2592000 detik = 30 Hari
ini_set('session.cookie_lifetime', 2592000);
ini_set('session.gc_maxlifetime', 2592000);
session_start();

// ==============================================================
// SISTEM AUTO-LOGIN (ANTI-LOGOUT) UNTUK APK
// ==============================================================
// 1. Cek apakah ada Cookie Permanen di HP
if (!isset($_SESSION['user_id']) && isset($_COOKIE['omni_uid']) && isset($_COOKIE['omni_role'])) {
    $_SESSION['user_id']  = $_COOKIE['omni_uid'];
    $_SESSION['username'] = $_COOKIE['omni_uname'];
    $_SESSION['name']     = $_COOKIE['omni_name'];
    $_SESSION['role']     = $_COOKIE['omni_role'];
}

// 2. Jika sudah login (via Session atau Cookie di atas), lempar ke dashboard
if (isset($_SESSION['user_id'])) {
    header("Location: dashboard.php");
    exit;
}
// ==============================================================

$host = 'localhost';
$user = 'majapahi_omnich';
$pass = 'A1b2c3d4.,@';
$db   = 'majapahi_omnich';
$conn = new mysqli($host, $user, $pass, $db);

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $conn->real_escape_string($_POST['username']);
    $password = MD5($_POST['password']); 

    $query = $conn->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
    
    if ($query->num_rows > 0) {
        $user_data = $query->fetch_assoc();
        $_SESSION['user_id']  = $user_data['id'];
        $_SESSION['username'] = $user_data['username'];
        $_SESSION['name']     = $user_data['name'];
        $_SESSION['role']     = $user_data['role'];
        
        // =======================================================
        // TAMBAHAN: Tanam Cookie Permanen (Aktif 1 Tahun / 365 Hari)
        // Ini memastikan APK di HP tidak logout saat RAM dibersihkan
        // =======================================================
        setcookie('omni_uid',   $user_data['id'],       time() + (86400 * 365), "/");
        setcookie('omni_uname', $user_data['username'], time() + (86400 * 365), "/");
        setcookie('omni_name',  $user_data['name'],     time() + (86400 * 365), "/");
        setcookie('omni_role',  $user_data['role'],     time() + (86400 * 365), "/");
        // =======================================================
        
        header("Location: dashboard.php");
        exit;
    } else {
        $error = "Username atau Password salah!";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login | A&S WebDeveloper System</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f0f2f5; height: 100vh; display: flex; align-items: center; justify-content: center; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
        .login-card { background: white; padding: 40px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); width: 100%; max-width: 400px; border: 1px solid #ddd; }
        .logo-area { text-align: center; margin-bottom: 25px; color: #008069; }
        .btn-login { background-color: #008069; color: white; width: 100%; padding: 12px; font-weight: bold; border-radius: 30px; border: none; transition: 0.3s; }
        .btn-login:hover { background-color: #006b58; }
        .brand-footer { margin-top: 30px; text-align: center; color: #999; font-size: 0.7rem; border-top: 1px solid #eee; padding-top: 15px; }
    </style>
</head>
<body>

<div class="login-card shadow-lg">
    <div class="logo-area">
        <svg viewBox="0 0 24 24" width="60" height="60" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12c0 1.84.5 3.56 1.36 5.09L2 22l4.91-1.36C8.44 21.5 10.16 22 12 22c5.52 0 10-4.48 10-10S17.52 2 12 2zm-1.85 14.53L7 13.38l1.41-1.41 1.74 1.74 4.09-4.09 1.41 1.41-5.5 5.5z"></path></svg>
        <h3 class="mt-2 fw-bold">A&S WA CS System</h3>
        <p class="text-muted small">Kelola chat pelanggan dengan mudah.</p>
    </div>

    <?php if($error): ?>
        <div class="alert alert-danger text-center small"><?= $error ?></div>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="mb-3">
            <label class="form-label small fw-bold">Username</label>
            <input type="text" name="username" class="form-control form-control-lg rounded-pill px-4" style="font-size: 0.9rem;" required autocomplete="off">
        </div>
        <div class="mb-4">
            <label class="form-label small fw-bold">Password</label>
            <input type="password" name="password" class="form-control form-control-lg rounded-pill px-4" style="font-size: 0.9rem;" required>
        </div>
        <button type="submit" class="btn btn-login shadow-sm">MASUK KE DASHBOARD</button>
    </form>

    <div class="brand-footer">
        <p class="mb-0 mt-3">&copy; <?= date("Y") ?> <strong>A&S Group</strong></p>
        <p class="mb-0">Powered by <strong>A&S WebDeveloper</strong></p>
    </div>
</div>

</body>
</html>