@extends('ventas.base')

@section('venta_subtitle')
    Pie
@endsection

@section('venta_content')
    <div class="ui basic compact segment">
        Valor Promesa: {{ $format->ufs($venta->valor) }} <br />
        10% {{ $format->ufs($venta->valor * 0.1) }}
    </div>
    <form class="ui form" id="add_pie">
        <input type="hidden" name="venta" value="{{ $venta->id }}" />
        <input type="hidden" name="fecha" value="{{ $venta->fecha->format('Y-m-d') }}" />
        <div class="three wide field">
            <label for="valor">Valor</label>
            <div class="ui right labeled input">
                <input type="text" name="valor" id="valor" value="{{ round($venta->valor * 0.1, 2) }}" />
                <div class="ui basic label">UF</div>
            </div>
        </div>
        <div class="three wide field">
            <label for="cuotas"># Cuotas</label>
            <input type="number" name="cuotas" id="cuotas" />
        </div>
        <button class="ui button">Agregar</button>
    </form>
@endsection

@push('page_scripts')
    <script>
        $(document).ready(() => {
            $('#add_pie').submit(event => {
                event.preventDefault()
                const data = new FormData(event.currentTarget)
                return fetchAPI('{{ $urls->api }}/venta/{{ $venta->id }}/pie/add', {method: 'post', body: data}).then(response => {
                    if (response.ok) {
                        return response.json()
                    }
                }).then(json => {
                    if (json.success) {
                        window.location = '{{$urls->base}}/venta/{{$venta->id}}'
                        return true
                    }
                    return false
                })
            })
        })
    </script>
@endpush
