@extends('layout.base')

@section('content')
<div class="panel panel-default">
	<div class="panel-heading">
		<h2>
			<a href="{{url('', ['p' => 'ventas', 'action' => 'show', 'venta' => $venta->id])}}">{{$venta->unidad()->descripcion}} - {{$venta->unidad()->proyecto()->descripcion}}</a>
			@if ($venta->pie()->asociados() != null)
				<br />
				@foreach ($venta->pie()->asociados() as $asociado)
					<small><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $asociado->venta()->id])}}">{{$asociado->venta()->unidad()->descripcion}}</a></small>
				@endforeach
			@elseif ($venta->pie()->asociado() != null)
				<br />
				<small><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->pie()->asociado()->venta()->id])}}">{{$venta->pie()->asociado()->venta()->unidad()->descripcion}}</a></small>
				@foreach ($venta->pie()->asociado()->asociados() as $asociado)
					@if ($asociado->venta()->id == $venta->id)
						@continue
					@endif
					<small><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $asociado->venta()->id])}}">{{$asociado->venta()->unidad()->descripcion}}</a></small>
			@endforeach
			@endif
		</h2>
	</div>
	<div class="panel-body">
		<table class="table">
		<thead>
		<tr class="info">
			<th colspan="7" class="text-center">PIE</th>
		</tr>
		</thead>
		<tbody>
		<tr>
			<td>Fecha</td>
			<td>{{\App\Helper\Format::shortDate($venta->pie()->fecha)}}</td>
			<td>Valor</td>
			<td>{{\App\Helper\Format::ufs($venta->pie()->valor)}} UF</td>
			<td>$ {{\App\Helper\Format::pesos($venta->pie()->valorPesos())}}</td>
			<td>Cuotas</td>
			<td>({{count($venta->pie()->abonadas())}}) {{count($venta->pie()->pagadas())}} / {{$venta->pie()->cuotas}}
		</tr>
		</tbody>
		</table>
		<table class="table table-striped">
		<thead>
		<tr class="info">
			<th>#</th>
			<th>Fecha</th>
			<th>Banco</th>
			<th>Identificador</th>
			<th class="text-right">Valor</th>
			<th class="text-right">UF</th>
			<th>
				<div class="row">
					<div class="col-md-6">Fecha</div>
					<div class="col-md-6">
						<div class="row">Deposito</div>
						<div class="row">Abono</div>
					</div>
				</div>
			</th>
			<th>
				Depositar<br />
				Abonar
			</th>
		</tr>
		</thead>
		<tbody>
		<?php
		$cnt = 0;
		$total = (object) [
				'pagado' => 0,
				'pagado_uf' => 0,
				'por_pagar' => 0,
				'por_pagar_uf' => 0,
				'cuotas' => 0,
				'cuotas_uf' => 0,
				'dif' => 0,
				'dif_uf' => 0
		];
		$t = \Carbon\Carbon::today(config('app.timezone'));
		?>
		@foreach ($venta->pie()->cuotas() as $cuota)
		<tr>
			<td class="@include('ventas.pies.cuotas.estado')">
				<a href="{{url('', ['p' => 'cuotas', 'a' => 'show', 'cuota' => $cuota->id])}}">{{++$cnt}} <span class="glyphicon glyphicon-edit small"></span></a>
			</td>
			<td>{{\App\Helper\Format::shortDate($cuota->pago()->fecha)}}</td>
			<td>{{($cuota->pago()->banco()) ? $cuota->pago()->banco()->nombre : ''}}</td>
			<td>
			@if ($cuota->pago()->identificador == '')
				<form action="#" method="post"><input type="text" name="identificador{{$cuota->id}}" /></form>
			@else
				{{$cuota->pago()->identificador}}
			@endif
			</td>
			<td class="text-right">$ {{\App\Helper\Format::pesos($cuota->valor())}}</td>
			<td class="text-right">{{\App\Helper\Format::ufs($cuota->valor('ufs'))}} UF</td>
			<td class="text-center @include('ventas.pies.cuotas.estado')">
				{{\App\Helper\Format::shortDate($cuota->pago()->estado()->fecha)}}
				<a href="{{url('', ['p' => 'pagos', 'a' => 'edit', 'pago' => $cuota->pago()->id, 'asociado' => 'venta', 'venta' => $venta->id])}}"><span class="glyphicon glyphicon-edit small"></span></a>
			</td>
			<td>
			@if ($cuota->pago()->estado()->estado == 0 or $cuota->pago()->estado()->estado == -1)
			<?php
			$total->por_pagar += $cuota->valor();
			$total->por_pagar_uf += $cuota->valor('ufs');
			?>
				<form action="#" method="post">
				<div class="row">
					<div class="col-md-3"><select name="d{{$cuota->id}}" class="form-control">
					@for ($i = 0; $i < 31; $i ++)
						<option value="{{$i + 1}}"{{($i + 1 == $t->day) ? ' selected="selected"' : ''}}>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select></div>
					<div class="col-md-3"><select name="m{{$cuota->id}}" class="form-control">
					@for ($i = 0; $i < 12; $i ++)
						<option value="{{$i + 1}}"{{($i + 1 == $t->month) ? ' selected="selected"' : ''}}>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select></div>
					<div class="col-md-4"><select name="y{{$cuota->id}}" class="form-control">
					@for ($i = $t->year; $i > $t->year - 5; $i --)
						<option value="{{$i}}"{{($i == $t->year) ? ' selected="selected"' : ''}}>{{$i}}</option>
					@endfor
					</select></div>
					<div class="col-md-1"><a><span class="glyphicon glyphicon-ok depositar click" data-id="{{$cuota->id}}"></span></a></div>
				</div>
				</form>
			@elseif ($cuota->pago()->estado()->estado == 1)
				<?php
				$total->pagado += $cuota->pago()->valor;
				$total->pagado_uf += $cuota->pago()->valor('ufs');
				?>
				<form action="#" method="post">
				<div class="row">
					<?php $f = \Carbon\Carbon::parse($cuota->pago()->estado()->fecha, config('app.timezone')) ?>
					<div class="col-md-3"><select name="d{{$cuota->id}}" class="form-control">
					@for ($i = 0; $i < 31; $i ++)
						<option value="{{$i + 1}}"{{($i + 1 == $f->day) ? ' selected="selected"' : ''}}>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select></div>
					<div class="col-md-3"><select name="m{{$cuota->id}}" class="form-control">
					@for ($i = 0; $i < 12; $i ++)
						<option value="{{$i + 1}}"{{($i + 1 == $f->month) ? ' selected="selected"' : ''}}>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
					@endfor
					</select></div>
					<div class="col-md-4"><select name="y{{$cuota->id}}" class="form-control">
					@for ($i = $t->year; $i > $f->year - 5; $i --)
						<option value="{{$i}}"{{($i == $f->year) ? ' selected="selected"' : ''}}>{{$i}}</option>
					@endfor
					</select></div>
					<div class="col-md-1">
						<a><span class="glyphicon glyphicon-ok abonar click" data-id="{{$cuota->id}}"></span></a><br />
						<a><span class="glyphicon glyphicon-remove rebotar click" data-id="{{$cuota->id}}"></span></a>
					</div>
				</div>
				</form>
			@else
			<?php
			$total->pagado += $cuota->pago()->valor;
			$total->pagado_uf += $cuota->pago()->valor('ufs');
			?>
			@endif
			<?php
			$total->cuotas += $cuota->pago()->valor;
			$total->cuotas_uf += $cuota->pago()->valor('ufs');
			?>
			</td>
			<td><a class="remove-cuota" data-id="{{$cuota->id}}" href="#"><span class="glyphicon glyphicon-remove"></span></a>
		</tr>
		@endforeach
		<?php
		$total->dif = $venta->pie()->valorPesos() - $total->cuotas;
		$total->dif_uf = $venta->pie()->valor - $total->cuotas_uf;

		$valores = (object) [
				'titulo' => 'Total Pagado',
				'pesos' => $total->pagado,
				'ufs' => $total->pagado_uf
		];
		?>
		@include('ventas.pies.totales')
		<?php
		$valores->titulo = 'Por Pagar';
		$valores->pesos = $total->por_pagar;
		$valores->ufs = $total->por_pagar_uf;
		?>
		@include('ventas.pies.totales')
		<?php
		$valores->titulo = 'Total Cuotas';
		$valores->pesos = $total->cuotas;
		$valores->ufs = $total->cuotas_uf;
		?>
		@include('ventas.pies.totales')
		<?php
		$valores->titulo = 'Diferencia c/Pie';
		$valores->pesos = $total->dif;
		$valores->ufs = $total->dif_uf;
		?>
		@include('ventas.pies.totales')
		</tbody>
		</table>
		<a href="{{nUrl('informes', 'cuotas', ['venta' => $venta->id])}}">Exportar</a>
	</div>
