<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<?php if(!isset($productname)) {$productname = '';} ?>
<div class="page-wrapper">
	<div class="container-fluid">
		
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Unit Group</h4>
			</div>
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='unitgroup/create/' 
					onclick="window.open(this.href, 'Create Unit Group','width=900,height=600,toolbar=1,resizable=0'); return false;">
					<i class="fas fa-plus-circle"></i> Create
				</a>
			</div>
		</div>
		<?php 
		if(isset($validation)) {
		?>
			<div class='alert alert-danger alert-dismissible'>
				<?= $validation->listErrors(); ?>
				<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"> <span aria-hidden="true"></span> </button>
			</div>
		<?php
		}
		?>
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<form method='post' >
							<div class='form-group row'>
								<label class='form-label col-2'>Product Name</label>
								<div class='col-10'>
									<input class='form-control' type='text' name='productname' placeholder='product name' value='<?=$productname;?>' />
								</div>
							</div>
							<button type="submit" class="btn btn-success text-white"> <i class="fa fa-search"></i> Search</button>
						</form>
					</div>
				</div>
			</div>
		</div>
		
		<?php if(isset($unitgroup)) { ?>
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<div class="table-responsive">
							<table id="myTable" class="table display table-striped border">
								<thead>
									<th>Code</th> <th>Product</th> <th>Unit</th> <th>Qty.</th> <th>Base Unit</th> <th></th>
								</thead>
								<tbody>
									<?php
									foreach($unitgroup as $data) {
										$unitgroupid = $data['unitgroupid'];
										$unitgroupcode = $data['unitgroupcode'];
										$productname = $data['productname'];
										$quantity = $data['quantity'];
										$unit = $data['unit'];
										$baseunit = $data['baseunit'];
										if( $data['enddate'] != '' ) {
											$enddate = date('d-m-Y', strtotime($data['enddate']));
										} else { $enddate = ''; }

										if( $enddate != '' ) { 
											$classend = 'class="bg-danger"'; 
											$toggletext = "<a class='btn btn-success btn-sm' href='unitgroup/toggle/$unitgroupid' 
												onclick=\"window.open(this.href, 'form','width=400,height=300,toolbar=1,resizable=0'); return false;\">
												<i class='fa-solid fa-check'></i> Enable
											</a>";
										} else {
											$toggletext = "<a class='btn btn-danger btn-sm' href='unitgroup/toggle/$unitgroupid' 
												onclick=\"window.open(this.href, 'form','width=400,height=300,toolbar=1,resizable=0'); return false;\">
												<i class='fas fa-trash-alt'></i> Disable
											</a>";
										}
									?>

									<tr <?php echo $enddate == '' ? '' : " class='bg-danger' "; ?> > 
										<td><?=$unitgroupcode;?></td> <td><?=$productname;?></td> <td><?=$unit;?></td> <td><?=$quantity;?></td> <td><?=$baseunit;?></td> 
										<td>
											<a class='btn btn-warning btn-sm' href='unitgroup/edit/<?=$unitgroupid;?>' 
												onclick="window.open(this.href, 'Catalog Editor','width=900,height=600,toolbar=1,resizable=0'); return false;">
												<i class="fas fa-pencil-alt"></i> Edit
											</a>
											<?=$toggletext;?>
										</td>
									</tr>
									<?php } ?>
								</tbody>
							</table>
						</div>
						<!-- edit Modal -->
						<div id="editModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="tooltipmodel" aria-hidden="true">
							<div class="modal-dialog modal-dialog-centered modal-xl">
								<div class="modal-content">
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php } ?>
		
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
$(function () {
	$('#myTable').DataTable({
		"order" : []
	});
});
</script>
<?= $this->endSection() ?>