@extends('layout.base')

@section('content')
<div class="row page-heading">
	<div class="col-md-12 h2"><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id])}}">Devoluci&oacute;n - {{$venta->unidad()->descripcion}} - {{$venta->proyecto()->descripcion}}</a></div>
</div>
<br />
<form class="form-horizontal" method="post" action="{{url('', ['p' => 'ventas', 'a' => 'devolver', 'venta' => $venta->id])}}">
<div class="form-group">
	<div class="col-md-2">Saldo</div>
	<div class="col-md-2">{{format('ufs', $venta->saldo('ufs'), null, true)}}</div>
	<div class="col-md-2" id="pesos">{{format('pesos', $valor, null, true)}}</div>
	<div class="col-md-2" id="uf">$ {{format('ufs', $uf)}}</div>
</div>
<div class="form-group" id="fecha">
	<div class="col-md-2">Fecha</div>
	@include('form.fecha')
</div>
<div class="form-group">
	<div class="col-md-2">Valor [$]</div>
	<div class="col-md-3"><input type="text" name="valor" class="form-control" /></div>
</div>
<div class="form-group">
	<div class="col-md-2">(Identificador)</div>
	<div class="col-md-4"><input type="text" name="identificador" class="form-control" /></div>
</div>
<div class="form-group">
	<div class="col-md-2">(Banco)</div>
	<div class="col-md-3"><input type="text" name="banco" id="banco" class="form-control" autocomplete="off" /></div>
</div>
<div class="form-group">
	<div class="col-md-offset-2 col-md-2"><input type="submit" value="Devolver" class="form-control" /></div>
</div>
</form>
@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
	$.getJSON('{!!url('', ['p' => 'ajax', 'a' => 'buscar', 't' => 'bancos'])!!}', function(data, status, xhr) {
		$('#banco').typeahead({"source": data});
	});
	$('#fecha').find('select').change(function(e) {
		var val = {{$venta->saldo('ufs')}};
		var d = $("select[name='day']").val();
		var m = $("select[name='month']").val();
		var y = $("select[name='year']").val();
		$.getJSON('http://localhost/money/api/', {"p": 'uf', "cmd": 'value', "day": d, "month": m, "year": y}, function(data) {
			var uf = {{$uf}};
			if (typeof data != 'undefined') {
				uf = data.uf.value;
			}
			var formatter = Intl.NumberFormat('es-Cl', {"minimumFractionDigits": 2, "maximumFractionDigits": 2});
			var str = '$ ' + formatter.format(uf);
			$('#uf').html(str);
			formatter = Intl.NumberFormat('es-Cl', {"maximumFractionDigits": 0});
			str = '$ ' + formatter.format(uf * val);
			$('#pesos').html(str);
		});
	});
});
</script>
@endpush
