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

if ($_POST) {
    $user = $_POST['username'];
    $pass = sha1($_POST['password']);
    $query = mysqli_query($conn, "SELECT * FROM petugas WHERE username='$user' AND password='$pass'");
    if (mysqli_num_rows($query) > 0) {
        $data = mysqli_fetch_assoc($query);
        $_SESSION['login'] = true;
        $_SESSION['username'] = $user;
        $_SESSION['role'] = $data['role'];
        header("Location: ../index.php");
        exit;
    } else {
        $error = "Username atau password salah.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login Petugas</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
     <style>
        body {
            background: linear-gradient(to right, #5cb8ff, #2d9fff);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-card {
            width: 100%;
            max-width: 400px;
            padding: 2rem;
            border-radius: 15px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.15);
        }
        .login-title {
            font-weight: bold;
            text-align: center;
            margin-bottom: 1.5rem;
        }
    </style>
</head>
<body>
    <div class="login-card">
        <h4 class="login-title text-primary">Login Petugas</h4>
        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?= $error ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="mb-3">
                <label class="form-label">Username</label>
                <input type="text" name="username" class="form-control" required autofocus>
            </div>
            <div class="mb-3">
                <label class="form-label">Password</label>
                <input type="password" name="password" class="form-control" required>
            </div>
            <button class="btn btn-primary w-100">Login</button>
    </form>
</div>
</body>
</html>