<?php
$appSetting = function_exists('app_setting') ? app_setting() : [];
$appName    = trim((string) ($appSetting['nama_aplikasi'] ?? 'NusaPOS')) ?: 'NusaPOS';
$appTitle   = trim((string) ($appSetting['judul_aplikasi'] ?? '')) ?: $appName;
$appTagline = trim((string) ($appSetting['tagline'] ?? '')) ?: 'Point of Sale dan service toko.';
$appLogo    = $appSetting['logo'] ?? null;
$appFavicon = $appSetting['favicon'] ?? null;
$logoUrl    = ! empty($appLogo) ? base_url('uploads/settings/' . $appLogo) : null;
$faviconUrl = ! empty($appFavicon) ? base_url('uploads/settings/' . $appFavicon) : base_url('favicon.ico');
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login - <?= esc($appTitle) ?></title>
    <link rel="icon" href="<?= esc($faviconUrl, 'attr') ?>">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <style>
        :root {
            --admin-bg: #f7f9fc;
            --admin-sidebar: #101828;
            --admin-text: #101828;
            --admin-muted: #667085;
            --admin-border: #e4e7ec;
            --admin-primary: #2563eb;
            --admin-card: #ffffff;
        }

        * {
            box-sizing: border-box;
        }

        body {
            min-height: 100vh;
            margin: 0;
            background: var(--admin-bg);
            color: var(--admin-text);
            font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
        }

        .login-page {
            min-height: 100vh;
            display: grid;
            grid-template-columns: minmax(0, 1.05fr) minmax(420px, .95fr);
            background: var(--admin-card);
        }

        .login-image {
            position: relative;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            overflow: hidden;
            padding: 34px;
            background: var(--admin-sidebar);
            color: #ffffff;
        }

        .brand-lockup {
            display: flex;
            align-items: center;
            gap: 12px;
            min-width: 0;
        }

        .brand-logo,
        .brand-mark {
            width: 46px;
            height: 46px;
            flex: 0 0 46px;
            border-radius: 8px;
            background: #ffffff;
        }

        .brand-logo {
            object-fit: contain;
            padding: 5px;
        }

        .brand-mark {
            display: grid;
            place-items: center;
            color: var(--admin-sidebar);
            font-size: 21px;
            font-weight: 800;
        }

        .brand-name {
            font-size: 20px;
            font-weight: 800;
            line-height: 1.15;
        }

        .brand-tagline {
            margin-top: 2px;
            color: rgba(255, 255, 255, .76);
            font-size: 13px;
        }

        .login-image-content {
            max-width: 560px;
        }

        .login-image-title {
            margin: 0;
            max-width: 540px;
            font-size: 42px;
            line-height: 1.12;
            font-weight: 850;
            letter-spacing: 0;
        }

        .login-image-text {
            max-width: 480px;
            margin: 14px 0 0;
            color: rgba(255, 255, 255, .78);
            font-size: 15px;
            line-height: 1.7;
        }

        .login-summary {
            width: 100%;
            max-width: 520px;
            margin-top: 26px;
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 10px;
        }

        .login-summary-item {
            min-height: 88px;
            padding: 14px;
            border: 1px solid rgba(255, 255, 255, .16);
            border-radius: 8px;
            background: rgba(255, 255, 255, .07);
        }

        .login-summary-icon {
            width: 34px;
            height: 34px;
            display: grid;
            place-items: center;
            margin-bottom: 10px;
            border-radius: 8px;
            background: #ffffff;
            color: var(--admin-sidebar);
        }

        .login-summary-label {
            color: rgba(255, 255, 255, .72);
            font-size: 12px;
        }

        .login-summary-title {
            margin-top: 2px;
            color: #ffffff;
            font-size: 13px;
            font-weight: 750;
        }

        .login-image-footer {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .feature-pill {
            display: inline-flex;
            align-items: center;
            gap: 7px;
            padding: 8px 11px;
            border: 1px solid rgba(255, 255, 255, .20);
            border-radius: 8px;
            background: rgba(255, 255, 255, .10);
            color: rgba(255, 255, 255, .86);
            font-size: 13px;
            font-weight: 650;
        }

        .login-form-side {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 32px;
        }

        .login-card {
            width: 100%;
            max-width: 430px;
        }

        .mobile-brand {
            display: none;
            margin-bottom: 26px;
        }

        .mobile-brand .brand-name {
            color: var(--admin-text);
        }

        .mobile-brand .brand-tagline {
            color: var(--admin-muted);
        }

        .mobile-brand .brand-mark {
            background: var(--admin-sidebar);
            color: #ffffff;
        }

        .login-heading {
            margin-bottom: 22px;
        }

        .login-heading h1 {
            margin: 0 0 8px;
            font-size: 30px;
            line-height: 1.2;
            font-weight: 850;
            letter-spacing: 0;
        }

        .login-heading p {
            margin: 0;
            color: var(--admin-muted);
            font-size: 14px;
            line-height: 1.6;
        }

        .alert {
            border-radius: 8px;
            font-size: 14px;
        }

        .form-label {
            color: #344054;
            font-size: 14px;
        }

        .input-group-text {
            width: 44px;
            justify-content: center;
            border-color: var(--admin-border);
            border-radius: 8px 0 0 8px;
            background: #ffffff;
            color: #667085;
        }

        .form-control {
            min-height: 44px;
            border-color: var(--admin-border);
            border-radius: 0 8px 8px 0;
            padding: .6rem .82rem;
            color: var(--admin-text);
            font-size: 14px;
        }

        .form-control:focus {
            border-color: var(--admin-primary);
            box-shadow: 0 0 0 .2rem rgba(37, 99, 235, .12);
        }

        .password-input {
            border-radius: 0;
        }

        .password-toggle {
            width: 44px;
            border-color: var(--admin-border);
            border-left: 0;
            border-radius: 0 8px 8px 0;
            background: #ffffff;
            color: #667085;
        }

        .password-toggle:hover,
        .password-toggle:focus {
            border-color: var(--admin-border);
            background: #f8fafc;
            color: var(--admin-text);
        }

        .btn-login {
            min-height: 44px;
            border-radius: 8px;
            background: var(--admin-sidebar);
            border-color: var(--admin-sidebar);
            font-weight: 750;
        }

        .btn-login:hover,
        .btn-login:focus {
            background: #1d2939;
            border-color: #1d2939;
        }

        .login-help {
            margin-top: 18px;
            padding-top: 18px;
            border-top: 1px solid var(--admin-border);
            color: var(--admin-muted);
            font-size: 13px;
            line-height: 1.55;
        }

        @media (max-width: 991.98px) {
            .login-page {
                display: block;
                min-height: 100vh;
            }

            .login-image {
                display: none;
            }

            .login-form-side {
                min-height: 100vh;
                padding: 28px 20px;
            }

            .mobile-brand {
                display: flex;
                align-items: center;
                gap: 12px;
            }
        }

        @media (max-width: 575.98px) {
            .login-form-side {
                align-items: flex-start;
                padding: 22px 16px;
            }

            .login-heading h1 {
                font-size: 26px;
            }
        }
    </style>
</head>
<body>
    <main class="login-page">
        <section class="login-image" aria-label="<?= esc($appName, 'attr') ?>">
            <div class="brand-lockup">
                <?php if ($logoUrl) : ?>
                    <img src="<?= esc($logoUrl, 'attr') ?>" alt="Logo <?= esc($appName) ?>" class="brand-logo">
                <?php else : ?>
                    <div class="brand-mark"><?= esc(strtoupper(substr($appName, 0, 1))) ?></div>
                <?php endif; ?>
                <div class="min-w-0">
                    <div class="brand-name"><?= esc($appName) ?></div>
                    <div class="brand-tagline"><?= esc($appTagline) ?></div>
                </div>
            </div>

            <div class="login-image-content">
                <h1 class="login-image-title">Kelola penjualan dan service dalam satu sistem.</h1>
                <p class="login-image-text">
                    Proses transaksi kasir, pantau stok part/barang, dan kelola pekerjaan service pelanggan dari satu panel.
                </p>

                <div class="login-summary">
                    <div class="login-summary-item">
                        <div class="login-summary-icon"><i class="bi bi-cart-check"></i></div>
                        <div class="login-summary-label">POS</div>
                        <div class="login-summary-title">Transaksi kasir</div>
                    </div>
                    <div class="login-summary-item">
                        <div class="login-summary-icon"><i class="bi bi-wrench-adjustable"></i></div>
                        <div class="login-summary-label">Service</div>
                        <div class="login-summary-title">Pekerjaan pelanggan</div>
                    </div>
                    <div class="login-summary-item">
                        <div class="login-summary-icon"><i class="bi bi-box-seam"></i></div>
                        <div class="login-summary-label">Stok</div>
                        <div class="login-summary-title">Part dan barang</div>
                    </div>
                </div>
            </div>

            <div class="login-image-footer">
                <span class="feature-pill"><i class="bi bi-cart-check"></i>Penjualan</span>
                <span class="feature-pill"><i class="bi bi-wrench-adjustable"></i>Service</span>
                <span class="feature-pill"><i class="bi bi-box-seam"></i>Stok Barang</span>
            </div>
        </section>

        <section class="login-form-side">
            <div class="login-card">
                <div class="mobile-brand">
                    <?php if ($logoUrl) : ?>
                        <img src="<?= esc($logoUrl, 'attr') ?>" alt="Logo <?= esc($appName) ?>" class="brand-logo">
                    <?php else : ?>
                        <div class="brand-mark"><?= esc(strtoupper(substr($appName, 0, 1))) ?></div>
                    <?php endif; ?>
                    <div class="min-w-0">
                        <div class="brand-name"><?= esc($appName) ?></div>
                        <div class="brand-tagline"><?= esc($appTagline) ?></div>
                    </div>
                </div>

                <div class="login-heading">
                    <h1>Masuk</h1>
                    <p>Gunakan akun administrator atau kasir yang sudah aktif.</p>
                </div>

                <?php if (session()->getFlashdata('error')) : ?>
                    <div class="alert alert-danger" role="alert">
                        <?= esc(session()->getFlashdata('error')) ?>
                    </div>
                <?php endif; ?>

                <?php if (session()->getFlashdata('success')) : ?>
                    <div class="alert alert-success" role="alert">
                        <?= esc(session()->getFlashdata('success')) ?>
                    </div>
                <?php endif; ?>

                <form action="<?= site_url('login') ?>" method="post">
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label for="email" class="form-label fw-semibold">Email</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-envelope"></i></span>
                            <input
                                type="email"
                                class="form-control"
                                id="email"
                                name="email"
                                value="<?= esc((string) old('email'), 'attr') ?>"
                                placeholder="nama@email.com"
                                autocomplete="email"
                                required
                            >
                        </div>
                    </div>

                    <div class="mb-4">
                        <label for="password" class="form-label fw-semibold">Password</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-lock"></i></span>
                            <input
                                type="password"
                                class="form-control password-input"
                                id="password"
                                name="password"
                                placeholder="Masukkan password"
                                autocomplete="current-password"
                                required
                            >
                            <button class="btn password-toggle" type="button" id="passwordToggle" aria-label="Tampilkan password" title="Tampilkan password">
                                <i class="bi bi-eye"></i>
                            </button>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary btn-login w-100">
                        <i class="bi bi-box-arrow-in-right me-1"></i>Masuk ke Dashboard
                    </button>
                </form>

                <div class="login-help">
                    <i class="bi bi-shield-check me-1"></i>
                    Akses hanya untuk akun yang terdaftar dan aktif di sistem.
                </div>
            </div>
        </section>
    </main>

    <script>
        const passwordInput = document.getElementById('password');
        const passwordToggle = document.getElementById('passwordToggle');

        if (passwordInput && passwordToggle) {
            passwordToggle.addEventListener('click', function () {
                const isHidden = passwordInput.type === 'password';
                const icon = passwordToggle.querySelector('i');

                passwordInput.type = isHidden ? 'text' : 'password';
                passwordToggle.setAttribute('aria-label', isHidden ? 'Sembunyikan password' : 'Tampilkan password');
                passwordToggle.setAttribute('title', isHidden ? 'Sembunyikan password' : 'Tampilkan password');

                if (icon) {
                    icon.className = isHidden ? 'bi bi-eye-slash' : 'bi bi-eye';
                }
            });
        }
    </script>
</body>
</html>
