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

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

    // Cek di tabel admin
    $queryAdmin = "SELECT * FROM users WHERE username = '$username'";
    $resultAdmin = $conn->query($queryAdmin);

    if ($resultAdmin->num_rows == 1) {
        $admin = $resultAdmin->fetch_assoc();

        // Verifikasi password untuk admin
        if (password_verify($password, $admin['password'])) {
            $_SESSION['username'] = $admin['username'];
            $_SESSION['role'] = $admin['role']; // Menandai role
            $_SESSION['nama'] = $admin['name'];
            $_SESSION['user_id'] = $admin['id'];

            header("Location: index.php");
            exit();
        } else {
            // Password salah
            $_SESSION['error'] = "Password salah!";
            header("Location: login.php");
            exit();
        }
    } else {
        // username salah
        $_SESSION['error'] = "Username tidak ditemukan!";
        header("Location: login.php");
        exit();
    }
}
