@extends(getTemplate() .'.panel.layouts.panel_layout')

@push('styles_top')
    <link rel="stylesheet" href="/assets/default/vendors/daterangepicker/daterangepicker.min.css">
@endpush

@section('content')

    <section class="mt-25">
        <h2 class="section-title">{{ trans('quiz.filter_certificates') }}</h2>

        <div class="panel-section-card py-20 px-25 mt-20">
            <form action="" method="get" class="row">
                <div class="col-12 col-lg-6">
                    <div class="row">
                        <div class="col-12 col-md-6">
                            <div class="form-group">
                                <label class="input-label">{{ trans('public.from') }}</label>
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text" id="dateInputGroupPrepend">
                                            <i data-feather="calendar" width="18" height="18" class="text-white"></i>
                                        </span>
                                    </div>
                                    <input type="text" name="from" autocomplete="off" class="form-control @if(!empty(request()->get('from'))) datepicker @else datefilter @endif" value="{{ request()->get('from','') }}" aria-describedby="dateInputGroupPrepend"/>
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="form-group">
                                <label class="input-label">{{ trans('public.to') }}</label>
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text" id="dateInputGroupPrepend">
                                            <i data-feather="calendar" width="18" height="18" class="text-white"></i>
                                        </span>
                                    </div>
                                    <input type="text" name="to" autocomplete="off" class="form-control @if(!empty(request()->get('to'))) datepicker @else datefilter @endif" value="{{ request()->get('to','') }}" aria-describedby="dateInputGroupPrepend"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-12 col-lg-4">
                    <div class="form-group">
                        <label class="input-label">{{ trans('product.course') }}</label>
                        <select name="webinar_id" class="form-control">
                            <option value="all">{{ trans('webinars.all_courses') }}</option>

                            @foreach($userWebinars as $userWebinar)
                                <option value="{{ $userWebinar->id }}" @if(request()->get('webinar_id','') == $userWebinar->id) selected @endif>{{ $userWebinar->title }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-12 col-lg-2 d-flex align-items-center justify-content-end">
                    <button type="submit" class="btn btn-sm btn-primary w-100 mt-2">{{ trans('public.show_results') }}</button>
                </div>
            </form>
            <p class="text-gray font-13 mt-15 mb-0">{{ trans('quiz.certificate_unified_hint') }}</p>
        </div>
    </section>

    <section class="mt-35">
        <div class="d-flex align-items-start align-items-md-center justify-content-between flex-column flex-md-row">
            <h2 class="section-title">{{ trans('quiz.my_certificates') }}</h2>
        </div>

        @if(!empty($filteredOutAllCerts) && $filteredOutAllCerts)
            @include(getTemplate() . '.includes.no-result',[
                'file_name' => 'cert.png',
                'title' => trans('quiz.my_course_certificates_filter_no_match'),
                'hint' => nl2br(trans('quiz.my_course_certificates_filter_no_match_hint')),
            ])
        @elseif(!empty($allCertificates) && $allCertificates->total() > 0)
            <div class="panel-section-card py-20 px-25 mt-20">
                <div class="row">
                    <div class="col-12 ">
                        <div class="table-responsive">
                            <table class="table text-center custom-table">
                                <thead>
                                <tr>
                                    <th>{{ trans('quiz.certificate_column_type') }}</th>
                                    <th class="text-left">{{ trans('quiz.certificate_column_subject') }}</th>
                                    <th class="text-center">{{ trans('quiz.certificate_column_grade_or_sn') }}</th>
                                    <th class="text-center">{{ trans('public.certificate_id') }}</th>
                                    <th class="text-center">{{ trans('public.date') }}</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($allCertificates as $cert)
                                    @php
                                        $ctype = $cert->certificate_type ?? 'old';
                                    @endphp
                                    <tr>
                                        <td class="align-middle">
                                            @if($ctype === 'old')
                                                @if($cert->type === 'course')
                                                    <span class="badge badge-success">{{ trans('quiz.certificate_kind_course') }}</span>
                                                @elseif($cert->type === 'quiz')
                                                    <span class="badge badge-primary">{{ trans('quiz.certificate_kind_quiz') }}</span>
                                                @elseif($cert->type === 'imtmontessori')
                                                    <span class="badge badge-warning">{{ trans('quiz.certificate_kind_montessori') }}</span>
                                                @else
                                                    <span class="badge badge-secondary">{{ $cert->type }}</span>
                                                @endif
                                            @elseif($ctype === 'new')
                                                <span class="badge badge-info">{{ trans('quiz.certificate_kind_template') }}</span>
                                            @elseif($ctype === 'last')
                                                <span class="badge badge-dark">{{ trans('quiz.certificate_kind_generated') }}</span>
                                            @endif
                                        </td>
                                        <td class="text-left align-middle">
                                            @if($ctype === 'old')
                                                @if($cert->type === 'quiz' && !empty($cert->quiz))
                                                    <span class="d-block text-dark-blue font-weight-500">{{ $cert->quiz->webinar ? $cert->quiz->webinar->title : $cert->quiz->title }}</span>
                                                @elseif($cert->webinar)
                                                    <span class="d-block text-dark-blue font-weight-500">{{ $cert->webinar->title }}</span>
                                                @else
                                                    <span class="text-gray">—</span>
                                                @endif
                                            @elseif($ctype === 'new')
                                                <span class="d-block text-dark-blue font-weight-500">{{ $cert->course_name ?? optional($cert->template)->title ?? '—' }}</span>
                                            @elseif($ctype === 'last')
                                                <span class="d-block text-dark-blue font-weight-500">{{ $cert->template_name ?? trans('quiz.certificate_kind_generated') }}</span>
                                            @endif
                                        </td>
                                        <td class="align-middle">
                                            @if($ctype === 'old')
                                                @if(!empty($cert->user_grade))
                                                    {{ $cert->user_grade }}%
                                                @elseif(!empty($cert->serial_number))
                                                    {{ $cert->serial_number }}
                                                @else
                                                    —
                                                @endif
                                            @elseif($ctype === 'new')
                                                {{ $cert->serial_number ?? '—' }}
                                            @else
                                                —
                                            @endif
                                        </td>
                                        <td class="align-middle">{{ $cert->id }}</td>
                                        <td class="align-middle">
                                            @if($ctype === 'old')
                                                <span class="text-dark-blue font-weight-500">{{ dateTimeFormat($cert->created_at, 'j M Y') }}</span>
                                            @elseif($ctype === 'new')
                                                @if(!empty($cert->completion_date))
                                                    <span class="text-dark-blue font-weight-500">{{ $cert->completion_date instanceof \Carbon\Carbon ? dateTimeFormat($cert->completion_date->timestamp, 'j M Y') : dateTimeFormat(strtotime((string)$cert->completion_date), 'j M Y') }}</span>
                                                @else
                                                    —
                                                @endif
                                            @elseif($ctype === 'last')
                                                @if(!empty($cert->generated_at))
                                                    <span class="text-dark-blue font-weight-500">{{ dateTimeFormat($cert->generated_at instanceof \Carbon\Carbon ? $cert->generated_at->timestamp : strtotime((string)$cert->generated_at), 'j M Y') }}</span>
                                                @else
                                                    —
                                                @endif
                                            @endif
                                        </td>
                                        <td class="align-middle font-weight-normal">
                                            @if($ctype === 'old')
                                                @if($cert->type === 'quiz' && !empty($cert->quiz_result_id))
                                                    <a href="/panel/quizzes/results/{{ $cert->quiz_result_id }}/showCertificate" target="_blank" class="btn btn-sm btn-primary">{{ trans('quiz.certificate_download') }}</a>
                                                @elseif($cert->type === 'course' && !empty($cert->webinar_id))
                                                    <a href="/panel/certificates/webinars/{{ $cert->id }}/show" target="_blank" class="btn btn-sm btn-primary">{{ trans('quiz.certificate_download') }}</a>
                                                @elseif($cert->type === 'imtmontessori')
                                                    <a href="/imtmontessori-certificates/download/{{ $cert->id }}" target="_blank" class="btn btn-sm btn-primary">{{ trans('quiz.certificate_download') }}</a>
                                                @else
                                                    <span class="text-gray font-12">{{ trans('quiz.certificate_pending_file') }}</span>
                                                @endif
                                            @elseif($ctype === 'new')
                                                @if(!empty($cert->generated_image_path))
                                                    <a href="/new-certificates/download/{{ $cert->id }}" target="_blank" class="btn btn-sm btn-primary">{{ trans('quiz.certificate_download') }}</a>
                                                @else
                                                    <span class="text-gray font-12">{{ trans('quiz.certificate_pending_file') }}</span>
                                                @endif
                                            @elseif($ctype === 'last')
                                                @if(!empty($cert->image_path))
                                                    <a href="/last-certificates/download/{{ $cert->id }}" target="_blank" class="btn btn-sm btn-primary">{{ trans('quiz.certificate_download') }}</a>
                                                @else
                                                    <span class="text-gray font-12">{{ trans('quiz.certificate_pending_file') }}</span>
                                                @endif
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        @else
            @include(getTemplate() . '.includes.no-result',[
                'file_name' => 'cert.png',
                'title' => trans('quiz.my_certificates_no_result'),
                'hint' => nl2br(trans('quiz.my_certificates_no_result_hint')),
            ])
        @endif
    </section>

    @if(!empty($allCertificates) && $allCertificates->total() > 0)
        <div class="my-30">
            {{ $allCertificates->appends(request()->input())->links('vendor.pagination.panel') }}
        </div>
    @endif

@endsection

@push('scripts_bottom')
    <script src="/assets/default/vendors/daterangepicker/daterangepicker.min.js"></script>

    <script src="/assets/default/js/panel/certificates.min.js"></script>
@endpush
