<?php
session_start();
require_once '../config.php';

// Jika sudah login
if(isset($_SESSION['user_id'])){
    header("Location: index.php");
    exit;
}

$error = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = md5(trim($_POST['password']));

    $stmt = $pdo->prepare("SELECT * FROM tbl_user WHERE username = ? AND password = ?");
    $stmt->execute([$username, $password]);
    $user = $stmt->fetch();

    if ($user) {
        $_SESSION['user_id'] = $user->id;
        $_SESSION['nama_lengkap'] = $user->nama_lengkap;
        $_SESSION['username'] = $user->username;
        $_SESSION['level'] = $user->level;
        header("Location: index.php");
        exit;
    } else {
        $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">
    <title>Login Petugas | PengajuanV2</title>
    <link rel="icon" type="image/png" href="<?= base_url('logo/logo.png?v='.time()) ?>">
    <link rel="shortcut icon" type="image/png" href="<?= base_url('logo/logo.png?v='.time()) ?>">
    
    <!-- Google Fonts: Inter -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <!-- FontAwesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Bootstrap 5 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
    
    <style>
        body {
            font-family: 'Inter', sans-serif;
            /* Background gradient yang sangat modern dan eye-catching */
            background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            position: relative;
            overflow: hidden;
        }
        
        /* Elemen dekorasi background melayang */
        body::before {
            content: '';
            position: absolute;
            width: 400px;
            height: 400px;
            background: rgba(255, 255, 255, 0.15);
            border-radius: 50%;
            top: -100px;
            left: -100px;
            z-index: 0;
            animation: float 6s ease-in-out infinite;
        }
        body::after {
            content: '';
            position: absolute;
            width: 300px;
            height: 300px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            bottom: -50px;
            right: -50px;
            z-index: 0;
            animation: float 8s ease-in-out infinite reverse;
        }
        
        @keyframes float {
            0% { transform: translateY(0px) rotate(0deg); }
            50% { transform: translateY(20px) rotate(10deg); }
            100% { transform: translateY(0px) rotate(0deg); }
        }

        .login-wrapper {
            width: 100%;
            max-width: 450px;
            padding: 20px;
            z-index: 10; /* Berada di atas dekorasi */
        }
        
        .card-login {
            border: none;
            border-radius: 20px;
            /* Efek Glassmorphism (Kaca) */
            background: rgba(255, 255, 255, 0.85);
            backdrop-filter: blur(15px);
            -webkit-backdrop-filter: blur(15px);
            box-shadow: 0 20px 50px rgba(0,0,0,0.15);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.5);
            transition: transform 0.3s ease;
        }
        
        .card-login:hover {
            transform: translateY(-5px);
        }

        .card-login .card-body {
            padding: 40px 35px;
        }
        
        .logo-container {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo-container img {
            max-height: 100px;
            margin-bottom: 15px;
            filter: drop-shadow(0 8px 12px rgba(0,0,0,0.15));
            transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }
        
        .logo-container img:hover {
            transform: scale(1.1) rotate(-3deg);
        }
        
        .login-title {
            font-weight: 800;
            color: #1a237e;
            font-size: 24px;
            margin-bottom: 5px;
            letter-spacing: -0.5px;
        }
        
        .login-subtitle {
            color: #5c6bc0;
            font-size: 14px;
            font-weight: 500;
            margin-bottom: 25px;
        }
        
        .form-control {
            border-radius: 12px;
            padding: 14px 15px 14px 45px;
            border: 2px solid transparent;
            background-color: #f0f2f5;
            font-size: 15px;
            font-weight: 500;
            color: #333;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            box-shadow: 0 0 0 4px rgba(92, 107, 192, 0.15);
            border-color: #5c6bc0;
            background-color: #fff;
        }
        
        .form-control::placeholder {
            color: #9aa0a6;
            font-weight: 400;
        }
        
        .input-icon {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            left: 18px;
            color: #7986cb;
            z-index: 10;
            font-size: 18px;
            transition: color 0.3s;
        }
        
        .form-group {
            position: relative;
            margin-bottom: 22px;
        }
        
        .form-control:focus + .input-icon,
        .form-group:focus-within .input-icon {
            color: #1a237e;
        }
        
        .btn-login {
            background: linear-gradient(to right, #3f51b5, #1a237e);
            border: none;
            border-radius: 12px;
            padding: 14px;
            font-weight: 700;
            letter-spacing: 1px;
            color: white;
            width: 100%;
            margin-top: 10px;
            transition: all 0.3s;
            box-shadow: 0 8px 20px rgba(26, 35, 126, 0.3);
            text-transform: uppercase;
        }
        
        .btn-login:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 25px rgba(26, 35, 126, 0.4);
            background: linear-gradient(to right, #3949ab, #151b60);
        }
        
        .btn-login:active {
            transform: translateY(1px);
        }
        
        .back-link {
            display: inline-block;
            margin-top: 30px;
            color: #5c6bc0;
            text-decoration: none;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s;
        }
        
        .back-link:hover {
            color: #1a237e;
            letter-spacing: 0.5px;
        }
    </style>
</head>
<body>

<div class="login-wrapper">
    <div class="card card-login">
        <div class="card-body text-center">
            
            <div class="logo-container">
                <?php if(file_exists('../logo/logo.png')): ?>
                    <img src="<?= base_url('logo/logo.png?v='.time()) ?>" alt="Logo Instansi">
                <?php else: ?>
                    <div style="width: 90px; height: 90px; background: linear-gradient(135deg, #3f51b5, #1a237e); border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; margin-bottom: 20px; color: white; box-shadow: 0 10px 20px rgba(0,0,0,0.2);">
                        <i class="fas fa-shield-alt fa-3x"></i>
                    </div>
                <?php endif; ?>
                <h2 class="login-title">Portal Petugas</h2>
                <p class="login-subtitle">Masuk untuk mengelola data permohonan</p>
            </div>
            
            <?php if($error): ?>
                <div class="alert alert-danger" style="border-radius: 10px; font-size: 14px; padding: 12px; font-weight: 500; border: none; background-color: #ffebee; color: #c62828;">
                    <i class="fas fa-exclamation-triangle me-2"></i> <?= $error ?>
                </div>
            <?php endif; ?>

            <form action="login.php" method="post" class="text-start mt-4">
                <div class="form-group">
                    <i class="fas fa-user input-icon"></i>
                    <input type="text" name="username" class="form-control" placeholder="Username" required autocomplete="off">
                </div>
                <div class="form-group">
                    <i class="fas fa-lock input-icon"></i>
                    <input type="password" name="password" class="form-control" placeholder="Password" required>
                </div>
                
                <button type="submit" class="btn btn-login">
                    <i class="fas fa-sign-in-alt me-2"></i> MASUK
                </button>
            </form>
            
            <a href="../index.php" class="back-link">
                <i class="fas fa-arrow-left me-1"></i> Kembali ke Halaman Utama
            </a>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
