@extends('layouts.app')

@section('title', 'Novo documento')
@section('page-title', 'Novo documento')

@section('content')
    @include('partials.flash')

    <form method="post" action="{{ route('documents.store') }}" class="mx-auto max-w-2xl space-y-5 rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
        @csrf
        <div>
            <label class="block text-sm font-medium">Pessoa</label>
            <select name="contact_id" required class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <option value="">Selecione…</option>
                @foreach ($contacts as $c)
                    <option value="{{ $c->id }}" @selected(old('contact_id') == $c->id)>{{ $c->display_name ?: $c->phone }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-sm font-medium">Negociação (opcional)</label>
            <select name="deal_id" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <option value="">—</option>
                @foreach ($deals as $d)
                    <option value="{{ $d->id }}" @selected(old('deal_id') == $d->id)>{{ $d->title }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-sm font-medium">Imóvel (opcional)</label>
            <select name="property_id" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <option value="">—</option>
                @foreach ($properties as $p)
                    <option value="{{ $p->id }}" @selected(old('property_id') == $p->id)>{{ $p->title ?: $p->internal_code }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-sm font-medium">Tipo de documento</label>
            <select name="document_type" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <option value="">Usar template selecionado</option>
                @foreach ($documentTypes as $type)
                    <option value="{{ $type->value }}" @selected(old('document_type') == $type->value)>{{ $type->label() }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-sm font-medium">Template</label>
            <select name="template_id" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <option value="">Padrão do tipo</option>
                @foreach ($templates as $t)
                    <option value="{{ $t->id }}" @selected(old('template_id') == $t->id || ($t->is_default && ! old('template_id')))>{{ $t->name }} ({{ $t->document_type->label() }})</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-sm font-medium">Valor</label>
            <input type="number" step="0.01" name="amount" value="{{ old('amount') }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
        </div>
        <div>
            <label class="block text-sm font-medium">Mensagem</label>
            <textarea name="intro_message" rows="3" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{{ old('intro_message', 'Segue documento para sua análise.') }}</textarea>
        </div>
        <label class="flex items-center gap-2 text-sm">
            <input type="checkbox" name="share_now" value="1" class="rounded border-slate-300 text-emerald-600" @checked(old('share_now', true))>
            Compartilhar no portal e enviar link pelo WhatsApp
        </label>
        <div class="flex justify-end gap-2">
            <a href="{{ route('documents.index') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm">Cancelar</a>
            <button class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white">Gerar documento</button>
        </div>
    </form>
@endsection
