@extends('layouts.client')
@section('title', 'Checkout')

@section('script')
    <script>
        window.onload = () => {
            "use strict";
            window.checkout__init = (function init() {
                $('form#discount').submit(function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    const discountCode = $(this).find('input[name=code]').val();
                    if (window.checkout__get_loading(this) || !discountCode) {
                        return
                    }
                    window.checkout__set_loading(this, true)
                    window.checkout__set_loading($('#payment__submit'), true)
                    $.ajax({
                            url: '/checkout/check-discount',
                            type: 'GET',
                            data: $(this).serialize(),
                        })
                        .done((response) => {
                            toastr.success(response?.message)
                            $(this).find('input').attr('disabled', true);
                            $(this).find('button')
                                .attr('disabled', true)
                                .text('Used');
                            $('#form__payment input[name=discount_code]').val(discountCode);
                            window.checkout__calc_total(discountCode)
                        })
                        .fail((response) => {
                            toastr.error(response?.responseJSON?.message)
                        })
                        .always(() => {
                            window.checkout__set_loading(this, false)
                            window.checkout__set_loading($('#payment__submit'), false)
                        })
                })
                return init
            })();
            window.checkout__calc_total = function(discountCode) {
                window.checkout__set_loading($('#payment__submit'), true)
                $.get('/checkout/calc-total', {
                    discount_code: discountCode,
                    package_id: '{{ $package->id }}'
                }).done((response) => {
                    // each fields need payment
                    for (let key in response.data) {
                        $(`[data-section=${key}]`).fadeIn();
                        $(`span[data-checkout=${key}]`).text(
                            response.data[key].toLocaleString('en-US', {
                                style: 'currency',
                                currency: 'USD',
                            })
                        );
                        $(`input[data-checkout=${key}]`).val(response.data[
                            key]);
                    }
                }).fail((response) => {
                    toastr.error(response?.responseJSON?.message)
                }).always(() => {
                    window.checkout__set_loading($('#payment__submit'), false)
                });
            };
            window.checkout__get_loading = function(el) {
                return $(el).attr('disabled') ? true : false
            };
            window.checkout__set_loading = function(el, loading = false) {
                if (loading) {
                    $(el).attr('disabled', true)
                    $(el).find('.loading').fadeIn();
                } else {
                    $(el).removeAttr('disabled')
                    $(el).find('.loading').fadeOut();
                }
            };
        }
    </script>
@endsection

@section('style')
    <style>
        section#checkout {
            background: url("{{ asset('/img/bg_contact.jpg') }}");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }
    </style>
@endsection

@section('content')
    <form method="POST" id="form__payment" class="hidden"
        action="{{ route('checkout.payment', ['id' => Request::route('id')]) }}">
        @csrf
        <input type="hidden" name="discount_code" data-checkout="discount_code" value="">
    </form>
    <section id="checkout" class="animation-fade-in grow py-36 min-h-screen">
        <div class="container space-y-16">
            <h1 class="uppercase text-3xl md:text-4xl xl:text-4xl font-bold text-center text-blue-700">
                Checkout
            </h1>
            <div class="w-4/5 md:w-3/4 lg:w-3/5 bg-white rounded-lg flex flex-col p-4 space-y-4 mx-auto">
                @include('components.alert_error', [
                    'title' => __('There was an error with your payment!'),
                    'content' => $errors->first(),
                ])
                <div class="flex justify-between mb-1">
                    <span class="text-base text-natural-900">Product:</span>
                    <h3 class="text-wrap w-4/5 text-end text-lg">{{ $package->title }}</strong>
                </div>
                <div class="flex justify-between">
                    <span class="text-base text-natural-900">Price:</span>
                    <span>&#36;<span>{{ view_price($package->price) }}</span></span>
                </div>
                <div class="flex justify-between">
                    <span class="text-base text-natural-900">Point:</span>
                    <strong>{{ $package->point }}</strong>
                </div>
                {{-- Discount value after fill discount code --}}
                <div class="hidden" data-section="discount_value">
                    <div class="flex justify-between">
                        <span class="text-base text-natural-900 font-bold">Discount Value:</span>
                        <div class="italic">
                            <span> -</span>
                            <span data-checkout="discount_value"></span>
                        </div>
                    </div>
                </div>
                <div data-section="total_price">
                    <div class="flex justify-between">
                        <span class="text-base text-natural-900 font-bold">Total:</span>
                        <div class="flex font-bold">
                            <span data-checkout="total_price">&#36; {{ view_price($package->price) }}</span>
                            <span class="loading hidden">...</span>
                        </div>
                    </div>
                </div>
                <div class="flex justify-between">
                    <span class="text-base text-natural-900">Discount:</span>
                    <form id="discount">
                        {{ csrf_field() }}
                        <div class="group">
                            <div class="relative flex items-center">
                                <input type="type" name="code" autofocus
                                    maxlength="10"
                                    class="px-4 py-1 transition duration-300 w-full border border-gray-300 rounded focus:border-transparent focus:outline-none focus:ring-1 focus:ring-blue-100">

                                <button type="submit"
                                    class="absolute right-0 py-2 px-4 rounded-r-md bg-blue-200 text-xs font-bold text-white transition-all duration-200 ease-in-out bg-blue-500 hover:opacity-80">
                                    Apply
                                    <span class="loading hidden">...</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="text-center">
                    <button type="submit" form="form__payment" onclick="this.disabled = true; this.form.submit();"
                        class="w-full font-semi-bold py-2 rounded-md text-white bg-blue-500 text-center hover:opacity-80">
                        <i class="ri-paypal-line"></i>
                        <span class="ml-1">Payment with PAYPAL</span>
                        <span class="loading hidden">...</span>
                    </button>
                </div>
            </div>
        </div>
    </section>
@endsection
