<?= $this->extend('layouts/main.php') ?>
<?= $this->section('content') ?>
<div class="page-wrapper">
	<?php if (session()->get('msg')) : ?>
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        <strong><?= session()->getFlashdata('msg'); ?></strong>
        </div>
    <?php endif; ?>
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Activities List</h4>
			</div>					
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='activities/create/' > <i class="fas fa-plus-circle"></i> Create</a>
			</div>
		</div>
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<form method='post' >
							<div class="accordion accordion-flush mb-3" id="accordionExample">
								<div class="accordion-item">
									<h2 class="accordion-header" id="headingTwo">
									<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" 
										aria-expanded="false" aria-controls="collapseTwo" style='padding:5px;'>
										<span class="me-3"> Filter </span>
									</button>
									</h2>
									<div id="collapseTwo" class="accordion-collapse collapse show bg-white" aria-labelledby="headingTwo">
										<div class="accordion-body ps-0">	
											<div class="row">
												<div class="col-12 col-lg-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Date</span>
														<input class="form-control form-control-sm" type="date" name="opendate" value="<?= $opendate;?>" onfocus="this.showPicker()"/>
														<span class="input-group-text d-flex align-items-center justify-content-center">-</span>
														<input class="form-control form-control-sm" type="date" name="closedate" value="<?= $closedate;?>" onfocus="this.showPicker()"/>
													</div>
												</div>
												<div class="col-12 col-lg-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Area</span>
														<div class="col">
															<select name="areaid" class="form-select form-select-sm select2 areaid">
																<option value="" selected >--</option>
																<?php
																	foreach($areas as $data) {
																		$qareaid = $data['areaid'];
																		$qareaname = $data['areaname'];
																		if($qareaid == $areaid) { echo "<option value='$qareaid' selected>$qareaname</option>"; }
																		else { echo "<option value='$qareaid'>$qareaname</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>
											</div>

											<div class="row">
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width:80px;">Site</span>
														<div class="col">
															<select name="siteid" class="form-control form-select-sm select2 siteid" id="siteid" >
																<option value="" selected>--</option>
																<?php 
																	foreach ($sites as $data) {
																		$qsiteid = $data['siteid'];
																		$qsitename = $data['sitename'];
																		if($qsiteid==$siteid) { echo "<option value='$qsiteid' selected>$qsitename</option>"; }
																		else { echo "<option value='$qsiteid'>$qsitename</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style='width: 80px;' >Product</span>
														<div class="col">
															<select name="productid" class="form-select form-select-sm select2" id="productid" >
																<option value="" selected >--</option>
																<?php
																	if(isset($products)) {
																		foreach ($products as $data) {
																			$qproductid = $data['productid'];
																			$qproductname = $data['productname'];
																			$qproductnumber = $data['productnumber'];
																			$qproductlocationenddate = $data['locationenddate'];
																			if($qproductlocationenddate == NULL){
																				if($productid == $qproductid) { echo "<option value='$qproductid' selected>$qproductname ( SN# $qproductnumber )</option>\r\n"; }
																				else { echo "<option value='$qproductid'>$qproductname ( SN# $qproductnumber )</option>\r\n";  }
																			}
																		}
																	}
																?>
															</select>
														</div>
													</div>
												</div>
											</div>
											
											<div class="row">
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">User</span>
														<div class="col">
															<select name="userid" class="form-select form-select-sm select2" id="userid">
																<option value="0">--</option>
																<?php 
																	foreach($users as $data) {
																		$quserid = $data['userid'];
																		$qfirstname = $data['firstname'];
																		$qlastname = $data['lastname'];
																		if($quserid == $userid) { echo "<option value='$quserid' selected>$qfirstname $qlastname</option>"; }
																		else { echo "<option value='$quserid'>$qfirstname $qlastname</option>"; }
																	} 
																?>
															</select>
														</div>
													</div>
												</div>
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Status</span>
														<div class="col">
															<select name="status" class="form-select form-select-sm select2" id="status">
																<option value="">--</option>
																<?php 
																foreach($stats as $qcode => $qstatus) {
																	if($qcode == $status) { echo "<option value='$qcode' selected>$qstatus</option>"; } 
																	else { echo "<option value='$qcode'>$qstatus</option>"; }
																}
																/*
																if( session()->get('level') == '1' ) { 
																	<option value="S" <?=$status == 'S' ? "selected" : "" ?>>Suspend/Delete</option>
																}
																*/
																?>
															</select>
														</div>
													</div>
												</div>
											</div>
											
											<div class="row">
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" >Product Alias</span>
														<div class="col">
															<select name="productaliasid" class="form-select form-select-sm select2" id="product" >
																<option value="" selected >--</option>
																<?php 
																	foreach ($productalias as $data) {
																		$qproductaliasid = $data['productaliasid'];
																		$qproductaliastext = $data['productaliastext'];
																		if($qproductaliasid==$productaliasid) { echo "<option value='$qproductaliasid' selected>$qproductaliastext</option>"; }
																		else { echo "<option value='$qproductaliasid'>$qproductaliastext</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Sub</span>
														<div class="col">
															<select name="sub" class="form-select form-select-sm select2" id="sub">
																<option value="" selected >--</option>
																<option value="IT" <?=$sub == 'IT' ? "selected" : "" ?>>IT</option>
																<option value="IVD" <?=$sub == 'IVD' ? "selected" : "" ?>>IVD</option>
																<option value="PS" <?=$sub == 'PS' ? "selected" : "" ?>>Product Specialist</option>
															</select>
														</div>
													</div>
												</div>
											</div>

											<div class="row">
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" >Consumable</span>
														<div class="col">
															<select name="" class="form-select form-select-sm select2" id="" >
																<option value="" selected >--</option>
																<?php 
																	// foreach ($productalias as $data) {
																	// 	$qproductaliasid = $data['productaliasid'];
																	// 	$qproductaliastext = $data['productaliastext'];
																	// 	if($qproductaliasid==$productaliasid) { echo "<option value='$qproductaliasid' selected>$qproductaliastext</option>"; }
																	// 	else { echo "<option value='$qproductaliasid'>$qproductaliastext</option>"; }
																	// }
																?>
															</select>
														</div>
													</div>
												</div>
												<div class="col-12 col-md-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Act By</span>
														<div class="col">
															<select name="actbys" class="form-select form-select-sm select2" id="actbys" >
																<option value="" selected >--</option>
																<?php 
																	foreach ($actbys as $qid => $qvalue) {
																		if($qid == $actby) { echo "<option value='$qid' selected>$qvalue</option>"; }
																		else { echo "<option value='$qid'>$qvalue</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>
											</div>

											<div class="row">
												
												<div class="col-sm-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Action</span>
														<div class="col">
															<select name="actions" class="form-select form-select-sm select2" id="actions" >
																<option value="" selected >--</option>
																<?php 
																	foreach ($actions as $qid => $qvalue) {
																		if($qid == $action) { echo "<option value='$qid' selected>$qvalue</option>"; }
																		else { echo "<option value='$qid'>$qvalue</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>

												<div class="col-sm-6 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Act Type</span>
														<div class="col">
															<select name="acttypes" class="form-select form-select-sm select2" id="acttypes" >
																<option value="" selected >--</option>
																<?php 
																	foreach ($acttypes as $qid => $qvalue) {
																		if($qid == $acttype) { echo "<option value='$qid' selected>$qvalue</option>"; }
																		else { echo "<option value='$qid'>$qvalue</option>"; }
																	}
																?>
															</select>
														</div>
													</div>
												</div>

											</div>

											<div class="row">
												<div class="col-sm-12 mb-2">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 100px;">Search Detail</span>
														<div class="col">
															<input type="text" name="detail_activity" value="<?= $detail_activity; ?>" class="form-control" placeholder="Misal : mengirimkan modul training CL900i">
															<!-- <input type="text" class="form-control" placeholder="Lagi maintenis !" disabled />  -->
														</div>
													</div>
												</div>
											</div>

											<button type="submit" class="btn btn-success btn-sm text-white float-end"> <i class="fa fa-check"></i> &nbsp;Submit</button>
										</div>
									</div>
								</div>
							</div>							
						</form>
					</div>
				</div>
			</div>
		</div>
		
		<div class="row mt-2">
      		<div class="col-12">
				<div class="card">
					<div class="card-body">
						<button class="btn btn-success btn-xs text-white float-end" onclick="exportToExcel()"> <i class="fa fa-file-excel"></i> &nbsp;EXCEL </button>
						
						<div class="table-responsive">
							<table id="myTable" class="table small display border">
								<thead class="table-info">
									<tr>
										<th style="width: 1%; padding-right:0; margin-right:0;">No</th>
										<th style="width: 1%; padding-right:0; margin-right:0;">Type</th>
										<th style="width: 5%;">Site</th>
										<th style="width: 10%;">Product</th>
										<th style="width: 10%;">Subject</th>
										<th style="width: 1%;">Status</th>
										<th style="width: 7%;">Open/Close</th>
										<th style="width: 2%;">Owner</th>
										<th style="width: 2%;">Mail</th>
										<th style="width: 7%;">Action</th>
									</tr>
								</thead>
								<tbody>
									<?php 
										$no=1;
										foreach($tampildata as $row) {
											
											if ( ($row['activitystatus'] == 'S' OR $row['activitystatus'] == 'D') AND session()->get('level') != '0' AND session()->get('level') != '1'){continue;}

											$opendate = $row['opendate'];
											$closedate = $row['closedate'];
											if($opendate != '') { 
												if(substr($opendate,0,10)=='0000-00-00') {$opendate = '';} 
												else { $opendate = substr($opendate,0,16); }
											}
											if($closedate != '') { 
												if(substr($closedate,0,10)=='0000-00-00') {$closedate = '';} 
												else { $closedate = substr($closedate,0,16); }
											}
									?>
									<tr <?php echo $row['activitystatus'] == 'D' ? "style='background-color:#C7C8CC'" : "" ?> >
										<td><?=$no++;?></td>
										<td>
											<?php switch($row['acttypeid']){
												case "1": echo "IR"; break;
												case "2": echo "CR"; break;
												case "3": echo "PR"; break;
												case "4": echo "SP"; break;
												case "5": echo "MN"; break;
												case "6": echo "TR"; break;
												case "7": echo "RF"; break; 
												case "9": echo "TC"; break;
											}
											?>
										</td>
										<td><?=$row['sitename'];?></td>
										<td>
											<?php 
												if ($row['actby'] == 'P') {
													$row['productname'] != '' ? $row['productname'] = $row['productname'] : $row['productname'] = ' -- ';
													echo ($row['productname']);
												} else if ($row['actby'] == 'V') {
													echo ($row['vendorname'] . " (Vendor)");
												} else if ($row['actby'] == 'C') {
													echo ("Consumable / Reagent");
												} else if ($row['actby'] == 'O') {
													echo ("Other");
												} else {
													echo (" -- ");
												}
											?>
										</td>
										<td>
											<a <?php echo $row['activitystatus'] == 'D' ? "style='color:black'" : "" ?> href="activities/detail/<?= $row['actid']?>" onclick="window.open(this.href, 'Activity Detail','width=900,height=800,toolbar=1,resizable=0'); return false;"><?=$row['subject'];?></a>
											<?php if ($row['actid_ref'] > 0) { ?>
												<br>
												<a href="activities/detail/<?= $row['actid_ref']?>" style='color:#30364F' onclick="window.open(this.href, 'Activity Detail','width=900,height=800,toolbar=1,resizable=0'); return false;">Reff AR <i class="fa-solid fa-arrow-up-right-from-square"></i></a>
											<?php } ?>
										</td>
										<td>
											<?php 
											$activitystatus = $row['activitystatus'];
											if($activitystatus == "O") { echo "<button type='button' class='btn btn-sm btn-dark'>Open</button>"; }
											else if($activitystatus == "C") { echo "<button type='button' class='btn btn-sm btn-danger'>Closed</button>"; }
											else if ($activitystatus == "R") {
												if ($row['userid_owner'] == session()->get('userid')) {
													echo "<button type='button' class='btn btn-sm btn-info'>Refer</button>";;
												} else {
													echo '
														<a href="activities/refer/'.$row['actid'].'"
														class="btn btn-sm btn-outline-info">
														Refer
														</a>
														';
												}
											}
											else if($activitystatus == "P") { echo "<button type='button' class='btn btn-sm btn-light'>Pending</button>"; }
											else if($activitystatus == "S") { echo "<button type='button' class='btn btn-sm btn-warning'>Suspend</button>"; }
											else if($activitystatus == "D") { echo "<button type='button' class='btn btn-sm btn-secondary'>Disable</button>"; }
											?>
										</td>
										<td>Open : <?= $opendate; ?> <br> Close : <?= $closedate; ?></td>
										<td><?= $row['username']; ?></td>
										<td style='vertical-align:middle;line-height:1em;'>
											<i class='fa-regular fa-envelope fa-2xl'></i>
											<?php
												if($row['sendmail']==1) { echo '<i class="fa-regular fa-circle-check fa-xl" style="color: #008080;"></i>'; }
												else { echo '<i class="fa-regular fa-circle-xmark fa-xl" style="color: #800000;"></i>'; }
											?>
										</td>
										<td>
											<select name="action" class="form-select form-select-sm" onchange="action(this)">
												<option value="" selected> - </option>
												<?php 
													if ( $closedate != '') {
														$close = strtotime($closedate); 
														$now = time(); 
														$value = $now - $close; 
														if ( ($value <  2592000) ) { echo "<option value=\"activities/edit/".$row['actid']."\">Edit</option>"; } // 30 days to mars
														else if (in_array(session()->get('userid'), ['10', '5', '3', '2', '36', '9', '15', '12'])) {
															echo "<option value=\"activities/edit/".$row['actid']."\">Edit</option>";
														}
													} else {
														echo "<option value=\"activities/edit/".$row['actid']."\">Edit</option>";	
													}
													
												?>
												<option value="activities/compose/<?= $row['actid']?>">Send</option>
												<?php if( session()->get('level') == '0' || session()->get('level') == '1') { ?>
													
													<?php if($activitystatus == 'D') { ?>
														<option value="activities/disable/<?= $row['actid']?>">Enable</option>
													<?php } else { ?>
														<option value="activities/disable/<?= $row['actid']?>">Disable</option>
													<?php } ?>

													<?php if($activitystatus == 'S') { ?>
														<option value="activities/suspend/<?= $row['actid']?>">Unsuspend</option>
													<?php } else { ?>
														<option value="activities/suspend/<?= $row['actid']?>">Suspend</option>
													<?php } ?>

													<!-- <option value="activities/delete/<?= $row['actid']?>">Delete</option> -->

												<?php } ?>
												<!-- <option value="invtransactions/create/<?= $row['actid']?>">Sparepart Usage</option> -->
												<!-- <option value="activities/delete/<?= $row['actid']?>">Delete</option> -->
												<option value="activities/servicereport/<?= $row['actid']?>">Service Report</option>
											</select>
										</td>
									</tr>
									<?php } ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
		
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
function action(e){
	if( e.value.substr(0,18) == 'activities/delete/' ) {
		if( window.confirm("Are you sure?") ) {
			window.location = (e.value);
		}
	} else if (e.value.substr(0,25) == 'activities/servicereport/') {
		window.open('<?=base_url()?>/'+e.value, '_blank');
	} else { window.location = (e.value); }
}

$('.areaid').change(function() {
	if(this.value!=''){ $(".siteid").prop('disabled', true); } 
	else { $(".siteid").prop('disabled', false); }
})

<!-- SCRIPT DATATABLES -->
$(function () {
	$('#myTable').DataTable({
		"order" : [],
		"pageLength" : 50,
		"lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ]
	});
});

$('.select2').select2({
	theme: 'bootstrap-5',
	width: '100%'
});

<!-- SCRIPT UNTUK MENGHILANGKAN ALERT SECARA OTOMATIS -->
$(".alert-dismissible")
	.fadeTo(2000, 500)
	.slideUp(500, function () { 
		$(".alert-dismissible").alert("close");
	});

<!-- SCRIPT EXPORT TABLE TO EXCEL -->
function exportToExcel() {
	const data = document.getElementById("myTable");
	var ws = XLSX.utils.table_to_sheet(data);

	/* hide kolom ke 9 */
	ws["!cols"] = [];
	ws["!cols"][8] = { hidden: true };

	var wb = XLSX.utils.book_new();
	XLSX.utils.book_append_sheet(wb, ws, "Sheet 1");
	XLSX.writeFile(wb, "ActivityList.xlsx");
}

// // ajax get product
// $('#siteid').change(function() {
// 	var siteid=$('#siteid').val();
// 	$.get("<?=base_url();?>/activities/index/getproduct/"+siteid, function(data) {
// 		//console.log(data);
// 		$('#productid').val('');
// 		$('#productid').html(data);
// 		$('.select2').select2({
// 			theme: 'bootstrap-5',
// 			width: '100%'
// 		});
// 	})
// })

// ajax get product by site and productalias
$('#siteid, #product').change(function() {
    let siteid = $('#siteid').val();
	if (siteid === '') {
		siteid = 'none';
	}
    let productid=$('#product').val();
    if (productid === '') {
		productid = 'none';
	}

	$.get("<?=base_url();?>/activities/index/getproduct/"+siteid+"/"+productid, function(data) {
        console.log(data);
        $('#productid').empty().html(data);
        $('.select2').select2({
            theme: 'bootstrap-5',
            width: '100%'
        });
    });
});
</script>

<?= $this->endSection() ?>