<?php
// Pastikan session dimulai di paling atas
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once 'config/database.php';

// Redirect jika sudah login
if (isset($_SESSION['login'])) {
    // Redirect ke halaman yang diminta sebelumnya atau dashboard
    $redirect_url = isset($_SESSION['redirect_url']) ? $_SESSION['redirect_url'] : 'dashboard.php';
    unset($_SESSION['redirect_url']);
    header("Location: " . $redirect_url);
    exit();
}

$error = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

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

    if (mysqli_num_rows($result) == 1) {
        $user = mysqli_fetch_assoc($result);
        if ($password == $user['password']) { // Untuk demo saja, seharusnya menggunakan password_hash()
            $_SESSION['login'] = true;
            $_SESSION['user_id'] = $user['id_users'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['nama'] = $user['nama'];
            $_SESSION['LAST_ACTIVITY'] = time();
            
            // Set flag untuk menampilkan SweetAlert setelah redirect
            $_SESSION['show_login_alert'] = true;
            $_SESSION['login_alert_data'] = [
                'icon' => 'success',
                'title' => 'Login Berhasil',
                'text' => 'Selamat datang ' . $user['nama'],
                'timer' => 2000
            ];
            
            // Redirect ke halaman yang diminta sebelumnya atau dashboard
            $redirect_url = isset($_SESSION['redirect_url']) ? $_SESSION['redirect_url'] : 'dashboard.php';
            unset($_SESSION['redirect_url']);
            header("Location: " . $redirect_url);
            exit();
        }
    }
    
    $error = "Username atau password salah!";
}

// Tampilkan pesan timeout jika ada
$timeout_message = "";
if (isset($_GET['timeout']) && $_GET['timeout'] == 1) {
    $timeout_message = "Session Anda telah habis. Silakan login kembali.";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Buku Induk Sekolah SMAN 1 BANTARAN</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        :root {
            --primary-color: #3b5998;
            --secondary-color: #8b9dc3;
            --accent-color: #dfe3ee;
            --dark-color: #343a40;
            --light-color: #f8f9fa;
        }
        
        body {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .login-container {
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            overflow: hidden;
            width: 100%;
            max-width: 400px;
        }
        
        .login-header {
            background: var(--primary-color);
            color: white;
            padding: 25px 20px;
            text-align: center;
        }
        
        .login-header h3 {
            margin: 0;
            font-weight: 600;
        }
        
        .login-header p {
            margin: 5px 0 0;
            opacity: 0.9;
        }
        
        .login-body {
            padding: 25px;
        }
        
        .form-control {
            border-radius: 8px;
            padding: 12px 15px;
            margin-bottom: 15px;
            border: 2px solid #e1e5eb;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(59, 89, 152, 0.25);
        }
        
        .input-group-text {
            background-color: white;
            border: 2px solid #e1e5eb;
            border-right: none;
            border-radius: 8px 0 0 8px;
        }
        
        .input-group .form-control {
            border-left: none;
            border-radius: 0 8px 8px 0;
        }
        
        .btn-login {
            background-color: var(--primary-color);
            border: none;
            color: white;
            padding: 12px;
            border-radius: 8px;
            font-weight: 600;
            width: 100%;
            transition: all 0.3s;
        }
        
        .btn-login:hover {
            background-color: #2d4373;
            transform: translateY(-2px);
        }
        
        .demo-info {
            background-color: var(--accent-color);
            border-radius: 8px;
            padding: 12px;
            margin-top: 20px;
            text-align: center;
            font-size: 0.9rem;
        }
        
        .error-message {
            background-color: #ffe6e6;
            color: #d9534f;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 15px;
            text-align: center;
            border-left: 4px solid #d9534f;
        }
        
        .school-logo {
            width: 80px;
            height: 80px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .school-logo i {
            font-size: 40px;
            color: var(--primary-color);
        }
        
        .footer {
            text-align: center;
            margin-top: 20px;
            color: white;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <div class="school-logo">
                <i class="fas fa-book"></i>
            </div>
            <h3>BUKU INDUK SEKOLAH</h3>
            <p>SMAN 1 BANTARAN (SMANSAB)</p>
        </div>
        
        <div class="login-body">
            <?php if (!empty($error)): ?>
                <div class="error-message">
                    <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" id="loginForm">
                <div class="input-group mb-3">
                    <span class="input-group-text"><i class="fas fa-user"></i></span>
                    <input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
                </div>
                
                <div class="input-group mb-3">
                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                    <input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
                </div>
                
                <button type="submit" class="btn btn-login">
                    <i class="fas fa-sign-in-alt"></i> Login
                </button>
            </form>
            
           <div class="demo-info">
                <i class="fas fa-info-circle"></i> Kembali ke <a href= index.php> Halaman Depan </a>
            </div> 
        </div>
    </div>
    
  <!--  <div class="footer">
        &copy; <?php echo date('Y'); ?> SMAN 1 BANTARAN | Sistem Buku Induk
    </div> -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('loginForm').addEventListener('submit', function() {
            const btn = this.querySelector('button[type="submit"]');
            btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Memproses...';
            btn.disabled = true;
        });
    </script>
</body>
</html>