<?php
include 'koneksi.php';

$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = md5($_POST['password']);

$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) == 1) {
    $user = mysqli_fetch_assoc($result);
    $_SESSION['user'] = $user;
    $_SESSION['role'] = $user['role'];
    $_SESSION['unit'] = $user['unit'];
    $_SESSION['user_id'] = $user['id'];
    $_SESSION['full_name'] = $user['full_name'];
    
    // Log aktivitas login
    $log_query = "INSERT INTO activity_logs (user_id, activity, timestamp) VALUES ('{$user['id']}', 'Login', NOW())";
    mysqli_query($conn, $log_query);
    
    header("Location: dashboard.php");
} else {
    header("Location: index.php?error=1");
}
?>