@extends('layouts.admin')

@section('title', 'Create 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">Create New Template</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="card border-0 shadow-sm">
        <div class="card-body">
            <form action="{{ route('admin.email_templates.store') }}" method="POST">
                @csrf

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Template Name</label>
                        <input type="text" name="name" class="form-control" value="{{ old('name') }}" required placeholder="e.g. Order Confirmation">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Slug (Unique Identifier)</label>
                        <input type="text" name="slug" class="form-control" value="{{ old('slug') }}" required placeholder="e.g. order_confirmation">
                        <div class="form-text">Used in code to identify this template.</div>
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Subject</label>
                    <input type="text" name="subject" class="form-control" value="{{ old('subject') }}" required>
                </div>

                <div class="mb-3">
                    <label class="form-label">Body</label>
                    <textarea name="body" id="email-body-editor" class="form-control" rows="15">{{ old('body') }}</textarea>
                </div>

                <div class="text-end">
                    <button type="submit" class="btn btn-primary">Create Template</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Summernote -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/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 (defensive)
        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
