<?php
// Funkce pro načtení YAML (jednoduchá verze bez externích knihoven)
function load_yaml($file) {
    if (function_exists('yaml_parse_file')) {
        return yaml_parse_file($file);
    }

    $lines = file($file);
    $data = [];
    $path = [];
    $lastIndent = -1;

    foreach ($lines as $line) {
        if (trim($line) === '' || strpos(trim($line), '#') === 0) continue;
        
        $indent = strspn($line, ' ');
        $line = trim($line);
        $parts = explode(':', $line, 2);
        $key = trim($parts[0]);
        $value = isset($parts[1]) ? trim($parts[1], " \"'\r\n") : null;

        if ($indent > $lastIndent) {
            $path[] = $key;
        } elseif ($indent < $lastIndent) {
            $stepsBack = ($lastIndent - $indent) / 2;
            array_splice($path, -$stepsBack - 1);
            $path[] = $key;
        } else {
            array_pop($path);
            $path[] = $key;
        }
        
        $lastIndent = $indent;

        if ($value !== null && $value !== '') {
            $current = &$data;
            foreach ($path as $p) {
                if (!isset($current[$p])) $current[$p] = [];
                $current = &$current[$p];
            }
            $current = $value;
        }
    }
    return $data;
}

// Načtení překladů
$trans = load_yaml('pl.yml');

function t($key) {
    global $trans;
    $keys = explode('.', $key);
    $value = $trans;
    
    foreach ($keys as $k) {
        if (isset($value[$k])) {
            $value = $value[$k];
        } else {
            return $key;
        }
    }
    return $value;
}
?>

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= t('title') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    
    <style>
        body {
            background-image: url("background.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            min-height: 100vh;
        }

        nav {
            background-color: #172473;
            padding: 10px 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .nav-container {
            max-width: 1320px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .nav-links a { 
            color: #F8F9FA; 
            text-decoration: none; 
            margin-left: 20px; 
            font-weight: 500;
        }
        .nav-links a:hover { color: #ccc; }

        .text-muted-sub { color: #6c757d !important; }
        h1.fw-semibold { font-weight: 600 !important; color: #2c3e50; }

        .list-unstyled li { margin-bottom: 0.5rem; font-size: 1.05rem; }
        .checkmark { display: inline-flex; align-items: center; justify-content: center; }

        .loan-calculator-container {
            background: #ffffff;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.08);
            width: 100%;
            max-width: 450px;
        }

        .form-label { font-weight: 600; font-size: 0.9rem; color: #333; margin-bottom: 5px; }
        .form-control {
            padding: 12px 15px;
            border-radius: 8px;
            border: 1px solid #dee2e6;
            background-color: #f8f9fa;
            font-size: 1rem;
        }
        .form-control:focus {
            border-color: #3b5998;
            background-color: #fff;
            box-shadow: 0 0 0 4px rgba(59, 89, 152, 0.1);
        }

        .btn-primary-custom {
            background-color: #3b5998;
            color: white;
            font-weight: 600;
            padding: 14px;
            border-radius: 8px;
            width: 100%;
            border: none;
            transition: all 0.2s;
            margin-top: 10px;
        }
        .btn-primary-custom:hover {
            background-color: #2d4373;
            transform: translateY(-1px);
        }

        .disclaimer { font-size: 0.75rem; color: #adb5bd; text-align: center; margin-top: 15px; }
    </style>
</head>
<body>

    <nav>
        <div class="nav-container">
            <div class="logo">
                <img src="BPw.svg" alt="Profi Pôžička" style="height: 50px;"> 
            </div>
            <div class="nav-links">
            </div>
        </div>
    </nav>

    <div class="welcome-container py-5">
        <div class="container-lg">
            <div class="row gy-4 align-items-center">
                
                <!-- Levý sloupec -->
                <div class="col-12 col-lg-6 order-lg-0 order-0 d-flex justify-content-center flex-column">
                    <h1 class="fw-semibold mb-3">
                        <?= t('welcome.banner.title') ?>
                    </h1>
                    <p class="fs-5 mb-4 text-muted-sub">
                        <?= t('welcome.banner.subtitle') ?>
                    </p>
                    
                    <ul class="list-unstyled mb-0">
                        <?php 
                        for ($i = 1; $i <= 6; $i++): ?>
                        <li class="mb-3 d-flex align-items-start text-muted-sub">
                            <span class="checkmark me-3" aria-hidden="true">
                                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                    <circle cx="12" cy="12" r="11" fill="#F1F6F9" />
                                    <path d="M7.5 12.5L10.5 15.5L16.5 9.5" stroke="#2C8F7E" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" />
                                </svg>
                            </span>
                            <span><?= t('welcome.banner.features.bullet_' . $i) ?></span>
                        </li>
                        <?php endfor; ?>
                    </ul>
                </div>

                <!-- Pravý sloupec: Formulář -->
                <div class="col-12 col-lg-6 order-lg-1 order-1 d-flex justify-content-lg-end justify-content-center">
                    <div class="loan-calculator-container">
                        <h2 class="h4 mb-4 text-dark fw-bold"><?= t('form.title') ?></h2>
                        
                        <form action="backend.php" method="POST">
                            <div class="mb-3">
                                <label for="fullname" class="form-label"><?= t('form.fullname') ?></label>
                                <input type="text" class="form-control" id="fullname" name="fullname" placeholder="Jan Kowalski" required>
                            </div>

                            <div class="mb-3">
                                <label for="nip" class="form-label"><?= t('form.nip') ?></label>
                                <input type="text" class="form-control" id="nip" name="nip" placeholder="1234567890" pattern="\d{10}" title="Podaj prawidłowy 10-cyfrowy NIP" required>
                            </div>

                            <div class="mb-4">
                                <label for="email" class="form-label"><?= t('form.email') ?></label>
                                <input type="email" class="form-control" id="email" name="email" placeholder="jan@firma.pl" required>
                            </div>

                            <div class="mb-4">
                                <label for="phone" class="form-label"><?= t('form.phone') ?></label>
                                <input type="text" class="form-control" id="phone" name="phone" placeholder="+48 123 456 789" required>
                            </div>

                            <button type="submit" class="btn-primary-custom">
                                <?= t('form.submit') ?>
                            </button>
                        </form>
                        
                        <p class="disclaimer">
                            <?= t('form.disclaimer') ?>
                        </p>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <script>
        // Zkontrolujeme URL parametry
        const urlParams = new URLSearchParams(window.location.search);
        const status = urlParams.get('status');

        if (status === 'success') {
            Swal.fire({
                title: 'Dziękujemy!', // Polsky "Děkujeme"
                text: 'Twoje zgłoszenie zostało pomyślnie wysłane. Skontaktujemy się wkrótce.',
                icon: 'success',
                confirmButtonColor: '#3b5998',
                confirmButtonText: 'Super'
            }).then(() => {
                // Vyčistí URL od parametru ?status=success, aby se alert neukazoval po refresh
                window.history.replaceState(null, null, window.location.pathname);
            });
        } else if (status === 'error') {
            Swal.fire({
                title: 'Błąd',
                text: 'Wystąpił błąd podczas wysyłania formularza.',
                icon: 'error',
                confirmButtonColor: '#3b5998'
            });
        }
    </script>

</body>
</html>
