@extends('layouts.public')

@section('title', 'Pedido de Orçamento | SecureVision')
@section('meta_description', 'Solicite um orçamento de CCTV, alarmes, controle de acesso ou monitoramento 24h com a SecureVision.')

@php
    $submittedQuote = session('quote_payload');
    $whatsAppText = '';

    if ($submittedQuote) {
        $whatsAppText = "Olá SecureVision! Enviei um pedido de orçamento pelo site.\n\n"
            . "*Referência:* #" . $submittedQuote['id'] . "\n"
            . "*Nome:* " . $submittedQuote['name'] . "\n"
            . "*Contacto:* " . $submittedQuote['phone'] . "\n"
            . "*Morada:* " . ($submittedQuote['address'] ?: 'Não informada') . "\n"
            . "*Serviços:* " . (count($submittedQuote['services']) ? implode(', ', $submittedQuote['services']) : 'Não especificado') . "\n"
            . "*Notas:* " . ($submittedQuote['notes'] ?: 'Sem observações');
    }
@endphp

@section('content')
    <section class="relative overflow-hidden border-b border-slate-800/50 py-20 sm:py-24">
        <div class="absolute inset-0 cyber-grid pointer-events-none"></div>
        <div class="absolute -top-24 left-1/2 h-80 w-80 -translate-x-1/2 rounded-full blur-3xl pointer-events-none" style="background:rgba(59,130,246,0.18);"></div>

        <div class="relative z-10 mx-auto max-w-6xl px-4 sm:px-6 lg:px-8">
            <div class="grid grid-cols-1 lg:grid-cols-[0.9fr_1.1fr] gap-10 items-start">
                <div class="lg:sticky lg:top-24">
                    <span class="badge-primary">Orçamento Online</span>
                    <h1 class="mt-6 text-4xl sm:text-5xl font-extrabold tracking-tight text-white leading-tight">
                        Conte-nos o que precisa proteger.
                    </h1>
                    <p class="mt-5 text-base leading-relaxed" style="color:#cbd5e1;">
                        Em poucos passos recolhemos os dados essenciais para a equipa técnica preparar uma proposta bem enquadrada para o seu imóvel, empresa ou instalação industrial.
                    </p>

                    <div class="mt-8 grid grid-cols-1 sm:grid-cols-3 lg:grid-cols-1 gap-4">
                        @foreach([
                            ['title' => 'Resposta rápida', 'text' => 'Contacto em até 24 horas úteis.'],
                            ['title' => 'Escopo organizado', 'text' => 'CCTV, alarmes, acesso e monitoramento no mesmo pedido.'],
                            ['title' => 'Atendimento direto', 'text' => 'Depois do envio, pode continuar pelo WhatsApp.'],
                        ] as $benefit)
                            <div class="rounded-xl border border-slate-800 bg-slate-900/60 p-4">
                                <p class="font-semibold text-white">{{ $benefit['title'] }}</p>
                                <p class="mt-1 text-sm" style="color:#94a3b8;">{{ $benefit['text'] }}</p>
                            </div>
                        @endforeach
                    </div>
                </div>

                <div class="glass-card p-5 sm:p-8" x-data="{ step: {{ $submittedQuote ? 4 : 1 }}, next(currentStep, nextStep) { const invalid = this.$root.querySelector('[data-step=&quot;' + currentStep + '&quot;] [required]:invalid'); if (invalid) { invalid.reportValidity(); return; } this.step = nextStep; } }">
                    @if($submittedQuote)
                        <div class="text-center py-8 space-y-8">
                            <div class="mx-auto flex h-16 w-16 items-center justify-center rounded-full border border-emerald-500/30 bg-emerald-500/10">
                                <svg class="h-8 w-8 text-emerald-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                                </svg>
                            </div>
                            <div>
                                <h2 class="text-2xl font-bold text-white">Pedido recebido com sucesso</h2>
                                <p class="mx-auto mt-3 max-w-md text-sm leading-relaxed" style="color:#94a3b8;">
                                    A referência do seu pedido é #{{ $submittedQuote['id'] }}. Pode descarregar o resumo ou iniciar conversa no WhatsApp com os dados já preenchidos.
                                </p>
                            </div>
                            <div class="flex flex-col sm:flex-row justify-center gap-3">
                                <a href="https://wa.me/244976463372?text={{ urlencode($whatsAppText) }}" target="_blank" rel="noopener noreferrer" class="btn-primary" style="background:#10b981;">
                                    Falar no WhatsApp
                                </a>
                                <a href="{{ route('quote.download', $submittedQuote['id']) }}" class="btn-secondary">
                                    Descarregar resumo
                                </a>
                            </div>
                            <a href="{{ route('quote.create') }}" class="inline-flex text-sm font-semibold text-slate-400 hover:text-white">
                                Enviar outro pedido
                            </a>
                        </div>
                    @else
                        <div class="mb-8">
                            <div class="flex items-center justify-between text-xs font-semibold uppercase tracking-wider" style="color:#94a3b8;">
                                <span :class="step >= 1 ? 'text-white' : ''">Contacto</span>
                                <span :class="step >= 2 ? 'text-white' : ''">Escopo</span>
                                <span :class="step >= 3 ? 'text-white' : ''">Finalização</span>
                            </div>
                            <div class="mt-4 grid grid-cols-3 gap-2">
                                <div class="h-2 rounded-full" :class="step >= 1 ? 'bg-primary' : 'bg-slate-800'"></div>
                                <div class="h-2 rounded-full" :class="step >= 2 ? 'bg-primary' : 'bg-slate-800'"></div>
                                <div class="h-2 rounded-full" :class="step >= 3 ? 'bg-primary' : 'bg-slate-800'"></div>
                            </div>
                        </div>

                        @if ($errors->any())
                            <div class="alert-error mb-6">
                                <ul class="list-disc list-inside text-sm">
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif

                        <form method="POST" action="{{ route('quote.store') }}" class="space-y-6">
                            @csrf
                            <input type="hidden" name="full_quote" value="1">

                            <div x-show="step === 1" x-cloak data-step="1" class="space-y-5">
                                <h2 class="text-lg font-bold text-white border-b border-slate-800 pb-3">Informações de contacto</h2>
                                <div>
                                    <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Nome completo / empresa *</label>
                                    <input type="text" name="name" value="{{ old('name') }}" required class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Ex: Manuel Silva">
                                </div>
                                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                    <div>
                                        <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Email</label>
                                        <input type="email" name="email" value="{{ old('email') }}" class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="cliente@email.com">
                                    </div>
                                    <div>
                                        <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Telefone / WhatsApp *</label>
                                        <input type="tel" name="phone" value="{{ old('phone') }}" required class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="912 345 678">
                                    </div>
                                </div>
                                <div>
                                    <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Morada do local de instalação</label>
                                    <input type="text" name="address" value="{{ old('address') }}" class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Camama, Belas, Luanda">
                                </div>
                                <div class="flex justify-end pt-2">
                                    <button type="button" @click="next(1, 2)" class="btn-primary">Próximo passo</button>
                                </div>
                            </div>

                            <div x-show="step === 2" x-cloak data-step="2" class="space-y-5">
                                <h2 class="text-lg font-bold text-white border-b border-slate-800 pb-3">Tipo de instalação e escopo</h2>
                                <div>
                                    <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Tipo de imóvel</label>
                                    <select name="property_type" class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500">
                                        <option value="">Selecione...</option>
                                        <option value="residential" @selected(old('property_type') === 'residential')>Residencial</option>
                                        <option value="commercial" @selected(old('property_type') === 'commercial')>Comercial</option>
                                        <option value="industrial" @selected(old('property_type') === 'industrial')>Industrial</option>
                                    </select>
                                </div>
                                <div>
                                    <span class="block text-xs font-bold uppercase tracking-wider mb-3" style="color:#94a3b8;">Serviços pretendidos</span>
                                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
                                        @foreach([
                                            'cctv' => 'CCTV e vídeo vigilância',
                                            'alarme' => 'Sistemas de alarme',
                                            'acesso' => 'Controle de acesso',
                                            'monitorizacao' => 'Monitoramento 24h',
                                        ] as $value => $label)
                                            <label class="flex items-center gap-3 rounded-xl bg-slate-950/80 border border-slate-800 p-4 cursor-pointer hover:border-blue-500/50 transition-colors">
                                                <input type="checkbox" name="services[]" value="{{ $value }}" @checked(in_array($value, old('services', []))) class="h-4 w-4 rounded border-slate-700 bg-slate-950 text-blue-600">
                                                <span class="text-sm font-semibold text-slate-200">{{ $label }}</span>
                                            </label>
                                        @endforeach
                                    </div>
                                </div>
                                <div>
                                    <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Quantidade estimada de câmeras</label>
                                    <input type="number" name="cctv_count" value="{{ old('cctv_count') }}" min="0" class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Ex: 4">
                                </div>
                                <div class="flex justify-between pt-2 border-t border-slate-800">
                                    <button type="button" @click="step = 1" class="btn-secondary">Voltar</button>
                                    <button type="button" @click="next(2, 3)" class="btn-primary">Próximo passo</button>
                                </div>
                            </div>

                            <div x-show="step === 3" x-cloak data-step="3" class="space-y-5">
                                <h2 class="text-lg font-bold text-white border-b border-slate-800 pb-3">Observações finais</h2>
                                <div>
                                    <label class="block text-xs font-bold uppercase tracking-wider mb-2" style="color:#94a3b8;">Detalhes adicionais</label>
                                    <textarea name="details" rows="6" class="w-full rounded-xl bg-slate-950/80 border border-slate-700 px-4 py-3 text-sm text-white focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none" placeholder="Indique horários ideais para contacto, detalhes da estrutura ou exigências especiais de segurança...">{{ old('details') }}</textarea>
                                </div>
                                <div class="rounded-xl border border-blue-500/20 bg-blue-500/10 p-4 flex gap-3">
                                    <svg class="h-5 w-5 text-blue-400 shrink-0 mt-0.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                                    </svg>
                                    <p class="text-xs leading-relaxed" style="color:#cbd5e1;">
                                        Ao enviar, autoriza a SecureVision a usar estes dados apenas para preparar e acompanhar o seu pedido de orçamento.
                                    </p>
                                </div>
                                <div class="flex justify-between pt-2 border-t border-slate-800">
                                    <button type="button" @click="step = 2" class="btn-secondary">Voltar</button>
                                    <button type="submit" class="btn-primary">
                                        Enviar solicitação
                                        <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"/></svg>
                                    </button>
                                </div>
                            </div>
                        </form>
                    @endif
                </div>
            </div>
        </div>
    </section>
@endsection
