<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();
if(isset($_SESSION['user_id'])){
    // CEK ROLE DULU SEBELUM REDIRECT
    if($_SESSION['role'] == 'bendahara'){
        header("Location: dashboard_bendahara.php");
    } else {
        header("Location: dashboard.php");
    }
    exit;
}

include 'config/database.php';
$error = '';

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $db = new Database();
    $conn = $db->getConnection();
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $sql = "SELECT u.*, g.nama_gereja FROM users u 
            LEFT JOIN gereja g ON u.gereja_id = g.id 
            WHERE u.username = ? LIMIT 1"; // HAPUS u.status = 'Aktif' kalo kolomnya ga ada
    $stmt = $conn->prepare($sql);
    $stmt->execute([$username]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if($user && password_verify($password, $user['password'])){
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['nama'] = $user['nama'];
        $_SESSION['role'] = $user['role'];
        $_SESSION['foto'] = $user['foto'] ?? 'default.png';
        $_SESSION['gereja_id'] = $user['gereja_id'];
        $_SESSION['nama_gereja'] = $user['nama_gereja'];
        $_SESSION['created'] = time();
        
        // REDIRECT SESUAI ROLE
        if($user['role'] == 'bendahara'){
            header("Location: dashboard_bendahara.php");
        } else {
            header("Location: dashboard.php");
        }
        exit;
    } else {
        $error = 'Username atau Password salah!';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - SIMJEBPW Keerom</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
        }
        .login-box {
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 50px rgba(0,0,0,0.3);
            width: 100%;
            max-width: 400px;
            padding: 45px 40px;
        }
        .logo-wrap { text-align: center; margin-bottom: 25px; }
        .logo-wrap img { width: 100px; height: 100px; object-fit: contain; }
        .login-title { text-align: center; font-size: 26px; font-weight: 600; color: #2a5298; margin-bottom: 5px; }
        .login-subtitle { text-align: center; color: #6c757d; font-size: 13px; margin-bottom: 35px; }
        .form-group { margin-bottom: 20px; position: relative; }
        .form-group i { position: absolute; left: 18px; top: 14px; color: #adb5bd; }
        .form-control { border-radius: 10px; padding: 12px 20px 12px 45px; border: 1px solid #e0e6ed; font-size: 14px; height: auto; background: #f8f9fa; }
        .form-control:focus { border-color: #2a5298; background: white; box-shadow: 0 0 0 0.2rem rgba(42,82,152,.15); }
        .btn-login { background: linear-gradient(135deg, #2a5298 0%, #1e3c72 100%); border: none; border-radius: 10px; padding: 13px; font-weight: 600; width: 100%; margin-top: 10px; color: white; font-size: 15px; }
        .btn-login:hover { opacity: 0.9; }
        .alert { border-radius: 10px; font-size: 13px; padding: 10px 15px; }
        .footer-text { text-align: center; margin-top: 30px; font-size: 12px; color: #adb5bd; }
        
    </style>
</head>
<body>
    <div class="login-box">
        <div class="logo-wrap">
            <img src="/simjebpw/assets/img/logo_gbi.png" onerror="this.onerror=null; this.src='https://upload.wikimedia.org/wikipedia/id/thumb/3/35/Logo_GBI.png/200px-Logo_GBI.png'">
        </div>
        <h1 class="login-title">SIAMAN</h1>
        <p class="login-subtitle">Sistem Informasi Administrasi Manajemen GBI Kabupaten Keerom</p>
        
        <?php if($error): ?>
        <div class="alert alert-danger text-center">
            <i class="fas fa-exclamation-circle"></i> <?= $error ?>
        </div>
        <?php endif; ?>
        
        <form method="POST" action="">
            <div class="form-group">
                <i class="fas fa-user"></i>
                <input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
            </div>
            <div class="form-group">
                <i class="fas fa-lock"></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 mr-2"></i>LOGIN
            </button>
        </form>
        
        <div class="footer-text">© 2026 GBI DPW Keerom</div>
    </div>
</body>
</html>