<?php
session_start();
include '../includes/config.php'; 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    try {
        $stmt = $pdo->prepare("SELECT * FROM admin WHERE username = ?");
        $stmt->execute([$username]);
        $admin = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($admin) {
            // LANGSUNG COCOKKAN TEKS BIASA (Pasti Berhasil)
            if ($password === $admin['password']) {
                $_SESSION['admin_id']        = $admin['id'];
                $_SESSION['admin_username']  = $admin['username'];
                $_SESSION['is_logged_in']    = true;

                header("Location: dashboard.php");
                exit();
            } else {
                die("Gagal: Kata sandi yang Anda masukkan salah.");
            }
        } else {
            die("Gagal: Username tidak ditemukan.");
        }

    } catch (PDOException $e) {
        die("Error Database: " . $e->getMessage());
    }
}