@extends('layouts.app')
@section('title', $template->exists ? 'Edit Template' : 'Create Template')
@section('content')
    <x-page-head :title="$template->exists ? 'Edit Template' : 'Create Template'" subtitle="{{ $template->name ?? 'New template' }}">
        <x-slot:actions>
            <a href="{{ route('communication.email-templates.index') }}" class="btn btn-outline">Back</a>
        </x-slot:actions>
    </x-page-head>

    <x-panel>
        <form method="POST" action="{{ $template->exists ? route('communication.email-templates.update', $template) : route('communication.email-templates.store') }}">
            @csrf
            @if ($template->exists) @method('PUT') @endif
            <div class="form-grid-2">
                <div class="form-group">
                    <label class="form-label">Name</label>
                    <input class="input" name="name" value="{{ old('name', $template->name) }}" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Category</label>
                    <select class="select w-full" name="category" required>
                        @foreach ($categories as $cat)
                            <option value="{{ $cat['value'] }}" @selected(old('category', $template->category) === $cat['value'])>{{ $cat['label'] }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Subject</label>
                <input class="input" name="subject" value="{{ old('subject', $template->subject) }}" required>
            </div>
            <x-rich-editor name="body_html" :value="old('body_html', $template->body_html)" label="Body" height="320px" />
            <div class="form-group" style="margin-top:16px;">
                <label class="form-label">Header override (optional)</label>
                <textarea class="input" name="header_html" rows="2">{{ old('header_html', $template->header_html) }}</textarea>
            </div>
            <div class="form-group">
                <label class="form-label">Footer override (optional)</label>
                <textarea class="input" name="footer_html" rows="2">{{ old('footer_html', $template->footer_html) }}</textarea>
            </div>
            <div style="display:flex; gap:20px; margin:16px 0;">
                <label><input type="checkbox" name="is_active" value="1" @checked(old('is_active', $template->is_active ?? true))> Active</label>
                <label><input type="checkbox" name="is_default" value="1" @checked(old('is_default', $template->is_default))> Default for category</label>
            </div>
            <button type="submit" class="btn btn-primary">Save Template</button>
        </form>
    </x-panel>
@endsection
