@extends('layouts.frontend')

@section('title', 'Checkout')

@section('content')
    <article id="checkout">
        <div class="breadcrumb">
            <div class="container d-flex align-items-center">
                <a href="{{ url('/') }}">Inicio</a>
                <span>@yield('title')</span>
            </div>
        </div>
        <section>
            <div class="container">
                {!! Form::open() !!}
                <div class="row row-grid">
                    <div class="col-md-6 mb-3">
                        <h2>Datos de facturación</h2>
                        <div class="form-group">
                            {!! Form::label('first_name', 'Nombre *', ['class' => 'form-label']) !!}
                            {!! Form::text('first_name', auth()->user()->user_data->first_name, ['required', 'class' => 'form-control']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('last_name', 'Apellido *', ['class' => 'form-label']) !!}
                            {!! Form::text('last_name', auth()->user()->user_data->last_name, ['required', 'class' => 'form-control']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('business_name', 'Razón social (opcional)', ['class' => 'form-label']) !!}
                            {!! Form::text('business_name', null, ['class' => 'form-control']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('address', 'Dirección *', ['class' => 'form-label']) !!}
                            {!! Form::textarea('address', null, ['class' => 'form-control', 'required', 'placeholder' => 'Calle y número de casa. Departamento, piso, etc. (opcional)', 'rows' => 3]) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('city', 'Ciudad *', ['class' => 'form-label']) !!}
                            {!! Form::text('city', null, ['class' => 'form-control', 'required']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('phone', 'Teléfono *', ['class' => 'form-label']) !!}
                            {!! Form::text('phone', auth()->user()->user_data->phone, ['class' => 'form-control', 'required']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('email', 'Email *', ['class' => 'form-label']) !!}
                            {!! Form::email('email', auth()->user()->email, ['class' => 'form-control', 'required']) !!}
                        </div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h2>Datos de envío</h2>
                        <div class="form-group">
                            <ul class="list-unstyled accordion">
                                @foreach($shipping_methods as $shipping_method)
                                    <li>
                                        {!! Form::radio('shipping_method_id', $shipping_method->id, false, ['id' => 'shippingMethod'.$shipping_method->id, 'class' => 'shipping_method', 'data-price' => $shipping_method->price, 'data-formatted-price' => $shipping_method->price ? num_format($shipping_method->price) : 'Gratis', 'required' => $loop->first]) !!}
                                        <label for="shippingMethod{{ $shipping_method->id }}">
                                            <h3>{{ $shipping_method->title }}</h3>
                                            <p>{{ $shipping_method->excerpt }}</p>
                                        </label>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                        <div class="form-group">
                            <input type="checkbox" name="toggleShipping" id="toggleShipping">
                            <label for="toggleShipping">Enviar a otra dirección</label>
                        </div>
                        <div class="form-group d-none">
                            {!! Form::label('shipping_data[first_name]', 'Nombre *', ['class' => 'form-label']) !!}
                            {!! Form::text('shipping_data[first_name]', auth()->user()->user_data->first_name, ['required', 'class' => 'form-control optional', 'disabled']) !!}
                        </div>
                        <div class="form-group d-none">
                            {!! Form::label('shipping_data[last_name]', 'Apellido *', ['class' => 'form-label']) !!}
                            {!! Form::text('shipping_data[last_name]', auth()->user()->user_data->last_name, ['required', 'class' => 'form-control optional', 'disabled']) !!}
                        </div>
                        <div class="form-group d-none">
                            {!! Form::label('shipping_data[address]', 'Dirección *', ['class' => 'form-label']) !!}
                            {!! Form::textarea('shipping_data[address]', null, ['class' => 'form-control optional', 'required', 'placeholder' => 'Calle y número de casa. Departamento, piso, etc. (opcional)', 'rows' => 3, 'disabled']) !!}
                        </div>
                        <div class="form-group d-none">
                            {!! Form::label('shipping_data[city]', 'Ciudad *', ['class' => 'form-label']) !!}
                            {!! Form::text('shipping_data[city]', null, ['class' => 'form-control optional', 'required', 'disabled']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('feedback', 'Observaciones (opcional)', ['class' => 'form-label']) !!}
                            {!! Form::textarea('feedback', null, ['class' => 'form-control', 'placeholder' => 'Notas acerca del pedido, instrucciones para envío, etc.', 'rows' => 3]) !!}
                        </div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h2>Forma de pago</h2>
                        <div class="form-group">
                            <ul class="list-unstyled accordion">
                                @foreach($payment_methods as $payment_method)
                                    <li>
                                        {!! Form::radio('payment_method_id', $payment_method->id, $loop->first, ['id' => 'paymentMethod'.$payment_method->id]) !!}
                                        <label for="paymentMethod{{ $payment_method->id }}">
                                            <h3>{{ $payment_method->title }}</h3>
                                            <p>{{ $payment_method->excerpt }}</p>
                                        </label>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                        <div class="form-group">
                            <ul class="list-unstyled accordion">
                                <li>
                                    <span>Producto</span>
                                    <span>Subtotal</span>
                                </li>
                                @foreach(Cart::content() as $item)
                                    <li>
                                        <span>{{ $item->name }} <b>x{{ $item->qty }}</b></span>
                                        <h3><b>{{ num_format($item->qty * $item->price) }}</b></h3>
                                    </li>
                                @endforeach
                                <li>
                                    <span>Envío</span>
                                    <h3><b id="shipping">-</b></h3>
                                </li>
                                <li>
                                    <span>Total</span>
                                    <h3><b id="total" data-value="{{ Cart::subtotal(0, '', '') }}">{{ Cart::subtotal(0, ',', '.') }}</b></h3>
                                </li>
                            </ul>
                        </div>
                        <div class="btn-container mt-5 text-right">
                            <button type="submit" class="btn">
                                <span>Hacer pedido</span>
                                <i class="far fa-arrow-right"></i>
                            </button>
                        </div>
                    </div>
                    <div class="col-md-6 mb-3">
                        @if($options->checkout_image)
                            <img src="{{ asset($options->checkout_image) }}" alt="" class="sticky-image">
                        @endif
                    </div>
                </div>
                {!! Form::close() !!}
            </div>
        </section>
    </article>
@endsection

@push('scripts')
    <script>
        $(function() {
          $('#toggleShipping').on('change', function() {
            if($(this).is(':checked')) {
              $('.optional').removeAttr('disabled');
              $('.optional').parents('.form-group').removeClass('d-none');
            } else {
              $('.optional').attr('disabled', 'disabled');
              $('.optional').parents('.form-group').addClass('d-none');
            }
          });

          $('.shipping_method').on('change', function() {
            $('#shipping').text($(this).data('formatted-price'));
            $('#total').text((parseInt($('#total').data('value')) + parseInt($(this).data('price'))).toLocaleString('es-ES', {
              maximumFractionDigits: 0
            }));
          });
        });
    </script>
@endpush