<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Instalasi SIMAK - Sistem Informasi Manajemen Akademik</title>
    <!-- Google Fonts (Inter) -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <style>
        :root {
            --primary: #1e3c72;
            --primary-light: #2a5298;
            --accent: #0d6efd;
            --success: #198754;
            --danger: #dc3545;
            --warning: #ffc107;
            --text-dark: #212529;
            --text-muted: #6c757d;
            --bg-light: #f8f9fa;
            --border-color: #dee2e6;
            --shadow-sm: 0 .125rem .25rem rgba(0,0,0,.075);
            --shadow-lg: 0 1rem 3rem rgba(0,0,0,.175);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: var(--text-dark);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .installer-container {
            width: 100%;
            max-width: 680px;
            background: #ffffff;
            border-radius: 20px;
            box-shadow: var(--shadow-lg);
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .installer-header {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
            color: #ffffff;
            padding: 40px 30px;
            text-align: center;
            position: relative;
        }

        .installer-header h1 {
            font-size: 1.8rem;
            font-weight: 700;
            margin-bottom: 8px;
            letter-spacing: -0.5px;
        }

        .installer-header p {
            font-size: 0.95rem;
            color: rgba(255, 255, 255, 0.75);
        }

        .step-indicators {
            display: flex;
            justify-content: space-between;
            background: #f1f3f5;
            padding: 15px 30px;
            border-bottom: 1px solid var(--border-color);
        }

        .step-indicator {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.85rem;
            font-weight: 500;
            color: var(--text-muted);
            position: relative;
        }

        .step-indicator.active {
            color: var(--accent);
            font-weight: 600;
        }

        .step-indicator.completed {
            color: var(--success);
        }

        .step-indicator .circle {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            background: #dee2e6;
            color: var(--text-muted);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.75rem;
            font-weight: 600;
        }

        .step-indicator.active .circle {
            background: var(--accent);
            color: #ffffff;
        }

        .step-indicator.completed .circle {
            background: var(--success);
            color: #ffffff;
        }

        .installer-body {
            padding: 40px 30px;
        }

        .step-content {
            display: none;
        }

        .step-content.active {
            display: block;
            animation: fadeIn 0.4s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-size: 0.85rem;
            font-weight: 600;
            color: #495057;
            margin-bottom: 8px;
        }

        .form-control {
            width: 100%;
            padding: 12px 16px;
            font-size: 0.95rem;
            font-family: inherit;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            outline: none;
            transition: border-color 0.2s ease;
        }

        .form-control:focus {
            border-color: var(--accent);
            box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.15);
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }

        @media (max-width: 576px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            .step-indicators {
                padding: 15px 15px;
            }
            .step-indicator span {
                display: none;
            }
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 12px 24px;
            font-size: 0.95rem;
            font-weight: 600;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--accent) 0%, #0b5ed7 100%);
            color: #ffffff;
        }

        .btn-primary:hover {
            opacity: 0.9;
        }

        .btn-secondary {
            background: #e9ecef;
            color: #495057;
        }

        .btn-secondary:hover {
            background: #dee2e6;
        }

        .btn:disabled {
            background: #dee2e6;
            color: #adb5bd;
            cursor: not-allowed;
        }

        .btn-container {
            display: flex;
            justify-content: space-between;
            margin-top: 35px;
            padding-top: 20px;
            border-top: 1px solid var(--border-color);
        }

        /* Requirements Table */
        .req-list {
            list-style: none;
            margin-bottom: 20px;
        }

        .req-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 12px 15px;
            background: var(--bg-light);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            margin-bottom: 10px;
            font-size: 0.9rem;
        }

        .req-item .req-name {
            font-weight: 500;
        }

        .req-item .req-status {
            display: flex;
            align-items: center;
            gap: 6px;
            font-weight: 600;
        }

        .req-status.success {
            color: var(--success);
        }

        .req-status.danger {
            color: var(--danger);
        }

        /* Progress State */
        .progress-container {
            text-align: center;
            padding: 40px 10px;
        }

        .spinner {
            width: 50px;
            height: 50px;
            border: 5px solid #e9ecef;
            border-top: 5px solid var(--accent);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .alert {
            padding: 15px;
            border-radius: 8px;
            font-size: 0.85rem;
            margin-bottom: 20px;
            line-height: 1.5;
        }

        .alert-warning {
            background: #fff3cd;
            color: #664d03;
            border: 1px solid #ffe69c;
        }
    </style>
</head>
<body>

<div class="installer-container">
    <div class="installer-header">
        <h1>SIMAK INSTALLATION WIZARD</h1>
        <p>Pasang sistem manajemen akademik sekolah Anda secara instan</p>
    </div>

    <div class="step-indicators">
        <div class="step-indicator active" id="ind-1">
            <div class="circle">1</div>
            <span>Persyaratan</span>
        </div>
        <div class="step-indicator" id="ind-2">
            <div class="circle">2</div>
            <span>Database</span>
        </div>
        <div class="step-indicator" id="ind-3">
            <div class="circle">3</div>
            <span>Admin</span>
        </div>
    </div>

    <div class="installer-body">
        <form id="installForm" method="POST" action="/install/process">
            <?= csrf_field() ?>

            <!-- STEP 1: Requirements Check -->
            <div class="step-content active" id="step-1">
                <h3 style="margin-bottom: 15px; font-weight: 600;">Pemeriksaan Sistem</h3>
                <p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 20px;">Sistem perlu memastikan hosting server Anda memenuhi spesifikasi minimum sebelum instalasi dimulai.</p>
                
                <ul class="req-list">
                    <?php foreach ($requirements as $key => $req): ?>
                        <li class="req-item">
                            <span class="req-name"><?= $req['name'] ?></span>
                            <span class="req-status <?= $req['met'] ? 'success' : 'danger' ?>">
                                <?php if ($req['met']): ?>
                                    <i class="bi bi-check-circle-fill"></i> <?= $req['current'] ?>
                                <?php else: ?>
                                    <i class="bi bi-x-circle-fill"></i> <?= $req['current'] ?>
                                <?php endif; ?>
                            </span>
                        </li>
                    <?php endforeach; ?>
                </ul>

                <?php if (!$allMet): ?>
                    <div class="alert alert-warning">
                        <i class="bi bi-exclamation-triangle-fill"></i> <strong>Perhatian:</strong> Beberapa persyaratan sistem belum terpenuhi. Tolong perbaiki perizinan folder atau aktifkan modul PHP yang kurang terlebih dahulu.
                    </div>
                <?php endif; ?>

                <div class="btn-container" style="justify-content: flex-end;">
                    <button type="button" class="btn btn-primary" id="btn-next-1" <?= !$allMet ? 'disabled' : '' ?>>
                        Lanjut <i class="bi bi-arrow-right"></i>
                    </button>
                </div>
            </div>

            <!-- STEP 2: Database Configuration -->
            <div class="step-content" id="step-2">
                <h3 style="margin-bottom: 15px; font-weight: 600;">Koneksi Database</h3>
                <p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 20px;">Buat database kosong terlebih dahulu di phpMyAdmin, lalu masukkan detail koneksinya di bawah ini.</p>

                <div class="form-row">
                    <div class="form-group">
                        <label for="db_host">Database Host</label>
                        <input type="text" class="form-control" id="db_host" name="db_host" value="localhost" placeholder="e.g. localhost">
                    </div>
                    <div class="form-group">
                        <label for="db_port">Port</label>
                        <input type="text" class="form-control" id="db_port" name="db_port" value="3306" placeholder="e.g. 3306">
                    </div>
                </div>

                <div class="form-group">
                    <label for="db_name">Nama Database</label>
                    <input type="text" class="form-control" id="db_name" name="db_name" placeholder="e.g. simak_db">
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="db_user">Username Database</label>
                        <input type="text" class="form-control" id="db_user" name="db_user" value="root" placeholder="e.g. root">
                    </div>
                    <div class="form-group">
                        <label for="db_pass">Password Database</label>
                        <input type="password" class="form-control" id="db_pass" name="db_pass" placeholder="Kosongkan jika tidak ada">
                    </div>
                </div>

                <div class="btn-container">
                    <button type="button" class="btn btn-secondary" id="btn-back-2">
                        <i class="bi bi-arrow-left"></i> Kembali
                    </button>
                    <button type="button" class="btn btn-primary" id="btn-next-2">
                        Lanjut <i class="bi bi-arrow-right"></i>
                    </button>
                </div>
            </div>

            <!-- STEP 3: School Info & Superadmin Account -->
            <div class="step-content" id="step-3">
                <h3 style="margin-bottom: 15px; font-weight: 600;">Informasi Sekolah & Akun</h3>
                <p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 20px;">Tentukan nama sekolah Anda dan buat akun administrator pertama untuk login.</p>

                <div class="form-group">
                    <label for="school_name">Nama Instansi Sekolah</label>
                    <input type="text" class="form-control" id="school_name" name="school_name" placeholder="e.g. SMA Nusantara atau SMPN 1 Makassar">
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="school_type">Tipe Sekolah</label>
                        <select class="form-control" id="school_type" name="school_type">
                            <option value="TK">TK</option>
                            <option value="SD">SD</option>
                            <option value="SMP">SMP</option>
                            <option value="SMA">SMA</option>
                            <option value="SMK">SMK</option>
                            <option value="RA">RA</option>
                            <option value="MI">MI</option>
                            <option value="MTs">MTs</option>
                            <option value="MA">MA</option>
                            <option value="MAK">MAK</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="accreditation">Akreditasi</label>
                        <select class="form-control" id="accreditation" name="accreditation">
                            <option value="A">Akreditasi A</option>
                            <option value="B">Akreditasi B</option>
                            <option value="C">Akreditasi C</option>
                            <option value="Belum Terakreditasi">Belum Terakreditasi</option>
                        </select>
                    </div>
                </div>

                <div class="form-group" style="margin-top: 15px; border-top: 1px solid var(--border-color); padding-top: 15px;">
                    <label for="admin_email">Email Administrator</label>
                    <input type="email" class="form-control" id="admin_email" name="admin_email" placeholder="e.g. admin@sekolah.sch.id">
                </div>

                <div class="form-group">
                    <label for="admin_password">Password Administrator</label>
                    <input type="password" class="form-control" id="admin_password" name="admin_password" placeholder="Minimal 8 karakter">
                </div>

                <div class="btn-container">
                    <button type="button" class="btn btn-secondary" id="btn-back-3">
                        <i class="bi bi-arrow-left"></i> Kembali
                    </button>
                    <button type="submit" class="btn btn-primary" id="btn-submit" style="background: linear-gradient(135deg, var(--success) 0%, #157347 100%);">
                        <i class="bi bi-cloud-check-fill"></i> Mulai Pasang Sistem
                    </button>
                </div>
            </div>

            <!-- STEP 4: Processing (Full Screen Overlay/State) -->
            <div class="step-content" id="step-loading">
                <div class="progress-container">
                    <div class="spinner"></div>
                    <h4 style="margin-bottom: 10px; font-weight: 600;">Memproses Instalasi Sistem...</h4>
                    <p class="text-muted" style="font-size: 0.9rem; line-height: 1.5;">Sedang menghubungkan ke database, menyusun file konfigurasi, menjalankan migrasi 71 tabel, mengimpor data bawaan, dan mengonfigurasi akun admin Anda.<br><strong class="text-danger">Mohon jangan menutup halaman ini sampai selesai.</strong></p>
                </div>
            </div>

        </form>
    </div>
</div>

<script>
$(document).ready(function() {
    // Step switching logic
    $('#btn-next-1').click(function() {
        switchStep(1, 2);
    });

    $('#btn-back-2').click(function() {
        switchStep(2, 1);
    });

    $('#btn-next-2').click(function() {
        // Validate Step 2 inputs locally
        if ($('#db_name').val().trim() === '' || $('#db_user').val().trim() === '') {
            Swal.fire({
                icon: 'warning',
                title: 'Data Kurang',
                text: 'Harap isi nama database dan username database terlebih dahulu.'
            });
            return;
        }
        switchStep(2, 3);
    });

    $('#btn-back-3').click(function() {
        switchStep(3, 2);
    });

    function switchStep(from, to) {
        $(`#step-${from}`).removeClass('active');
        $(`#step-${to}`).addClass('active');

        $(`#ind-${from}`).removeClass('active').addClass('completed');
        $(`#ind-${to}`).addClass('active');
    }

    // Form submission via AJAX
    $('#installForm').submit(function(e) {
        e.preventDefault();

        // Local validation step 3
        if ($('#school_name').val().trim() === '' || $('#admin_email').val().trim() === '' || $('#admin_password').val().trim().length < 8) {
            Swal.fire({
                icon: 'warning',
                title: 'Validasi Gagal',
                text: 'Pastikan Nama Sekolah terisi, Email valid, dan Password admin minimal 8 karakter.'
            });
            return;
        }

        // Show loading state
        $('#step-3').removeClass('active');
        $('#step-loading').addClass('active');
        $('#ind-3').addClass('completed');

        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(res) {
                if (res.success) {
                    Swal.fire({
                        icon: 'success',
                        title: 'Instalasi Sukses!',
                        text: 'Aplikasi SIMAK Anda berhasil dikonfigurasi. Anda sekarang dialihkan ke laman login.',
                        allowOutsideClick: false,
                        confirmButtonText: 'Buka Laman Login'
                    }).then(() => {
                        window.location.href = '/';
                    });
                } else {
                    // Revert to step 3 and show error
                    $('#step-loading').removeClass('active');
                    $('#step-3').addClass('active');
                    $('#ind-3').removeClass('completed');

                    Swal.fire({
                        icon: 'error',
                        title: 'Instalasi Gagal',
                        text: res.message
                    });
                }
            },
            error: function(xhr, status, error) {
                $('#step-loading').removeClass('active');
                $('#step-3').addClass('active');
                $('#ind-3').removeClass('completed');

                Swal.fire({
                    icon: 'error',
                    title: 'Kesalahan Sistem',
                    text: 'Terjadi kegagalan jaringan atau timeout. Silakan coba kembali.'
                });
            }
        });
    });
});
</script>
</body>
</html>
