@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h3 class="card-title">Create New Template</h3>
                        <a href="{{ route('admin.template-builder.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Templates
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    @if($errors->any())
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form action="{{ route('admin.template-builder.store') }}" method="POST" enctype="multipart/form-data">
                        @csrf

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="name">Template Name *</label>
                                    <input type="text" name="name" id="name" class="form-control" required
                                           value="{{ old('name') }}" placeholder="Enter template name">
                                </div>

                                <div class="form-group">
                                    <label for="description">Description</label>
                                    <textarea name="description" id="description" class="form-control" rows="3"
                                              placeholder="Enter template description">{{ old('description') }}</textarea>
                                </div>

                                <div class="form-group">
                                    <label for="category">Category</label>
                                    <select name="category" id="category" class="form-control">
                                        <option value="general" {{ old('category') == 'general' ? 'selected' : '' }}>General</option>
                                        <option value="academic" {{ old('category') == 'academic' ? 'selected' : '' }}>Academic</option>
                                        <option value="professional" {{ old('category') == 'professional' ? 'selected' : '' }}>Professional</option>
                                        <option value="training" {{ old('category') == 'training' ? 'selected' : '' }}>Training</option>
                                        <option value="certification" {{ old('category') == 'certification' ? 'selected' : '' }}>Certification</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="background_image">Background Image *</label>
                                    <input type="file" name="background_image" id="background_image" class="form-control-file"
                                           accept="image/*" required>
                                    <small class="form-text text-muted">
                                        Upload a high-quality image (JPG, PNG, GIF). Recommended size: 800x600px or larger.
                                    </small>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="width">Width (pixels)</label>
                                    <input type="number" name="width" id="width" class="form-control"
                                           value="{{ old('width', 800) }}" min="100" max="3000">
                                </div>

                                <div class="form-group">
                                    <label for="height">Height (pixels)</label>
                                    <input type="number" name="height" id="height" class="form-control"
                                           value="{{ old('height', 600) }}" min="100" max="3000">
                                </div>

                                <div class="form-group">
                                    <label>Template Fields</label>
                                    <div class="border rounded p-3 bg-light">
                                        <p class="text-muted mb-2">
                                            <i class="fas fa-info-circle"></i>
                                            Default fields will be automatically added to your template:
                                        </p>
                                        <ul class="list-unstyled mb-0">
                                            <li><i class="fas fa-check text-success"></i> Student Name</li>
                                            <li><i class="fas fa-check text-success"></i> Course Name</li>
                                            <li><i class="fas fa-check text-success"></i> Graduation Date</li>
                                            <li><i class="fas fa-check text-success"></i> Serial Number</li>
                                            <li><i class="fas fa-check text-success"></i> Instructor Name</li>
                                            <li><i class="fas fa-check text-success"></i> Organization Name</li>
                                        </ul>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label>Template Data (JSON)</label>
                                    @verbatim
                                    <textarea name="template_data" id="template_data" class="form-control" rows="8" readonly>
{
    "fields": [
        {
            "id": "student_name",
            "type": "text",
            "label": "Student Name",
            "value": "{{student_name}}",
            "x": 50,
            "y": 50,
            "fontSize": 24,
            "fontFamily": "Arial",
            "color": "#000000",
            "textAlign": "center",
            "fontWeight": "bold",
            "fontStyle": "normal",
            "opacity": 100,
            "editable": false,
            "required": true
        },
        {
            "id": "course_name",
            "type": "text",
            "label": "Course Name",
            "value": "{{course_name}}",
            "x": 50,
            "y": 30,
            "fontSize": 18,
            "fontFamily": "Arial",
            "color": "#000000",
            "textAlign": "center",
            "fontWeight": "normal",
            "fontStyle": "normal",
            "opacity": 100,
            "editable": false,
            "required": false
        },
        {
            "id": "graduation_date",
            "type": "text",
            "label": "Graduation Date",
            "value": "{{graduation_date}}",
            "x": 50,
            "y": 70,
            "fontSize": 16,
            "fontFamily": "Arial",
            "color": "#000000",
            "textAlign": "center",
            "fontWeight": "normal",
            "fontStyle": "normal",
            "opacity": 100,
            "editable": false,
            "required": true
        },
        {
            "id": "serial_number",
            "type": "text",
            "label": "Serial Number",
            "value": "{{serial_number}}",
            "x": 50,
            "y": 85,
            "fontSize": 14,
            "fontFamily": "Arial",
            "color": "#666666",
            "textAlign": "center",
            "fontWeight": "normal",
            "fontStyle": "normal",
            "opacity": 100,
            "editable": false,
            "required": false
        }
    ],
    "settings": {
        "backgroundColor": "#ffffff",
        "borderColor": "#000000",
        "borderWidth": 0,
        "borderStyle": "solid"
    }
}
                                    </textarea>
                                    @endverbatim
                                    <small class="form-text text-muted">
                                        This contains the default field configuration. You can edit this later in the template builder.
                                    </small>
                                </div>
                            </div>
                        </div>

                        <div class="row mt-4">
                            <div class="col-12">
                                <div class="form-group">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save"></i> Create Template
                                    </button>
                                    <a href="{{ route('admin.template-builder.index') }}" class="btn btn-secondary ml-2">
                                        Cancel
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Auto-detect image dimensions when file is selected
    $('#background_image').change(function() {
        const file = this.files[0];
        if (file) {
            const img = new Image();
            img.onload = function() {
                $('#width').val(this.width);
                $('#height').val(this.height);
            };
            img.src = URL.createObjectURL(file);
        }
    });

    // Format the JSON textarea
    const textarea = document.getElementById('template_data');
    const jsonContent = textarea.value;
    try {
        const formatted = JSON.stringify(JSON.parse(jsonContent), null, 4);
        textarea.value = formatted;
    } catch (e) {
        console.log('JSON is already formatted');
    }
});
</script>
@endpush
