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

if (isset($_POST['login'])) {
    $username = mysqli_real_escape_string($koneksi, $_POST['username']);
    $password = $_POST['password'];

    $query = mysqli_query($koneksi, "SELECT * FROM users WHERE username = '$username'");
    
    if (mysqli_num_rows($query) === 1) {
        $data = mysqli_fetch_assoc($query);
        
        // Pengecekan password teks biasa
        if ($password === $data['password']) {
            // Set session
            $_SESSION['username'] = $data['username'];
            $_SESSION['status'] = "login";
            
            // Pindah ke halaman dashboard
            header("Location: dashboard.php");
            exit(); 
        }
    }
    
    // Jika salah password / username tidak ada
    header("Location: login.php?pesan=gagal");
    exit();
}
?>