<!DOCTYPE html>
<html>
<head>
    <title>Laravel Setup</title>
    <style>
        /* body { font-family: sans-serif; padding: 30px }
        .ok { color: green }
        .fail { color: red }
        button { padding: 10px 20px } */
        body { font-family: sans-serif; padding: 30px }

        .ok { color: green }
        .fail { color: red }

        button {
            padding: 10px 20px;
            cursor: pointer;
        }

        #loader {
            display: none;
            margin-top: 20px;
        }

        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #ddd;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-bottom: 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>

    @if ($status['users_view'] ) 
        <h2>🚀 Laravel Setup Checker</h2>
        <h2>🚀 Laravel Setup Checker</h2>
        <script>window.location = "/login";</script>
    @else

        @if(session('success'))
            <p class="ok">{{ session('success') }}</p>
        @endif

        @if(session('error'))
            <p class="fail">{{ session('error') }}</p>
        @endif

        <ul>
            <li>pgcrypto:
                <b class="{{ $status['pgcrypto'] ? 'ok' : 'fail' }}">
                    {{ $status['pgcrypto'] ? 'OK' : 'NOT FOUND' }}
                </b>
            </li>

            <li>pegawai.id:
                <b class="{{ $status['pegawai_columns']['id'] ? 'ok' : 'fail' }}"> 
                    {{ $status['pegawai_columns']['id'] ? 'OK' : 'MISSING' }}
                </b>
            </li>

            <li>pegawai.email:
                <b class="{{ $status['pegawai_columns']['email'] ? 'ok' : 'fail' }}">
                    {{ $status['pegawai_columns']['email'] ? 'OK' : 'MISSING' }}
                </b>
            </li>

            <li>pegawai.email_verified_at:
                <b class="{{ $status['pegawai_columns']['email_verified_at'] ? 'ok' : 'fail' }}">
                    {{ $status['pegawai_columns']['email_verified_at'] ? 'OK' : 'MISSING' }}
                </b>
            </li>

            <li>pegawai.remember_token:
                <b class="{{ $status['pegawai_columns']['remember_token'] ? 'ok' : 'fail' }}">
                    {{ $status['pegawai_columns']['remember_token'] ? 'OK' : 'MISSING' }}
                </b>
            </li>

            <li>users view:
                <b class="{{ $status['users_view'] ? 'ok' : 'fail' }}">
                    {{ $status['users_view'] ? 'OK' : 'NOT FOUND' }}
                </b>
            </li>

            <li>transaksi_parkir.veri_time:
                <b class="{{ $status['veri_time'] ? 'ok' : 'fail' }}">
                    {{ $status['veri_time'] ? 'OK' : 'MISSING' }}
                </b>
            </li>
        </ul>

        <form method="POST" action="{{ route('setup.run') }}">
            @csrf
            <button type="submit">⚙️ Jalankan Setup</button>

            <div id="loader">
                <div class="spinner"></div>
                <b>Memproses setup database…</b>
            </div>
        </form>
    @endif


    <script>
        document.getElementById('runSetup').addEventListener('click', function () {
            const btn = this;
            const loader = document.getElementById('loader');

            btn.disabled = true;
            btn.innerText = 'Processing...';
            loader.style.display = 'block';

            fetch("{{ route('setup.run') }}", {
                method: "POST",
                headers: {
                    "X-CSRF-TOKEN": document.querySelector('meta[name="csrf-token"]').content,
                    "Accept": "application/json"
                }
            })
            .then(res => res.json())
            .then(res => {
                if (res.success) {
                    loader.innerHTML = "<b class='ok'>✅ Setup selesai, mengalihkan halaman...</b>";

                    setTimeout(() => {
                        window.location.href = res.redirect;
                    }, 1500);
                }
            })
            .catch(err => {
                loader.innerHTML = "<b class='fail'>❌ Setup gagal</b>";
                btn.disabled = false;
                btn.innerText = '⚙️ Jalankan Setup';
                console.error(err);
            });
        });
    </script>
</body>
</html>
