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

// Jika operator sudah dalam keadaan login, langsung alihkan ke dashboard
if(isset($_SESSION['status']) && $_SESSION['status'] == "login"){
    header("location:dashboard.php");
    exit;
}

$pesan_error = "";

// Memproses data ketika form disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = mysqli_real_escape_string($koneksi, $_POST['username']);
    $password = mysqli_real_escape_string($koneksi, $_POST['password']);

    // Query memeriksa kecocokan username dan password
    $query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username' AND password='$password'");
    $cek = mysqli_num_rows($query);

    if($cek > 0){
        $data = mysqli_fetch_assoc($query);
        $_SESSION['username'] = $data['username'];
        $_SESSION['nama_lengkap'] = $data['nama_lengkap'];
        $_SESSION['status'] = "login";
        
        header("location:dashboard.php");
        exit;
    } else {
        $pesan_error = "Username atau Password salah!";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Pemilah Telur Ayam Kampung</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { 
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%); 
            height: 100vh; 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            font-family: 'Segoe UI', sans-serif;
        }
        .login-card { 
            width: 100%; 
            max-width: 420px; 
            padding: 35px; 
            border-radius: 15px; 
            box-shadow: 0 10px 25px rgba(0,0,0,0.2); 
            background: #fff; 
        }
        .btn-primary { background-color: #1e3c72; border: none; }
        .btn-primary:hover { background-color: #2a5298; }
    </style>
</head>
<body>

<div class="login-card">
    <h4 class="text-center fw-bold text-dark mb-1">Login</h4>
    <p class="text-muted text-center small mb-4">Rancang Bangun Pemilah Telur Ayam Kampung Menggunakan Image Processing</p>
    
    <?php if(!empty($pesan_error)): ?>
        <div class="alert alert-danger text-center py-2 small" role="alert">
            <?php echo $pesan_error; ?>
        </div>
    <?php endif; ?>
    
    <?php if(isset($_GET['pesan']) && $_GET['pesan'] == "belum_login"): ?>
        <div class="alert alert-warning text-center py-2 small" role="alert">
            Silakan login terlebih dahulu!
        </div>
    <?php endif; ?>
    
    <form action="" method="POST">
        <div class="mb-3">
            <label class="form-label fw-semibold text-secondary small">Username</label>
            <input type="text" name="username" class="form-control text-center" placeholder="Masukkan username" required autocomplete="off">
        </div>
        <div class="mb-3">
            <label class="form-label fw-semibold text-secondary small">Password</label>
            <input type="password" name="password" class="form-control text-center" placeholder="Masukkan password" required>
        </div>
        <button type="submit" class="btn btn-primary w-100 mt-3 py-2 fw-bold">Masuk ke Dashboard</button>
    </form>
</div>

</body>
</html>