@extends('ventas.base')

@section('venta_subtitle')
    Desistir
@endsection

@section('venta_content')
    <div class="ui list">
        <div class="item">
            <div class="header">Valor Pagado</div>
            <div class="content">
                {{$format->pesos($venta->formaPago()->pie->pagado('pesos'))}}
                <div class="ui left pointing small label">
                    {{$format->number($venta->formaPago()->pie->pagado() / $venta->valor * 100)}}% de la venta
                </div>
            </div>
        </div>
        <div class="item">
            <div class="header">
                Multa Estandar
                <div class="ui left pointing small label">5%</div>
            </div>
            <div class="content">
                {{$format->pesos($venta->valor * 0.05 * $UF->get())}}
            </div>
        </div>
    </div>
    <form class="ui form" id="desistir_form">
        <div class="three wide field">
            <label for="fecha">Fecha</label>
            <div class="ui calendar" id="fecha">
                <div class="ui left icon input">
                    <i class="calendar icon"></i>
                    <input type="text" />
                </div>
            </div>
        </div>
        <div class="three wide field">
            <label for="devolucion">Devolución [$]</label>
            <div class="ui left labeled input">
                <div class="ui basic label">$</div>
                <input type="text" name="devolucion" />
            </div>
        </div>
        <button class="ui button">Desistir</button>
    </form>
@endsection

@push('page_scripts')
    <script>
        $(document).ready(() => {
            $('#fecha').calendar(calendar_date_options)
            $('#desistir_form').submit(event => {
                event.preventDefault()
                const body = new FormData(event.currentTarget)
                const fecha = $('#fecha').calendar('get date')
                body.set('fecha', fecha.toISOString())
                const url = '{{$urls->api}}/venta/{{$venta->id}}/desistir'
                fetchAPI(url, {method: 'post', body}).then(response => {
                    if (!response) {
                        return
                    }
                    response.json().then(json => {
                        if (json.desistida) {
                            window.location = '{{$urls->base}}/venta/{{$venta->id}}'
                        }
                    })
                })
                return false
            })
        })
    </script>
@endpush
