@extends('admin.layouts.app')

@section('pageTitle', 'Generate Certificate')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">
                        <i class="fas fa-certificate"></i>
                        Generate IMT Montessori Certificate
                    </h3>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.certificates.generate.store') }}" method="POST" id="certificateForm">
                        @csrf

                        <div class="row">
                            <!-- Student Selection -->
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="student_id" class="form-label">Select Student *</label>
                                    <select name="student_id" id="student_id" class="form-control" required>
                                        <option value="">Choose a student...</option>
                                        @foreach($students as $student)
                                            <option value="{{ $student->id }}"
                                                    data-name="{{ $student->full_name }}"
                                                    data-age="{{ $student->age ?? '' }}"
                                                    data-type="{{ $student->student_type ?? 'normal' }}">
                                                {{ $student->full_name }}
                                                @if($student->email) ({{ $student->email }}) @endif
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <!-- Template Selection -->
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="template_id" class="form-label">Select Template *</label>
                                    <select name="template_id" id="template_id" class="form-control" required>
                                        <option value="">Choose a template...</option>
                                        @foreach($templates as $template)
                                            <option value="{{ $template->id }}"
                                                    data-width="{{ $template->canvas_width }}"
                                                    data-height="{{ $template->canvas_height }}">
                                                {{ $template->name }}
                                                @if($template->description) - {{ $template->description }} @endif
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>

                        <hr class="my-4">

                        <!-- Student Information -->
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="student_name" class="form-label">Student Name *</label>
                                    <input type="text" name="student_name" id="student_name" class="form-control" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="student_age" class="form-label">Student Age *</label>
                                    <input type="text" name="student_age" id="student_age" class="form-control" required>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="student_type" class="form-label">Student Type *</label>
                                    <select name="student_type" id="student_type" class="form-control" required>
                                        <option value="normal">Normal Student</option>
                                        <option value="special">Special Student</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="completion_date" class="form-label">Completion Date *</label>
                                    <input type="date" name="completion_date" id="completion_date" class="form-control" required>
                                </div>
                            </div>
                        </div>

                        <hr class="my-4">

                        <!-- Additional Information -->
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="course_name" class="form-label">Course Name</label>
                                    <input type="text" name="course_name" id="course_name" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="instructor_name" class="form-label">Instructor Name</label>
                                    <input type="text" name="instructor_name" id="instructor_name" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="organization_name" class="form-label">Organization Name</label>
                                    <input type="text" name="organization_name" id="organization_name" class="form-control">
                                </div>
                            </div>
                        </div>

                        <!-- Template Preview -->
                        <div class="row mt-4" id="templatePreview" style="display: none;">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">
                                            <i class="fas fa-eye"></i>
                                            Template Preview
                                        </h5>
                                    </div>
                                    <div class="card-body text-center">
                                        <div id="previewContainer" class="border rounded p-3">
                                            <p class="text-muted">Template preview will appear here</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row mt-4">
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary btn-lg">
                                    <i class="fas fa-magic"></i>
                                    Generate Certificate
                                </button>
                                <a href="{{ route('admin.certificates.imtmontessori') }}" class="btn btn-secondary btn-lg ml-2">
                                    <i class="fas fa-arrow-left"></i>
                                    Back to Certificates
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Success Modal -->
<div class="modal fade" id="successModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title">
                    <i class="fas fa-check-circle"></i>
                    Certificate Generated Successfully!
                </h5>
                <button type="button" class="close text-white" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>The certificate has been generated and saved to the database.</p>
                <p><strong>Student:</strong> <span id="modalStudentName"></span></p>
                <p><strong>Template:</strong> <span id="modalTemplateName"></span></p>
            </div>
            <div class="modal-footer">
                                            <a href="{{ route('admin.certificates.imtmontessori') }}" class="btn btn-primary">
                    View All Certificates
                </a>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Generate Another</button>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
.form-group {
    margin-bottom: 1.5rem;
}

.form-label {
    font-weight: 600;
    color: #495057;
}

.card {
    box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    border: 1px solid rgba(0, 0, 0, 0.125);
}

.card-header {
    background-color: #f8f9fa;
    border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}

.btn-lg {
    padding: 0.75rem 1.5rem;
    font-size: 1.1rem;
}

#templatePreview {
    background-color: #f8f9fa;
    border-radius: 0.375rem;
}

#previewContainer {
    min-height: 200px;
    background-color: white;
}
</style>
@endpush

@push('scripts')
<script>
$(document).ready(function() {
    // Auto-fill student data when student is selected
    $('#student_id').change(function() {
        const selectedOption = $(this).find('option:selected');
        if (selectedOption.val()) {
            $('#student_name').val(selectedOption.data('name'));
            $('#student_age').val(selectedOption.data('age'));
            $('#student_type').val(selectedOption.data('type'));
        }
    });

    // Show template preview when template is selected
    $('#template_id').change(function() {
        const selectedOption = $(this).find('option:selected');
        if (selectedOption.val()) {
            const width = selectedOption.data('width');
            const height = selectedOption.data('height');

            if (width && height) {
                $('#previewContainer').html(`
                    <div class="alert alert-info">
                        <strong>Template Dimensions:</strong> ${width} × ${height} pixels<br>
                        <strong>Template:</strong> ${selectedOption.text()}
                    </div>
                `);
                $('#templatePreview').show();
            }
        } else {
            $('#templatePreview').hide();
        }
    });

    // Set default completion date to today
    $('#completion_date').val(new Date().toISOString().split('T')[0]);

    // Form validation
    $('#certificateForm').submit(function(e) {
        const studentId = $('#student_id').val();
        const templateId = $('#template_id').val();

        if (!studentId || !templateId) {
            e.preventDefault();
            alert('Please select both a student and a template.');
            return false;
        }
    });

    // Show success message if redirected with success
    @if(session('success'))
        $('#modalStudentName').text('{{ session("success") }}');
        $('#modalTemplateName').text($('#template_id option:selected').text());
        $('#successModal').modal('show');
    @endif
});
</script>
@endpush
