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

$email = trim($_POST['email']);
$password = $_POST['password'];

// Debug: pastikan koneksi berhasil
if (!$conn) {
    die("Koneksi gagal: " . mysqli_connect_error());
}

// Jalankan query
$query = mysqli_query($conn, "SELECT * FROM admin WHERE email = '$email'");

// Debug: cek apakah query berhasil
if (!$query) {
    die("Query error: " . mysqli_error($conn));
}

// Ambil data admin
$data = mysqli_fetch_assoc($query);

if ($data && password_verify($password, $data['password'])) {
    $_SESSION['admin'] = [
        'id' => $data['id'],
        'email' => $data['email'],
        'nama_lengkap' => $data['nama_lengkap']
    ];
    header("Location: dashboard.php");
    exit;
} else {
    $_SESSION['error'] = "Email atau password salah!";
    header("Location: login.php");
    exit;
}
