<?php
declare(strict_types=1);

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require_once __DIR__ . '/config/master_auth.php';

if (!function_exists('e')) {
    function e($value): string
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

if (!defined('APP_NAME')) {
    define('APP_NAME', 'Sistem Keperawatan');
}

if (!defined('LOGO_URL')) {
    define('LOGO_URL', 'https://balimedhospital.co.id/wp-content/uploads/elementor/thumbs/logo-dark-o9cm6tnx14hcrnest1hge1pwnqj0yaumij7qohadlm.png');
}

if (!empty($_SESSION['user']['logged_in'])) {
    header('Location: index.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $login    = trim((string)($_POST['nik'] ?? ''));
    $password = (string)($_POST['password'] ?? '');

    if ($login === '' || $password === '') {
        $error = 'ID Karyawan / Username dan password wajib diisi.';
    } else {
        // app_code harus sama dengan yang ada di tabel apps master HR
        $result = master_login_app($login, $password, 'perawat');

        if (!empty($result['success'])) {
            header('Location: index.php');
            exit;
        }

        $error = (string)($result['message'] ?? 'Login gagal');
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?= e(APP_NAME) ?></title>

    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <style>
        body {
            font-family: 'Poppins', sans-serif;
        }

        .login-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    </style>
</head>
<body class="login-bg min-h-screen flex items-center justify-center p-4">
    <div class="max-w-md w-full">
        <div class="text-center mb-8">
            <div class="inline-flex items-center justify-center bg-white rounded-2xl shadow-lg px-5 py-3 mb-4">
                <img
                    src="<?= e(LOGO_URL) ?>"
                    alt="BaliMed"
                    class="h-14 w-auto object-contain"
                >
            </div>
            <h2 class="text-3xl font-bold text-white">Sistem Keperawatan</h2>
            <p class="text-white/80 text-sm mt-1">RS BaliMed Denpasar</p>
        </div>

        <div class="bg-white/10 backdrop-blur-lg rounded-2xl shadow-2xl overflow-hidden border border-white/20">
            <div class="p-8">
                <?php if ($error !== ''): ?>
                    <div class="mb-6 bg-red-500/20 border-l-4 border-red-500 p-4 rounded backdrop-blur-sm">
                        <p class="text-white text-sm"><?= e($error) ?></p>
                    </div>
                <?php endif; ?>

                <form method="POST" class="space-y-6">
                    <div>
                        <label class="block text-white/90 text-sm font-medium mb-2">
                            <i class="fas fa-id-card mr-2"></i>
                            ID Karyawan / Username
                        </label>
                        <input
                            type="text"
                            name="nik"
                            required
                            class="w-full px-4 py-3 bg-white/10 border border-white/30 rounded-lg text-white placeholder-white/50 focus:outline-none focus:border-white/60 focus:bg-white/20 transition"
                            placeholder="Masukkan ID Karyawan / Username"
                            value="<?= e($_POST['nik'] ?? '') ?>"
                        >
                    </div>

                    <div>
                        <label class="block text-white/90 text-sm font-medium mb-2">
                            <i class="fas fa-lock mr-2"></i>
                            Password
                        </label>
                        <input
                            type="password"
                            name="password"
                            required
                            class="w-full px-4 py-3 bg-white/10 border border-white/30 rounded-lg text-white placeholder-white/50 focus:outline-none focus:border-white/60 focus:bg-white/20 transition"
                            placeholder="••••••••"
                        >
                    </div>

                    <button
                        type="submit"
                        class="w-full bg-gradient-to-r from-blue-500 to-purple-600 text-white font-semibold py-3 px-4 rounded-lg hover:from-blue-600 hover:to-purple-700 transform hover:scale-[1.02] transition-all duration-200 shadow-lg flex items-center justify-center"
                    >
                        <i class="fas fa-sign-in-alt mr-2"></i>
                        Masuk ke Sistem
                    </button>
                </form>

                <div class="mt-6 text-center text-white/50 text-xs">
                    <p>Gunakan username master HR atau ID karyawan yang terdaftar</p>
                </div>
            </div>
        </div>

        <div class="text-center mt-8 text-white/60 text-sm">
            &copy; <?= date('Y') ?> RS BaliMed. All rights reserved.
        </div>
    </div>
</body>
</html>
