<?php $this->load->view("partial/header"); ?>

<script type="text/javascript">
$(function(){
	$(document).on('change', '.pv-verify', function(){
		var $cb = $(this);
		var lineType = $cb.data('type');
		var lineId = parseInt($cb.data('id')) || 0;
		var detalleId = parseInt($cb.data('detalle-id')) || 0;
		var verified = $cb.is(':checked') ? 1 : 0;
		$.post(<?php echo json_encode(site_url('cierre/ajax_set_payment_verified')); ?>,
			{line_type: lineType, line_id: lineId, cierre_detalle_id: detalleId, verified: verified},
			function(resp){
				if (!resp || !resp.success)
				{
					// revert if failed
					$cb.prop('checked', !verified);
				}
				location.reload();
			},
			'json'
		);
	});
});
</script>

<div class="row">
	<div class="col-md-12">
		<div class="panel panel-piluku">
			<div class="panel-heading">
				<h3 class="panel-title"><?php echo lang('cierre_payment_verification_title'); ?></h3>
			</div>
			<div class="panel-body">

				<?php
				$total_usd = 0;
				$verified_usd = 0;
				foreach($rows as $r)
				{
					$amt = (float)$r['amount'];
					$moneda = $r['moneda'] ? $r['moneda'] : 'USD';
					$tasa = (float)$r['cambio_tasa'];
					$usd = $moneda === 'BS' ? ($tasa > 0 ? $amt / $tasa : 0) : $amt;
					$total_usd += $usd;
					if ((int)$r['verified']) $verified_usd += $usd;
				}
				$pending_usd = $total_usd - $verified_usd;
				?>

				<div class="row" style="margin-bottom:15px;">
					<div class="col-md-4"><strong><?php echo lang('cierre_total_to_verify'); ?>:</strong> <?php echo number_format($total_usd,2); ?></div>
					<div class="col-md-4"><strong><?php echo lang('cierre_total_verified'); ?>:</strong> <?php echo number_format($verified_usd,2); ?></div>
					<div class="col-md-4"><strong><?php echo lang('cierre_total_pending'); ?>:</strong> <?php echo number_format($pending_usd,2); ?></div>
				</div>

				<?php echo form_open('cierre/payment_verification', array('method'=>'get','class'=>'form-inline')); ?>
				<div class="form-group" style="margin-right:10px;">
					<label><?php echo lang('cierre_filter_from'); ?></label>
					<input type="date" class="form-control" name="from" value="<?php echo H($filters['from']); ?>">
				</div>
				<div class="form-group" style="margin-right:10px;">
					<label><?php echo lang('cierre_filter_to'); ?></label>
					<input type="date" class="form-control" name="to" value="<?php echo H($filters['to']); ?>">
				</div>
				<div class="form-group" style="margin-right:10px;">
					<label><?php echo lang('cierre_filter_payment_type'); ?></label>
					<select class="form-control" name="pago_id">
						<option value="0"><?php echo lang('cierre_filter_verified_all'); ?></option>
						<?php foreach($pagos as $p) { ?>
							<option value="<?php echo (int)$p->id; ?>" <?php echo ((int)$filters['pago_id'] === (int)$p->id) ? 'selected' : ''; ?>><?php echo H($p->nombre); ?></option>
						<?php } ?>
					</select>
				</div>
				<div class="form-group" style="margin-right:10px;">
					<label><?php echo lang('cierre_filter_verified'); ?></label>
					<select class="form-control" name="verified">
						<option value="all" <?php echo $filters['verified']=='all'?'selected':''; ?>><?php echo lang('cierre_filter_verified_all'); ?></option>
						<option value="no" <?php echo $filters['verified']=='no'?'selected':''; ?>><?php echo lang('cierre_filter_verified_no'); ?></option>
						<option value="yes" <?php echo $filters['verified']=='yes'?'selected':''; ?>><?php echo lang('cierre_filter_verified_yes'); ?></option>
					</select>
				</div>
				<button class="btn btn-primary" type="submit"><?php echo lang('common_search'); ?></button>
				<?php echo form_close(); ?>

				<hr />
				<div class="table-responsive">
					<table class="table table-bordered table-striped">
						<thead>
							<tr>
								<th><?php echo lang('cierre_fecha'); ?></th>
								<th><?php echo lang('cierre_caja'); ?></th>
								<th><?php echo lang('cierre_bolsa'); ?></th>
								<th><?php echo lang('cierre_tipo_pago'); ?></th>
								<th class="text-right"><?php echo lang('cierre_contado'); ?></th>
								<th class="text-center"><?php echo lang('cierre_verified'); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach($rows as $r) { ?>
								<tr>
									<td><?php echo H($r['fecha']); ?> <span class="text-muted">#<?php echo (int)$r['cierre_id']; ?></span></td>
									<td><?php echo H($r['caja_nombre']); ?></td>
									<td><?php echo H($r['bolsa_manual']); ?></td>
									<td><?php echo H($r['tipo_pago']); ?><?php echo $r['line_type']=='breakdown' ? ' <span class="label label-info">'.lang('cierre_desglose').'</span>' : ''; ?></td>
									<td class="text-right"><?php echo number_format((float)$r['amount'],2).' '.H($r['moneda']); ?></td>
									<td class="text-center">
										<input type="checkbox" class="pv-verify" data-type="<?php echo H($r['line_type']); ?>" data-id="<?php echo (int)$r['line_id']; ?>" data-detalle-id="<?php echo (int)$r['cierre_detalle_id']; ?>" <?php echo (int)$r['verified'] ? 'checked' : ''; ?> />
									</td>
								</tr>
							<?php } ?>
						</tbody>
					</table>
				</div>

				<div style="margin-top:15px;">
					<?php echo anchor('cierre', lang('cierre_back'), array('class'=>'btn btn-default')); ?>
				</div>

			</div>
		</div>
	</div>
</div>

<?php $this->load->view("partial/footer"); ?>
