@extends('layouts.app')

@section('title', 'Generate Letter')
@section('heading', 'Generate Letter')

@section('content')
@include('documents.top-submenu')
    <x-card class="max-w-3xl">
        <form action="{{ route('generated-letters.store') }}" method="post" class="space-y-6">
            @csrf

            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label class="block text-sm font-medium text-slate-700">Employee <span class="text-red-500">*</span></label>
                    <select name="user_id" required class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2 js-select2-search">
                        <option value="">Select employee</option>
                        @foreach($employees as $employee)
                            <option value="{{ $employee->id }}" {{ (int) old('user_id', $prefill['user_id']) === (int) $employee->id ? 'selected' : '' }}>{{ $employee->name }}</option>
                        @endforeach
                    </select>
                    @error('user_id')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">Letter type <span class="text-red-500">*</span></label>
                    <select name="letter_type" required class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                        @foreach($types as $type)
                            <option value="{{ $type }}" {{ old('letter_type', $prefill['letter_type']) === $type ? 'selected' : '' }}>
                                {{ match($type) {
                                    'relieving' => 'Relieving letter',
                                    'experience' => 'Experience certificate',
                                    'warning' => 'Warning letter',
                                    'offer' => 'Offer letter',
                                    default => ucfirst($type),
                                } }}
                            </option>
                        @endforeach
                    </select>
                    @error('letter_type')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">Letterhead template</label>
                    <select name="letter_head_template_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                        <option value="">Default (company name header)</option>
                        @foreach($templates as $template)
                            <option value="{{ $template->id }}" {{ (int) old('letter_head_template_id', $prefill['letter_head_template_id']) === (int) $template->id ? 'selected' : '' }}>{{ $template->name }}</option>
                        @endforeach
                    </select>
                    @error('letter_head_template_id')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">Last working date override</label>
                    <input type="date" name="last_working_date" value="{{ old('last_working_date', $prefill['last_working_date']) }}" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                    <p class="mt-1 text-xs text-slate-500">Optional. Uses employee record if blank.</p>
                    @error('last_working_date')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
            </div>

            <div>
                <label class="block text-sm font-medium text-slate-700">Subject</label>
                <input type="text" name="subject" value="{{ old('subject') }}" placeholder="Auto-generated if left blank" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                @error('subject')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-slate-700">Body HTML</label>
                <textarea name="body_html" rows="10" placeholder="Leave blank to use the default template for the selected letter type. Placeholders: Employee Name, Employee Id, Designation, Department, Joining Date, Last Working Date, Company Name, Today Date" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 font-mono text-sm">{{ old('body_html') }}</textarea>
                @error('body_html')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
            </div>

            <div class="flex gap-2">
                <x-button type="submit" variant="primary">Generate &amp; preview</x-button>
                <a href="{{ route('generated-letters.index') }}" class="inline-flex items-center rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-200">Cancel</a>
            </div>
        </form>
    </x-card>
@endsection
