@php
    $canSale = $canSale ?? ($course->canSale() and !$hasBought);
    $enrollFeatures = $course->features()->take(5)->get();
@endphp

<div class="cp-enroll" id="enroll">
    <div class="cp-enroll__card">
        <div class="cp-enroll__header d-none d-lg-flex">
            <span class="cp-enroll__header-icon" aria-hidden="true">🎓</span>
            <span class="cp-enroll__header-title">{{ trans('webinars.landing_enroll_card_title') }}</span>
        </div>
        <div class="cp-enroll__cover d-none d-lg-block">
            @include('web.default.includes.premium.responsive_image', [
                'src' => $course->getImage(),
                'alt' => $course->title,
                'class' => 'img-cover',
                'width' => 640,
                'height' => 360,
                'loading' => 'lazy',
            ])
        </div>

        <div class="cp-enroll__body px-20 pb-30 pt-20 pt-lg-0">
            @if($enrollFeatures->count() > 0)
                <ul class="cp-enroll__benefits">
                    @foreach($enrollFeatures as $f)
                        <li>
                            <span class="cp-enroll__check" aria-hidden="true">✓</span>
                            <span>{{ $f->description }}</span>
                        </li>
                    @endforeach
                </ul>
            @endif

            <form action="/cart/store" method="post" class="cp-enroll__form">
                {{ csrf_field() }}
                <input type="hidden" name="item_id" value="{{ $course->id }}">
                <input type="hidden" name="item_name" value="webinar_id">
                <input type="hidden" name="webinar_id" value="{{ $course->id }}">

                @if(!empty($course->tickets))
                    <div class="d-none d-lg-block">
                        @foreach($course->tickets as $ticket)
                            <div class="form-check mt-15">
                                <input class="form-check-input" @if(!$ticket->isValid()) disabled @endif type="radio"
                                       data-discount="{{ $ticket->discount }}"
                                       value="{{ ($ticket->isValid()) ? $ticket->id : '' }}"
                                       name="ticket_id" id="courseOff{{ $ticket->id }}">
                                <label class="form-check-label d-flex flex-column cursor-pointer" for="courseOff{{ $ticket->id }}">
                                    <span class="font-16 font-weight-500 text-dark-blue">{{ $ticket->title }} @if(!empty($ticket->discount)) ({{ $ticket->discount }}% {{ trans('public.off') }}) @endif</span>
                                    <span class="font-14 text-gray">{{ $ticket->getSubTitle() }}</span>
                                </label>
                            </div>
                        @endforeach
                    </div>
                @endif

                @if($course->price > 0)
                    @php
                        $realPrice = handleCoursePagePrice($course->price, $course);
                        $specialOfferPercent = !empty($activeSpecialOffer) ? (int) $activeSpecialOffer->percent : 0;
                        $bestTicketDiscount = 0;
                        if (!empty($course->tickets)) {
                            foreach ($course->tickets as $ticket) {
                                if ($ticket->isValid() && (int) $ticket->discount > $bestTicketDiscount) {
                                    $bestTicketDiscount = (int) $ticket->discount;
                                }
                            }
                        }
                        $previewDiscountPercent = $specialOfferPercent + $bestTicketDiscount;
                        $hasGiftDiscount = $previewDiscountPercent > 0;
                        $previewDiscountedAmount = $course->price - ($course->price * $previewDiscountPercent / 100);
                        $priceWithDiscount = $hasGiftDiscount
                            ? handleCoursePagePrice($previewDiscountedAmount, $course)
                            : null;
                    @endphp
                    <div id="priceBox"
                         class="cp-enroll__price mt-15 mt-lg-20 {{ $hasGiftDiscount ? 'cp-enroll__price--gift' : '' }}"
                         data-formatted-base="{{ $realPrice['price'] }}"
                         data-formatted-discount="{{ $hasGiftDiscount ? $priceWithDiscount['price'] : '' }}">
                        @if($hasGiftDiscount)
                            <div class="cp-gift-price" id="cpGiftPrice">
                                <div class="cp-gift-price__base-row text-center">
                                    <span id="realPrice"
                                          data-value="{{ $course->price }}"
                                          data-special-offer="{{ $specialOfferPercent }}"
                                          class="cp-enroll__price-main cp-gift-price__original">
                                        {{ $realPrice['price'] }}
                                    </span>
                                </div>
                                <button type="button"
                                        class="cp-gift-price__trigger"
                                        aria-expanded="false"
                                        aria-controls="cpGiftPriceReveal"
                                        data-gift-label="{{ trans('webinars.landing_gift_tap') }}">
                                    <div class="cp-gift-price__box">
                                        <div class="cp-gift-price__sparkles" aria-hidden="true">
                                            <span></span><span></span><span></span>
                                        </div>
                                        <div class="cp-gift-price__lid">
                                            <span class="cp-gift-price__bow" aria-hidden="true"></span>
                                        </div>
                                        <div class="cp-gift-price__body">
                                            <span class="cp-gift-price__emoji" aria-hidden="true">🎁</span>
                                            <span class="cp-gift-price__percent">{{ $previewDiscountPercent }}%</span>
                                            <span class="cp-gift-price__hint">{{ trans('webinars.landing_gift_tap') }}</span>
                                        </div>
                                    </div>
                                </button>
                                <div class="cp-gift-price__reveal" id="cpGiftPriceReveal" hidden>
                                    <div class="cp-enroll__price-row text-center">
                                        <span id="priceWithDiscount" class="cp-enroll__price-new cp-gift-price__new">
                                            {{ $priceWithDiscount['price'] }}
                                        </span>
                                    </div>
                                    @if(!empty($priceWithDiscount['tax']))
                                        <span class="cp-gift-price__tax d-block font-14 text-gray text-center">+ {{ $priceWithDiscount['tax'] }} tax</span>
                                    @endif
                                </div>
                            </div>
                        @else
                            <div class="cp-enroll__price-row text-center">
                                <span id="realPrice"
                                      data-value="{{ $course->price }}"
                                      data-special-offer="{{ $specialOfferPercent }}"
                                      class="cp-enroll__price-main">
                                    {{ $realPrice['price'] }}
                                </span>
                                <span id="priceWithDiscount" class="cp-enroll__price-new d-none"></span>
                            </div>
                            @if(!empty($realPrice['tax']))
                                <span class="d-block font-14 text-gray text-center">+ {{ $realPrice['tax'] }} tax</span>
                            @endif
                        @endif
                    </div>
                @else
                    <div class="cp-enroll__price mt-15 mt-lg-20 text-center">
                        <span class="cp-enroll__price-main">{{ trans('public.free') }}</span>
                    </div>
                @endif

                <div class="cp-enroll__actions mt-15 mt-lg-20">
                    @if($hasBought)
                        <a href="{{ $course->getLearningPageUrl() }}" class="btn btn-block cp-btn cp-btn--primary">{{ trans('update.browse_diploma') }}</a>
                    @elseif($course->price > 0)
                        @if(!$canSale)
                            <button type="button" class="btn btn-primary btn-block disabled {{ $course->cantSaleStatus($hasBought) }}">
                                {{ trans('update.disabled_add_to_cart') }}
                            </button>
                        @else
                            <button type="button" class="btn btn-block cp-btn cp-btn--primary js-course-direct-payment">
                                {{ trans('update.buy_course') }}
                            </button>
                            <button type="button" class="btn btn-outline-primary btn-block mt-15 d-none d-lg-block js-course-add-to-cart-btn">
                                {{ trans('update.add_to_cart') }}
                            </button>
                        @endif
                        @if($canSale and $course->subscribe)
                            <a href="/subscribes/apply/{{ $course->slug }}" class="btn btn-outline-primary btn-block btn-subscribe mt-15 d-none d-lg-block">{{ trans('public.subscribe') }}</a>
                        @endif
                        @if($canSale and !empty($course->points))
                            <a href="{{ !(auth()->check()) ? '/login' : '#' }}" class="{{ (auth()->check()) ? 'js-buy-with-point' : '' }} btn btn-outline-warning btn-block mt-15 d-none d-lg-block" rel="nofollow">
                                {!! trans('update.buy_with_n_points', ['points' => $course->points]) !!}
                            </a>
                        @endif
                    @else
                        <a href="{{ $canSale ? '/course/'. $course->slug .'/free' : '#' }}" class="btn btn-primary btn-block {{ (!$canSale) ? (' disabled ' . $course->cantSaleStatus($hasBought)) : '' }}">
                            {{ trans('public.enroll_on_webinar') }}
                        </a>
                    @endif
                </div>
            </form>

            @if(!empty(getOthersPersonalizationSettings('show_guarantee_text')) and getOthersPersonalizationSettings('show_guarantee_text'))
                <p class="cp-enroll__trust mt-15 d-none d-lg-flex">
                    <i data-feather="shield" width="16" height="16" aria-hidden="true"></i>
                    {{ getOthersPersonalizationSettings('guarantee_text') }}
                </p>
            @endif

            <div class="cp-enroll__tools mt-20 d-none d-lg-flex">
                @if($course->isWebinar())
                    <a href="{{ $course->addToCalendarLink() }}" target="_blank" class="cp-enroll__tool" title="{{ trans('public.reminder') }}">
                        <i data-feather="calendar" width="18" height="18"></i>
                    </a>
                @endif
                <a href="/favorites/{{ $course->slug }}/toggle" id="favoriteToggle" class="cp-enroll__tool" title="{{ trans('panel.favorite') }}">
                    <i data-feather="heart" class="{{ !empty($isFavorite) ? 'favorite-active' : '' }}" width="18" height="18"></i>
                </a>
                <a href="#" class="js-share-course cp-enroll__tool" title="{{ trans('public.share') }}">
                    <i data-feather="share-2" width="18" height="18"></i>
                </a>
            </div>

            <div class="cp-enroll__tools cp-enroll__tools--mobile d-flex d-lg-none mt-10">
                <a href="/favorites/{{ $course->slug }}/toggle" class="cp-enroll__tool btn btn-primary flex-fill mx-1">
                    <i data-feather="heart" class="{{ !empty($isFavorite) ? 'favorite-active' : '' }}" width="18" height="18"></i>
                </a>
                <a href="#" class="js-share-course cp-enroll__tool btn btn-primary flex-fill mx-1">
                    <i data-feather="share-2" width="18" height="18"></i>
                </a>
            </div>

            <div class="mt-15 text-center d-none d-lg-block">
                <button type="button" id="webinarReportBtn" class="font-14 text-gray btn-transparent">{{ trans('webinars.report_this_webinar') }}</button>
            </div>
        </div>
    </div>
</div>
