@extends('layouts.admin')

@section('title', 'Edit Email Template')

@section('admin-content')
<div class="container-fluid px-0">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">Edit Template: {{ $emailTemplate->name }}</h1>
        <a href="{{ route('admin.email_templates.index') }}" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-arrow-left me-1"></i> Back to List
        </a>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <form action="{{ route('admin.email_templates.update', $emailTemplate->id) }}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="mb-3">
                            <label class="form-label">Subject</label>
                            <input type="text" name="subject" class="form-control" value="{{ old('subject', $emailTemplate->subject) }}" required>
                            <div class="form-text">Available variables: {{ implode(', ', array_map(fn($v) => '{'.$v.'}', $emailTemplate->variables ?? [])) }}</div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Body (HTML)</label>
                            <textarea name="body" id="email-body-editor" class="form-control" rows="15">{{ old('body', $emailTemplate->body) }}</textarea>
                            <div class="form-text">You can use HTML tags and the variables listed above.</div>
                        </div>

                        <div class="mb-3 form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="isActive" name="is_active" value="1" {{ $emailTemplate->is_active ? 'checked' : '' }}>
                            <label class="form-check-label" for="isActive">Active</label>
                        </div>

                        <div class="text-end">
                            <button type="submit" class="btn btn-primary">Save Changes</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm bg-light">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">Available Variables</h6>
                    <ul class="list-group list-group-flush bg-transparent">
                        @foreach($emailTemplate->variables ?? [] as $variable)
                            <li class="list-group-item bg-transparent px-0 py-2">
                                <code class="text-primary fw-bold">{<span>{{ $variable }}</span>}</code>
                            </li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Summernote -->
<link href="{{ asset('css/summernote-lite.min.css') }}" rel="stylesheet">

@push('scripts')
<script src="{{ asset('js/summernote-lite.min.js') }}"></script>
<script>
    $(document).ready(function() {
        // Force remove TinyMCE if present
        if (typeof tinymce !== 'undefined') {
            tinymce.remove();
        }

        $('#email-body-editor').summernote({
            placeholder: 'Write your email content here...',
            tabsize: 2,
            height: 400,
            toolbar: [
                ['style', ['style']],
                ['font', ['bold', 'underline', 'clear']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['table', ['table']],
                ['insert', ['link', 'picture', 'video']],
                ['view', ['fullscreen', 'codeview', 'help']]
            ]
        });
    });
</script>
@endpush
@endsection
