@extends('frontend.layouts.app')

@section('title', app_name() . ' | ' . __('labels.frontend.auth.register_box_title'))

@section('content')
    <div class="row justify-content-center align-items-center">
        <div class="col col-sm-8 align-self-center">
            <div class="g_et_al_header2 text-center" style="padding-bottom:20px">
                Subscription payment
                {{--                        @lang('labels.frontend.auth.register_box_title')--}}
            </div>

            <div class="container">

                <div class="row g_et_al_header3 " style="padding-bottom: 10px">
                    <div class="col">
                        Selected plan: <span style="font-weight: bold">{{$plan->name}}</span>
                    </div>
                </div>
                <div class="row" style="padding-bottom: 10px">
                    <div class="col">
                        Selected plan price: <span style="font-weight: bold">$AUD {{round($plan->cost/100, 2)}}</span>
                        (@TODO make currency converter)
                    </div>
                </div>

                <div class="row" style="border: 1px solid grey">
                    <div class="col">
                        {{ html()->form('POST', route('frontend.user.firstsubscription'))->id('payment-form')->open() }}
                        {{html()->hidden('stripe_plan_id', $plan->id)}}
                        <div class="form-group">
                            <label for="cardholder">Name on card</label>
                            <input class="form-control" id="card-holder-name" name="cardholder">
                        </div>
                        <div class="form-group">

                            <label>Card Details</label>

                            <fieldset style="height: 30px">

                                <div id="card-element"></div>
                            </fieldset>
                        </div>
                        <div class="form-group">
                            <label style="font-style: italic; font-size: small">Secured by Stripe</label>
                        </div>
                        {{ html()->form()->close() }}
                    </div>

                </div>


                <div class="row">
                    <div class="col" style="padding-top: 20px">
                        <p>By clicking the button below, we own you. Dance, monkey!
                        </p>
                        <p><a href="#" data-toggle="modal"
                              data-target="#disclaimer">Tems and conditions</a></p>

                    </div>
                </div>

                <div class="row">
                    <div class="col">
                        <button type="button" class="btn btn-primary" id="card-button"
                                data-secret="{{ $intent->client_secret }}">Let's do it!
                        </button>
                    </div><!--col-->
                </div><!--row-->

            </div><!-- card-body -->


            {{--            </div><!-- card -->--}}
        </div><!-- col-md-8 -->
    </div><!-- row -->


@endsection

@section('dialogs')

    <div id="disclaimer" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="additemtitle">Terms and conditions</h4>

                </div>
                <div class="modal-body">

These are the terms and conditions. Blah blah you're not gunna read this anyway.

                </div>
            </div>
        </div>
    </div>
@endsection

@push('after-scripts')

    <script src="https://js.stripe.com/v3/"></script>
    <script>

        //@TODO use config for this
        const stripe = Stripe('pk_test_cw9zacXfqpELQeZnMGife6ox007zpVJJf6');

        const elements = stripe.elements();
        const cardElement = elements.create('card', {
            iconStyle: 'solid',
            hidePostalCode: true,
            style: {
                base: {
                    iconColor: '#c4f0ff',
                    color: '#000',
                    fontWeight: 500,
                    fontFamily: 'Inter UI, Open Sans, Segoe UI, sans-serif',
                    fontSize: '16px',
                    fontSmoothing: 'antialiased',

                    ':-webkit-autofill': {
                        color: '#fce883',
                    },
                    '::placeholder': {
                        color: '#87BBFD',
                    },
                },
                invalid: {
                    iconColor: '#FFC7EE',
                    color: '#FFC7EE',
                },
            },
        });

        cardElement.mount('#card-element');

        const cardHolderName = document.getElementById('card-holder-name');
        const cardButton = document.getElementById('card-button');
        const clientSecret = cardButton.dataset.secret;


        $(document).ready(function () {

            cardButton.addEventListener('click', async (e) => {
                const {setupIntent, error} = await stripe.confirmCardSetup(
                    clientSecret, {
                        payment_method: {
                            card: cardElement,
                            billing_details: {name: cardHolderName.value}
                        }
                    }
                );

                if (error) {
                    alert(error.message)
                } else {
                    stripePaymentHandler(setupIntent);
                }
            });
        });

        // Submit the form with the token ID.
        function stripePaymentHandler(setupIntent) {
            // Insert the token ID into the form so it gets submitted to the server
            var form = document.getElementById('payment-form');
            var hiddenInput = document.createElement('input');
            hiddenInput.setAttribute('type', 'hidden');
            hiddenInput.setAttribute('name', 'stripePaymentMethod');
            hiddenInput.setAttribute('value', setupIntent.payment_method);
            form.appendChild(hiddenInput);

            // Submit the form
            form.submit();
        }


    </script>

@endpush

@push('after-styles')
    <style>
        .select2 {
            width: 100% !important;
        }

        {
            background-color: #f6f9fc
        ;
        }

        * {
            font-family: Inter UI, Open Sans, Segoe UI, sans-serif;
            font-size: 16px;
            font-weight: 500;
        }

        form {

            padding:  15px;
        }





        fieldset {
border: 1px solid black;
            padding: 5px;

            background: rgba(18, 91, 152, 0.05);
            border-radius: 3px;
        }

        fieldset legend {
            float: left;
            width: 100%;
            text-align: center;
            font-size: 13px;
            color: #8898aa;
            padding: 3px 10px 7px;
        }

        .card-only {
            display: block;
        }

        .payment-request-available {
            display: none;
        }

        fieldset legend + * {
            clear: both;
        }

        input, button {
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            outline: none;
            border-style: none;
            color: #fff;
        }

        input:-webkit-autofill {
            transition: background-color 100000000s;
            -webkit-animation: 1ms void-animation-out;
        }

        #example4-card {
            padding: 10px;
            margin-bottom: 2px;
        }

        input {
            -webkit-animation: 1ms void-animation-out;
        }

        input::-webkit-input-placeholder {
            color: #9bacc8;
        }

        input::-moz-placeholder {
            color: #9bacc8;
        }

        input:-ms-input-placeholder {
            color: #9bacc8;
        }

        button {
            display: block;
            width: 100%;
            height: 37px;
            background-color: #d782d9;
            border-radius: 2px;
            color: #fff;
            cursor: pointer;
        }

        button:active {
            background-color: #b76ac4;
        }

        .error svg .base {
            fill: #e25950;
        }

        .error svg .glyph {
            fill: #f6f9fc;
        }

        .error .message {
            color: #e25950;
        }

        .success .icon .border {
            stroke: #ffc7ee;
        }

        .success .icon .checkmark {
            stroke: #d782d9;
        }

        .success .title {
            color: #32325d;
        }

        .success .message {
            color: #8898aa;
        }

        .success .reset path {
            fill: #d782d9;
        }
    </style>
@endpush
