@extends('layout.base')

@section('content')
<div class="panel panel-default">
	<div class="panel-heading">Cuotas Pendientes</div>
	<div class="panel-body">
		<div class="row">
			<div class="col-md-2">Total</div>
			<div class="col-md-1">{{count($cuotas)}}</div>
			<div class="col-md-8">$ {{format('pesos', $sum)}}</div>
			<div class="col-md-1 text-right"></div>
		</div>
		<div class="row">
			<div class="col-md-offset-8 col-md-4"><input type="text" class="form-control focus" id="tableFilter" /></div>
		</div>
		<table class="table table-striped" id="filteredTable">
		<thead>
		<tr>
			<th rowspan="2">Proyecto</th>
			<th>Departamento</th>
			<th>$</th>
			<th>D&iacute;a</th>
			<th>Cuota</th>
			<th><button onclick="javascript: pagarTodos();">&check;</button></th>
		</tr>
		<tr>
			<th>Propietario</th>
			<th>Banco</th>
			<th>Fecha Cheque</th>
			<th>Depositar</th>
			<th></th>
		</tr>
		</thead>
		<tbody>
		<?php $t = \Carbon\Carbon::today(config('app.timezone')) ?>
		@foreach ($cuotas as $cuota)
			<?php
			$f = \Carbon\Carbon::parse($cuota->pago()->fecha, config('app.timezone'));
			if ($f->dayOfWeek == \Carbon\Carbon::SATURDAY or $f->dayOfWeek == \Carbon\Carbon::SUNDAY) {
				$f->next(\Carbon\Carbon::MONDAY);
			}
			?>
		<tr>
			<td rowspan="2">{{$cuota->pie()->venta()->proyecto()->descripcion}}</td>
			<td class="text-center"><a href="{{url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $cuota->pie()->venta()->id])}}">{{$cuota->pie()->venta()->unidad()->descripcion}}</a></td>
			<td class="text-right">$ {{format('pesos', $cuota->pago()->valor)}}</td>
			<td>{{format('localDate', $f, 'EEEE dd')}}</td>
			<td>{{str_pad($cuota->numero, 2, '0', STR_PAD_LEFT)}} - {{str_pad($cuota->pie()->cuotas, 2, '0', STR_PAD_LEFT)}}</td>
			<td><input type="checkbox" name="chk" data-value="{{$cuota->id}}" /></td>
		</tr>
		<tr>
			<td>{{$cuota->pie()->venta()->propietario()->nombreCompleto()}}</td>
			<td>@if ($cuota->pago()->banco()) {{$cuota->pago()->banco()->nombre}}  @endif</td>
			<td
			@if ($f < $t)
				class="danger"
			@endif
			>{{format('shortDate', $cuota->pago()->fecha)}}</td>
			<td>
				<select name="d{{$cuota->id}}">
				@for ($i = 0; $i < 31; $i ++)
					<option value="{{$i + 1}}"
					@if ($i + 1 == $t->day)
					selected="selected"
					@endif
					>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
				@endfor
				</select>
				<select name="m{{$cuota->id}}">
				@for ($i = 0; $i < 12; $i ++)
					<option value="{{$i + 1}}"
					@if ($i + 1 == $t->month)
					selected="selected"
					@endif
					>{{str_pad($i + 1, 2, '0', STR_PAD_LEFT)}}</option>
				@endfor
				</select>
				<select name="y{{$cuota->id}}">
				@for ($i = $t->year; $i > $t->year - 5; $i --)
					<option value="{{$i}}">{{$i}}</option>
				@endfor
				</select>
			</td>
			<td><a href="#" onclick="javascript: pagar({{$cuota->id}})">&check;</a></td>
		</tr>
		@endforeach
		</tbody>
		</table>
	</div>
</div>
@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
	$('.focus').focus();
});
$('#tableFilter').filterTable({"table": 'filteredTable', "height": 2, "excludes": ['Depositar', '']});
function pagar(id_cuota) {
	var fy = $("select[name='y" + id_cuota + "']").val();
	var fm = $("select[name='m" + id_cuota + "']").val();
	var fd = $("select[name='d" + id_cuota + "']").val();
	var fecha = fy + '-' + fm + '-' + fd;
	$.post('{!!nUrl('cuotas', 'depositar')!!}', {"cuota": id_cuota, "fecha": fecha} , function(data) {
		if (data == 'ok') {
			window.location.reload();
		}
		console.debug(data);
	});
}
function pagarTodos()
{
	var cnt = 0;
	var ok = 0;
	$("input[name='chk']").each(function(e, i) {
		if (!$(this).is(':checked')) {
			return;
		}
		cnt ++;
		var id = $(this).attr('data-value');
		var fy = $("select[name='y" + id + "']").val();
		var fm = $("select[name='m" + id + "']").val();
		var fd = $("select[name='d" + id + "']").val();
		var fecha = fy + '-' + fm + '-' + fd;
		$.post('{!!nUrl('cuotas', 'depositar')!!}', {"cuota": id, "fecha": fecha}, function(data) {
			if (data == 'ok') {
				ok ++;
			} else {
				console.debug(data);
			}
		});
	});

	if (ok == cnt) {
		window.location.reload();
	}
}
</script>
@endpush
