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

<div class="row">
	<div class="col-md-12">
		<div class="row" style="margin-bottom:15px;">
			<?php if (empty($is_cashier_mode)) { ?>
			<div class="col-sm-7">
				<?php echo form_open('cierre', array('method'=>'get', 'class'=>'form-inline')); ?>
					<div class="form-group" style="margin-right:8px;">
						<input type="text" name="q" class="form-control" style="width:260px;" placeholder="<?php echo H(lang('cierre_search_placeholder')); ?>" value="<?php echo isset($q) ? H($q) : ''; ?>">
					</div>
					<div class="form-group" style="margin-right:8px;">
							<select name="cashier_id" class="form-control" style="width:220px;">
								<?php $csel = isset($cashier_id) ? (int)$cashier_id : 0; ?>
								<option value="0" <?php echo $csel===0?'selected':''; ?>><?php echo lang('cierre_filter_cashier_all'); ?></option>
								<?php if (!empty($cashiers)) { foreach($cashiers as $ca) { ?>
									<option value="<?php echo (int)$ca->person_id; ?>" <?php echo $csel===(int)$ca->person_id?'selected':''; ?>><?php echo H($ca->name); ?></option>
								<?php }} ?>
							</select>
						</div>
						<div class="form-group" style="margin-right:8px;">
						<select name="status" class="form-control" style="width:220px;">
							<?php $sel = isset($status) ? strtolower($status) : 'all'; ?>
							<option value="all" <?php echo $sel==='all'?'selected':''; ?>><?php echo lang('cierre_filter_all'); ?></option>
							<option value="pendiente" <?php echo $sel==='pendiente'?'selected':''; ?>><?php echo lang('cierre_filter_pending'); ?></option>
							<option value="completado" <?php echo $sel==='completado'?'selected':''; ?>><?php echo lang('cierre_filter_completed'); ?></option>
							<option value="revisado" <?php echo $sel==='revisado'?'selected':''; ?>><?php echo lang('cierre_filter_reviewed_completed'); ?></option>
						</select>
					</div>
					<button type="submit" class="btn btn-default"><span class="ion-search"></span> <?php echo lang('cierre_search'); ?></button>
				<?php echo form_close(); ?>
			</div>
			<?php } ?>
			<div class="col-sm-5 text-right">
				<?php if (!empty($is_cashier_mode) || $this->Employee->has_module_action_permission('cierre','add_update',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
					<?php echo anchor('cierre/create', '<span class="ion-plus"></span> '.lang('cierre_new'), array('class'=>'btn btn-primary')); ?>
				<?php } ?>
				<?php if (empty($is_cashier_mode) && $this->Employee->has_module_action_permission('cierre','statistics',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
					<?php echo anchor('cierre/statistics', '<span class="ion-stats-bars"></span> '.lang('cierre_statistics'), array('class'=>'btn btn-default')); ?>
				<?php } ?>
				<?php if (empty($is_cashier_mode) && $this->Employee->has_module_action_permission('cierre','config',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
					<?php echo anchor('cierre/config', '<span class="ion-gear-a"></span> '.lang('cierre_config'), array('class'=>'btn btn-default')); ?>
				<?php } ?>
			</div>
		</div>

		<div class="panel panel-piluku">
			<div class="panel-heading">
				<h3 class="panel-title"><?php echo lang('module_cierre'); ?></h3>
			</div>
			<div class="panel-body">
				<?php if (!empty($is_cashier_mode)) { ?>
					<div class="alert alert-info" style="margin-bottom:15px;">
						<?php echo H(lang('cierre_cashier_mode')); ?>
					</div>
				<?php } ?>

				<?php if ($this->session->flashdata('manage_success_message')) { ?>
					<div class="alert alert-success"><?php echo H($this->session->flashdata('manage_success_message')); ?></div>
				<?php } ?>
				<?php if ($this->session->flashdata('error')) { ?>
					<div class="alert alert-danger"><?php echo H($this->session->flashdata('error')); ?></div>
				<?php } ?>

				<?php if (empty($is_cashier_mode)) { ?>
				<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_estado'); ?></th>
								<th><?php echo lang('cierre_total_sistema'); ?></th>
								<th><?php echo lang('cierre_total_contado'); ?></th>
								<th><?php echo lang('cierre_diferencia'); ?></th>
								<th><?php echo lang('common_actions'); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php if (!empty($cierres)) { foreach ($cierres as $c) { ?>
								<tr>
									<td><?php echo H($c->fecha); ?></td>
									<td><?php echo H($c->caja_nombre); ?></td>
									<td><?php echo H($c->bolsa_manual); ?></td>
									<?php
										$estado_label_map = array(
	'pendiente' => lang('cierre_filter_pending'),
	'completado' => lang('cierre_filter_completed'),
	'revision' => lang('cierre_status_revision'),
	'completadorevisado' => lang('cierre_filter_reviewed_completed'),
);

// Normalizar estado para evitar blancos por espacios/mayúsculas/variantes
$raw_estado = isset($c->estado) ? trim((string)$c->estado) : '';
$norm_estado = strtolower(preg_replace('/[^a-z0-9]+/i','', $raw_estado));
if ($raw_estado === '')
{
	$estado_txt = lang('cierre_status_unknown');
}
else
{
	$estado_txt = isset($estado_label_map[$norm_estado]) ? $estado_label_map[$norm_estado] : $raw_estado;
}
									?>
									<td><?php echo H($estado_txt); ?></td>
									<td><?php echo to_currency($c->total_sistema_usd); ?></td>
									<td><?php echo to_currency($c->total_contado_usd); ?></td>
									<td><?php echo to_currency($c->diferencia_total_usd); ?></td>
									<td>
									<?php $view_txt = lang('common_view'); $view_txt = $view_txt ? $view_txt : 'Ver'; ?>
									<?php echo anchor('cierre/view/'.$c->id, '<span class="ion-eye"></span> '.$view_txt, array('class'=>'btn btn-xs btn-default')); ?>
										<?php if ($this->Employee->has_module_action_permission('cierre','add_update',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
											<?php echo anchor('cierre/edit/'.$c->id, lang('common_edit'), array('class'=>'btn btn-xs btn-primary')); ?>
										<?php } ?>
									</td>
								</tr>
							<?php }} else { ?>
								<tr><td colspan="8" class="text-center"><?php echo lang('cierre_no_records'); ?></td></tr>
							<?php } ?>
						</tbody>
					</table>
				</div>
				<?php } ?>

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

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