@extends('layout.base', ['titulo' => 'Escriturar'])

@section('content')
<div class="row page-heading">
	<h3><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id])}}">Escriturar - {{$venta->unidad()->descripcion}} - {{$venta->proyecto()->descripcion}}</a></h3>
</div>
<br />
<div class="row">
	<div class="col-md-2">Faltante</div>
	<div class="col-md-3">$ {{format('pesos', -$venta->saldo('pesos'))}}</div>
	<div class="col-md-3">{{format('ufs', -$venta->saldo())}} UF</div>
</div>
<br />
<form method="post" class="form-horizontal" action="{{url('', ['p' => 'escrituras', 'a' => 'agregar', 'venta' => $venta->id])}}">
	<div class="form-group">
		<div class="col-md-2">Fecha</div>
		<?php $f = \Carbon\Carbon::today(config('app.timezone')) ?>
		<div class="col-md-1">
			<select name="escritura_day" class="form-control">
			@for ($i = 0; $i < 31; $i ++)
				<option value="{{$i + 1}}"
				@if ($i + 1 == $f->day)
				selected="selected"
				@endif
				>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
			@endfor
			</select>
		</div>
		<div class="col-md-1">
			<select name="escritura_month" class="form-control">
			@for ($i = 0; $i < 12; $i ++)
				<option value="{{$i + 1}}"
				@if ($i + 1 == $f->month)
				selected="selected"
				@endif
				>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
			@endfor
			</select>
		</div>
		<div class="col-md-2">
			<select name="escritura_year" class="form-control">
			@for ($i = $f->year; $i > $f->year - 5; $i --)
				<option value="{{$i}}">{{$i}}</option>
			@endfor
			</select>
		</div>
	</div>
	@if ($venta->pie != 0 and $venta->pie()->reajuste == 0)
	<div class="panel panel-default">
		<div class="panel-heading fp-title" id="reajuste">Reajuste <span class="small glyphicon glyphicon-plus"></span></div>
		<div class="panel-body fp-body">
			<div class="form-group">
				<div class="col-md-2">Valor [$]</div>
				<div class="col-md-3"><input type="text" name="valor_reajuste" class="form-control" /></div>
			</div>
			<div class="form-group">
				<div class="col-md-2">Fecha</div>
				<div class="col-md-1">
					<select name="reajuste_day" class="form-control fp-day">
					@for ($i = 0; $i < 31; $i ++)
						<option value="{{$i + 1}}"
						@if ($i + 1 == $f->day)
						selected="selected"
						@endif
						>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select>
				</div>
				<div class="col-md-1">
					<select name="reajuste_month" class="form-control fp-month">
					@for ($i = 0; $i < 12; $i ++)
						<option value="{{$i + 1}}"
						@if ($i + 1 == $f->month)
						selected="selected"
						@endif
						>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select>
				</div>
				<div class="col-md-2">
					<select name="reajuste_year" class="form-control fp-year">
					@for ($i = $f->year; $i > $f->year - 5; $i --)
						<option value="{{$i}}">{{$i}}</option>
					@endfor
					</select>
				</div>
			</div>
		</div>
	</div>
	@endif
	@if ($venta->escritura == 0)
	<div class="panel panel-default">
		<div class="panel-heading fp-title" id="escritura">Pago en Escritura <span class="small glyphicon glyphicon-plus"></span></div>
		<div class="panel-body fp-body">
			<div class="form-group">
				<div class="col-md-2">Valor [$]</div>
				<div class="col-md-4"><input type="text" name="escritura_valor" class="form-control" /></div>
			</div>
			<div class="form-group">
				<div class="col-md-2">(Valor [UF])</div>
					<div class="col-md-4"><input type="text" name="escritura_valor_uf" class="form-control" /></div>
			</div>
			<div class="form-group">
				<div class="col-md-2">Fecha</div>
				<div class="col-md-1">
					<select name="pago_escritura_day" class="form-control fp-day">
					@for ($i = 0; $i < 31; $i ++)
						<option value="{{$i + 1}}"
						@if ($i + 1 == $f->day)
						selected="selected"
						@endif
						>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select>
				</div>
				<div class="col-md-1">
					<select name="pago_escritura_month" class="form-control fp-month">
					@for ($i = 0; $i < 12; $i ++)
						<option value="{{$i + 1}}"
						@if ($i + 1 == $f->month)
						selected="selected"
						@endif
						>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select>
				</div>
				<div class="col-md-2">
					<select name="pago_escritura_year" class="form-control fp-year">
					@for ($i = $f->year; $i > $f->year - 5; $i --)
						<option value="{{$i}}">{{$i}}</option>
					@endfor
					</select>
				</div>
			</div>
		</div>
	</div>
	@endif
	@if ($venta->subsidio == 0)
	<div class="panel panel-default">
		<div class="panel-heading fp-title" id="subsidio">Subsidio <span class="small glyphicon glyphicon-plus"></span></div>
		<div class="panel-body fp-body">
			<div class="form-group">
				<div class="col-md-2">Valor Ahorrado [UF]</div>
				<div class="col-md-4"><input type="text" name="subsidio_ahorrado" class="form-control" /></div>
			</div>
			<div class="form-group">
				<div class="col-md-2">Valor Subsidio [UF]</div>
				<div class="col-md-4"><input type="text" name="subsidio_valor" class="form-control" /></div>
			</div>
			<div class="row">
				<div class="col-md-2">Total</div>
				<div class="col-md-4" id="subsidio_total"></div>
				<div class="col-md-1">UF</div>
			</div>
		</div>
	</div>
	@endif
	@if ($venta->credito == 0)
	<div class="panel panel-default">
		<div class="panel-heading fp-title" id="credito">Cr&eacute;dito <span class="small glyphicon glyphicon-plus"></span></div>
		<div class="panel-body fp-body">
			<div class="form-group">
				<div class="col-md-2">Banco</div>
				<div class="col-md-5"><input type="text" name="credito_banco" id="credito_banco" class="form-control" autocomplete="off" /></div>
			</div>
			<div class="form-group">
				<div class="col-md-2">Valor [UF]</div>
				<div class="col-md-4"><input type="text" name="credito_valor" class="form-control" /></div>
			</div>
		</div>
	</div>
	@elseif ($venta->credito()->pago()->banco == 0)
	<div class="panel panel-default">
		<div class="panel-heading">Cr&eacute;dito</div>
		<div class="panel-body">
			<div class="row form-group">
				<div class="col-md-2">Valor [UF]</div>
				<div class="col-md-4">{{format('ufs', $venta->credito()->pago()->valor('ufs'), null, true)}}</div>
			</div>
			<div class="form-group">
				<div class="col-md-2">Banco</div>
				<div class="col-md-5"><input type="text" name="credito_banco" id="credito_banco" class="form-control" autocomplete="off" /></div>
			</div>
		</div>
	</div>
	@endif
	<div class="form-group">
		<div class="col-md-offset-2 col-md-3"><input type="submit" value="Escriturar" class="form-control" /></div>
	</div>
</form>
@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
	$('.fp-body').toggle();
	$('.fp-title').click(function(e) {
		var body = $(this).next('.fp-body');
		body.toggle();
		body.find('input').val('');
		body.find('select.fp-day').val({{$f->day}});
		body.find('select.fp-month').val({{$f->month}});
		body.find('select.fp-year').val({{$f->year}});
	});
	$('#subsidio').parent().find('input').change(function() {
		changeSubsidio();
	});
	$.getJSON('{!!url('', ['p' => 'ajax', 'a' => 'buscar', 't' => 'bancos'])!!}', function(data, status, xhr) {
		$('#credito_banco').typeahead({"source": data});
	});
});
function changeSubsidio() {
	var ahorro = parseFloat($("input[name='subsidio_ahorrado']").val()) || 0;
	var valor = parseFloat($("input[name='subsidio_valor']").val()) || 0;
	var total = ahorro + valor;
	$('#subsidio_total').html(total);
}
</script>
@endpush
