@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">Bulk Certificate Generation</h3>
                        <a href="{{ route('admin.modern-certificates.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Certificates
                        </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

                    <div class="row">
                        <div class="col-md-8">
                            <form action="{{ route('admin.modern-certificates.bulk-store') }}" method="POST" enctype="multipart/form-data">
                                @csrf

                                <div class="form-group">
                                    <label for="template_id">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 }}"
                                                    {{ old('template_id') == $template->id ? 'selected' : '' }}>
                                                {{ $template->name }} ({{ $template->category }})
                                            </option>
                                        @endforeach
                                    </select>
                                    <small class="form-text text-muted">
                                        Choose a template to use for all certificates
                                    </small>
                                </div>

                                <div class="form-group">
                                    <label for="csv_file">CSV File *</label>
                                    <input type="file" name="csv_file" id="csv_file" class="form-control-file"
                                           accept=".csv,.txt" required>
                                    <small class="form-text text-muted">
                                        Upload a CSV file with student data. File should be less than 5MB.
                                    </small>
                                </div>

                                <div class="form-group">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-upload"></i> Process CSV & Generate Certificates
                                    </button>
                                    <a href="{{ route('admin.modern-certificates.index') }}" class="btn btn-secondary ml-2">
                                        Cancel
                                    </a>
                                </div>
                            </form>
                        </div>

                        <div class="col-md-4">
                            <div class="card">
                                <div class="card-header">
                                    <h6 class="mb-0"><i class="fas fa-info-circle"></i> CSV Format Guide</h6>
                                </div>
                                <div class="card-body">
                                    <p class="text-muted small">
                                        Your CSV file should have the following columns:
                                    </p>
                                    <ul class="list-unstyled small">
                                        <li><strong>student_name</strong> - Required</li>
                                        <li><strong>student_email</strong> - Optional</li>
                                        <li><strong>student_id</strong> - Optional</li>
                                        <li><strong>course_name</strong> - Optional</li>
                                        <li><strong>graduation_date</strong> - Optional (YYYY-MM-DD format)</li>
                                        <li><strong>instructor_name</strong> - Optional</li>
                                        <li><strong>organization_name</strong> - Optional</li>
                                    </ul>

                                    <hr>

                                    <h6>Sample CSV:</h6>
                                    <pre class="small bg-light p-2 rounded">student_name,student_email,course_name,graduation_date
John Doe,john@example.com,Web Development,2024-01-15
Jane Smith,jane@example.com,Digital Marketing,2024-01-20</pre>

                                    <div class="mt-3">
                                        <a href="#" class="btn btn-sm btn-outline-primary" id="download_sample">
                                            <i class="fas fa-download"></i> Download Sample CSV
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Template Preview -->
                    <div class="row mt-4" id="template_preview" style="display: none;">
                        <div class="col-12">
                            <h6><i class="fas fa-eye"></i> Template Preview</h6>
                            <div class="border rounded p-3 bg-light">
                                <div id="template_info">
                                    <!-- Template information will be loaded here -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Template selection change
    $('#template_id').change(function() {
        const templateId = $(this).val();
        if (templateId) {
            // Show template preview
            $('#template_preview').show();
            loadTemplateInfo(templateId);
        } else {
            $('#template_preview').hide();
        }
    });

    // File size validation
    $('#csv_file').change(function() {
        const file = this.files[0];
        if (file) {
            const fileSize = file.size / 1024 / 1024; // Convert to MB
            if (fileSize > 5) {
                alert('File size must be less than 5MB');
                this.value = '';
            }
        }
    });

    // Download sample CSV
    $('#download_sample').click(function(e) {
        e.preventDefault();

        const csvContent = 'student_name,student_email,course_name,graduation_date,instructor_name,organization_name\n' +
                          'John Doe,john@example.com,Web Development,2024-01-15,Dr. Smith,IMT Academy\n' +
                          'Jane Smith,jane@example.com,Digital Marketing,2024-01-20,Prof. Johnson,IMT Academy\n' +
                          'Mike Wilson,mike@example.com,Data Science,2024-01-25,Dr. Brown,IMT Academy';

        const blob = new Blob([csvContent], { type: 'text/csv' });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'sample_certificates.csv';
        a.click();
        window.URL.revokeObjectURL(url);
    });

    function loadTemplateInfo(templateId) {
        // You can add AJAX call here to load template details
        $('#template_info').html(`
            <div class="row">
                <div class="col-md-6">
                    <p><strong>Template ID:</strong> ${templateId}</p>
                    <p><strong>Status:</strong> <span class="badge badge-success">Active</span></p>
                </div>
                <div class="col-md-6">
                    <p><strong>Category:</strong> General</p>
                    <p><strong>Fields:</strong> Student Name, Course, Date, etc.</p>
                </div>
            </div>
        `);
    }
});
</script>
@endpush