</div>
@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
	$("[name^='identificador']").change(function() {
		var name = $(this).attr('name');
		var id = parseInt(name.substr('identificador'.length, name.length));
		var url = '?p=cuotas&a=edited&cuota=' + id;
		var value = $(this).val();

		var promise = $.post(url, {"identificador": value});
		promise.done(function(data) {
			window.location.reload();
		});
	});
	$('.remove-cuota').click(function(e) {
		var choice = confirm('Estás seguro?');
		if (choice) {
			window.location = '{!!url('', ['p' => 'cuotas', 'a' => 'remove'])!!}&cuota=' + $(this).attr('data-id');
		}
	});
	$('.depositar').click(function(e) {
		var cuota = $(this).attr('data-id');

		var d = $("select[name='d" + cuota + "']").val();
		var m = $("select[name='m" + cuota + "']").val();
		var y = $("select[name='y" + cuota + "']").val();
		var f = y + '-' + m + '-' + d;

		$.post('{!!url('', ['p' => 'cuotas', 'a' => 'depositar', 'ajax' => 'true'])!!}', {'cuota': cuota, 'fecha': f}, function(data) {
			if (data == 'ok') {
				window.location.reload();
				return;
			}
		});
	});
	$('.abonar').click(function(e) {
		var cuota = $(this).attr('data-id');

		var d = $("select[name='d" + cuota + "']").val();
		var m = $("select[name='m" + cuota + "']").val();
		var y = $("select[name='y" + cuota + "']").val();
		var f = y + '-' + m + '-' + d;

		$.post('{!!url('', ['p' => 'cuotas', 'a' => 'abonar', 'ajax' => 'true'])!!}', {'cuota': cuota, 'fecha': f}, function(data) {
			if (data == 'ok') {
				window.location.reload();
				return;
			}
		});
	});
	$('.rebotar').click(function(e) {
		var cuota = $(this).attr('data-id');

		var d = $("select[name='d" + cuota + "']").val();
		var m = $("select[name='m" + cuota + "']").val();
		var y = $("select[name='y" + cuota + "']").val();
		var f = y + '-' + m + '-' + d;

		$.post('{!!url('', ['p' => 'cuotas', 'a' => 'rebotar', 'ajax' => 'true'])!!}', {'cuota': cuota, 'fecha': f}, function(data) {
			console.debug(data);
			if (data == 'ok') {
				window.location.reload();
				return;
			}
		});
	});
});
</script>
@endpush
