<?= $this->extend('layout/main') ?>

<?= $this->section('title') ?>Free Personal Domain Names<?= $this->endSection() ?>

<?= $this->section('content') ?>

<div class="hero">
    <div class="container">
        <span class="badge">Public Beta - Limited Time Offer</span>
        <h1>Your Professional<br><span class="highlight">Domain Identity</span></h1>
        <p>Get your free .pro.bd, .ami.bd, or .iam.bd domain today.<br>No credit card required. Full DNS control. Instant activation.</p>
        
        <div class="search-container">
            <input type="text" id="domain-input" class="search-input" placeholder="Enter your desired name">
            <select id="suffix-select" class="search-select">
                <?php foreach($suffixes as $suffix): ?>
                    <option value="<?= esc($suffix->id) ?>" data-name="<?= esc($suffix->suffix) ?>"><?= esc($suffix->suffix) ?></option>
                <?php endforeach; ?>
            </select>
            <button id="search-btn" class="search-btn">Search</button>
        </div>
        
        <div id="search-result" style="margin-top: 1rem; display: none; text-align: left; max-width: 600px; margin-left: auto; margin-right: auto; background: white; padding: 1.5rem; border: 1px solid var(--border-color); border-radius: 0.5rem; box-shadow: 0 4px 6px rgba(0,0,0,0.05);">
            <!-- Results injected here -->
        </div>

        <div style="margin-top: 2rem; display: flex; justify-content: center; gap: 2rem; font-size: 0.875rem; color: var(--success); font-weight: 500;">
            <span style="display: flex; align-items: center; gap: 0.5rem;"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>Forever free</span>
            <span style="display: flex; align-items: center; gap: 0.5rem;"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>No credit card</span>
            <span style="display: flex; align-items: center; gap: 0.5rem;"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>Instant activation</span>
        </div>
    </div>
</div>

<div id="suffixes" class="container" style="padding-top: 4rem; padding-bottom: 4rem;">
    <h2 style="text-align: center; margin-bottom: 2rem; color: var(--text-muted); font-size: 1.1rem; font-weight: 500;">Six specialized domain extensions for personal branding</h2>
    
    <div class="grid grid-3">
        <?php foreach($suffixes as $suffix): ?>
        <div class="card">
            <div class="suffix-title">
                <?= esc($suffix->suffix) ?>
                <?php if($suffix->suffix == '.pro.bd'): ?><span style="font-size: 0.7rem; background: var(--primary); color: white; padding: 0.2rem 0.5rem; border-radius: 4px;">Popular</span><?php endif; ?>
            </div>
            <div class="suffix-type"><?= esc($suffix->type) ?></div>
            <div class="suffix-desc"><?= esc($suffix->description) ?></div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

<div class="container" style="padding-top: 4rem; padding-bottom: 4rem; text-align: center;">
    <h2 style="font-size: 2.5rem; font-weight: 800; margin-bottom: 0.5rem;">Up and Running in 3 Minutes</h2>
    <p style="color: var(--text-muted); margin-bottom: 3rem;">Simple verification process, instant activation</p>
    
    <div style="display: flex; justify-content: center; gap: 4rem;">
        <div style="text-align: center;">
            <div style="width: 3rem; height: 3rem; background: var(--primary); color: white; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; font-weight: bold; margin: 0 auto 1rem;">1</div>
            <h4 style="font-weight: 600;">Choose Domain</h4>
            <p style="color: var(--text-muted); font-size: 0.875rem; margin-top: 0.5rem;">Select your perfect suffix</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 3rem; height: 3rem; background: var(--primary); color: white; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; font-weight: bold; margin: 0 auto 1rem;">2</div>
            <h4 style="font-weight: 600;">Create Account</h4>
            <p style="color: var(--text-muted); font-size: 0.875rem; margin-top: 0.5rem;">Sign up using email</p>
        </div>
        <div style="text-align: center;">
            <div style="width: 3rem; height: 3rem; background: var(--primary); color: white; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; font-weight: bold; margin: 0 auto 1rem;">3</div>
            <h4 style="font-weight: 600;">Setup DNS</h4>
            <p style="color: var(--text-muted); font-size: 0.875rem; margin-top: 0.5rem;">Add records or custom NS</p>
        </div>
    </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    document.getElementById('search-btn').addEventListener('click', function() {
        const subdomain = document.getElementById('domain-input').value.trim();
        const suffixSelect = document.getElementById('suffix-select');
        const suffixId = suffixSelect.value;
        const suffixText = suffixSelect.options[suffixSelect.selectedIndex].getAttribute('data-name');
        const resultDiv = document.getElementById('search-result');
        
        if(!subdomain) {
            alert('Please enter a domain name');
            return;
        }

        // Basic validation for subdomain
        if (!/^[a-zA-Z0-9\-]+$/.test(subdomain)) {
            alert('Domain name can only contain letters, numbers, and hyphens.');
            return;
        }

        const fullDomain = subdomain + suffixText;
        
        resultDiv.style.display = 'block';
        resultDiv.innerHTML = 'Searching...';

        fetch('<?= base_url("api/check-domain") ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: JSON.stringify({ subdomain: subdomain, suffix_id: suffixId, full_domain: fullDomain })
        })
        .then(res => res.json())
        .then(data => {
            if(data.available) {
                resultDiv.innerHTML = `
                    <div style="display: flex; justify-content: space-between; align-items: center;">
                        <div style="color: var(--success); font-weight: 600; font-size: 1.1rem; display: flex; align-items: center; gap: 0.5rem;">
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
                            ${fullDomain} is available!
                        </div>
                        <a href="<?= base_url('register') ?>?domain=${encodeURIComponent(fullDomain)}&suffix=${suffixId}" class="btn btn-primary" style="background-color: var(--success);">Claim Now</a>
                    </div>
                `;
            } else {
                resultDiv.innerHTML = `
                    <div style="color: var(--error); font-weight: 600; font-size: 1.1rem; display: flex; align-items: center; gap: 0.5rem;">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></svg>
                        ${fullDomain} is already taken.
                    </div>
                `;
            }
        });
    });
</script>
<?= $this->endSection() ?>
