<div class="container my-5 ">
	<div class="card bg-light-transparent">
		<div class="card-body">

			<div class="card card-body">
				<div class="container" >	
					<div class="row">
					<div class="col-8">	
					<form action="<?php echo base_url('/payslip/process'); ?>" enctype="multipart/form-upload" id="payslip-form">
						<div class="form-group row">
							<label for="month" class="col-lg-4"><h5>Salary/ Wage Month</h5></label>		
							<div class="col-lg-5" >
								<input type="text" name="salaryWage" class="datepicker-mm-yyyy form-control" id="salaryWage" value="<?php echo (!empty($table)) ? date('M',mktime(0,0,0,$table[0]->month,10)).'/'.$table[0]->year: ''?>" <?php echo ($payslip_id != null) ?'disabled':''?> />
							</div>
						</div>
						<div class="form-group row">
							<label for="company" class="col-lg-4"><h5>Company</h5></label>
							<div class="col-lg-5">
								<?php if (empty($table)){?>
									
								<select class="form-control" name="company" id="company" onchange="companyChoose()" <?php echo ($payslip_id != null) ?'disabled':''?> >
									<option value="-1" data-roc="-1">Select Company</option>
										<?php 
											if(!empty($companies)) foreach ($companies as $company){ 
												echo '<option value="'.$company->id.'" data-roc="'.$company->roc_no.'" data-address="'.$company->address.'" data-postcode="'.$company->postcode.'" data-state="'.$company->state.'" data-country="'.$company->country.'" data-city="'.$company->city.'">'. $company->name.'</option>';
											} 
										?>		
								</select>

								<?php }else{ ?>
								<input type="text" value="<?php echo $table[0]->name?>" class="form-control" disabled>
								<?php } ?>
							</div>
						</div>
						<div class="form-group row">
							<label for="companycode" class="col-lg-4"><h5>Company No</h5></label>
							<div class="col-lg-5">
								<input class="form-control" type="text" id="companycode" name="companycode" value="<?php echo (!empty($table))? $table[0]->roc_no: ''?>" readonly>
							</div>
						</div>
					</form>
					</div>
					<div class="col-4 d-none" id="error-legend">				
						<div class="row"><small>Error Detail:</small></div>
						<div class="row">
							<div class="col-2 text-danger"><small>01</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Gross pay not match.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>02</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Total deduction not match.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>03</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Net pay not match.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>04</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Number of column not match.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>05</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Mandatory field empty.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>06</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Company code not match.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>07</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>Duplicate data.</small></div>
						</div>
						<div class="row">
							<div class="col-2 text-danger"><small>08</small></div>
							<div class="col-1">-</div>
							<div class="col-8"><small>No user account or wrong IC</small></div>
						</div>
					</div>
					</div>

					<?php if (empty($table)){ ?>
						
					<div class="row">
						<div class="col-12">			
							<div class="row">
								<div class="col-12 col-md-3">
									<div class="bg-primary" style="color:#fff;border-radius:5px;">
										<label class="btn btn-block"> <span class="fa fa-file"></span>
											Select File
											<input type="file" name="payslip" id="fileUpload" style="display:none;" accept="application/vnd.ms-excel" <?php echo ($payslip_id != null) ?'disabled':''?> disabled/>
										</label>
									</div>			
								</div>
								<div class="col-12 col-md-3">
									<input type="text" class="form-control" id="fileUploadName" disabled>
								</div>
								<div class="col-12 col-md-3">
									<button type="button" class="btn btn-info btn-block" id="btnUpload" disabled>
										<span class="fa fa-plus"></span>  Upload 
									</button>
								</div>	
								<div class="col-12 col-md-3">
									<button type="button" class="btn btn-success btn-block" id="btnApprove" disabled>
										<span class="fa fa-check"></span> Approve
									</button>
								</div>
							</div>
						</div>
					</div>
					<?php }else{ ?>
						<div class="col-md-3">
							<a class="btn btn-primary btn-block" href="<?php echo base_url('/hr/payslip/print_payslip_upload_list/').$this->uri->segment(4); ?>" target="_blank"><span class="fas fa-print"></span> print</a>
						</div>
					<?php } ?>
				</div>
			</div>


		</div>
	</div>

	<div class="card bg-light-transparent">
		<div class="card-body">
			<div class="card">
				<div class="col-12 table-responsive mt-4" id="tablePayslip">
						<table class="table table-bordered" width="100%" border="1">
							<thead>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="#: activate to sort column ascending" style="width: 0px;">
									#
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Company Code/ Cost Centre: activate to sort column ascending" style="width: 0px;">
									Company No/ Cost Centre
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Company Name: activate to sort column ascending" style="width: 0px;">
									Company Name
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Salary/ Wage Month: activate to sort column ascending" style="width: 0px;">
									Salary/ Wage Month
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
									Employee ID
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 0px;">
									Name
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Net Pay: activate to sort column ascending" style="width: 0px;">
									Net Pay
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Gross: activate to sort column ascending" style="width: 0px;">
									Gross
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Total Deduction: activate to sort column ascending" style="width: 0px;">
									Total Deduction
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Status: activate to sort column ascending" style="width: 0px;">
									Status
								</th>
								<th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Action: activate to sort column ascending" style="width: 0px;">
									Action
								</th>
							</thead>		
							<tbody>
								<?php if (!empty($table))
									foreach ($table as $key => $content) {
								?>
									<tr>
										<td><?php echo $key+1; ?></td>
										<td><?php echo $content->roc_no; ?></td>
										<td><?php echo $content->name; ?></td>
										<td><?php echo date('M',mktime(0,0,0,$content->month)).' '.$content->year; ?></td>
										<td><?php echo $content->employeeID; ?></td>
										<td><?php echo $content->employeeName; ?></td>
										<td><?php echo number_format($content->netPay,2); ?></td>
										<td><?php echo number_format($content->grossPay,2);?></td>
										<td><?php echo number_format($content->totalDeduction,2); ?></td>
										<td class="text-success">OK</td>
										<td><a href="<?php echo base_url('/hr/payslip/view/'.$content->id);?>" target='_blank'><span class="fas fa-eye"></span></a></td>
									</tr>
								<?php
									}
								?>
							</tbody>
						</table>		
					</div>
			</div>

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

 <script>
 	var isBtnApprove = false;
 	var companycode = '';
	var companyName = '';
	var tableValue = '';
	var uploadReq = false;
	var approveReq = false;
	var duplicateEntry = '';
	var unknownIc = '';
	$('#salaryWage').on('change',function(){
		if($('#salaryWage').val() != ''){
			$('#fileUpload').removeAttr('disabled');
		}
	});

	$('#fileUpload').on("change", function(){
		var file = $('#fileUpload')[0].files[0];
		$('#fileUploadName').val(file.name);
		$('#btnUpload').removeAttr('disabled');

		if(isBtnApprove){
			disableBtnApprove();
		}
	});

	$('#btnUpload').click(function(event){
		event.preventDefault();
		var table = $('.table').DataTable();
		table.clear().draw();
		companycode = '';
		companyName = '';
		tableValue = '';

		var frmData = new FormData();
		frmData.append("payslip", $('#fileUpload').prop('files')[0]);
		frmData.append("salaryWage", $('#salaryWage').val());
		frmData.append("company", $('#company').val());
		frmData.append("companycode", $('#companycode').val());

		

		if(!uploadReq){
			iziToast.show({
				title: '',
				message: 'Please wait while we processing the documents'
			});
			uploadReq = true;
		$.ajax({
			url:"<?php echo base_url('/hr/payslip/ajxUploadPayslip'); ?>",
			type:'post',
			dataType:'json', //return type value
			data:frmData,
			processData: false,
			contentType: false,
			success:function(data){
				console.log(data);
				duplicateEntry = data.duplicateStaff;
				unknownIc = data.unknownStaff;

				if(data.status_code != 200){
					iziToast.error({
						title: '',
						message: '500 server error.Please reupload the document'
					});
					companycode = '';
					companyName = '';
					table.clear();
				}else{
					if(data.content){				
						populateTable(data.content,data.salaryWage);
					}
				}
				uploadReq = false;
			},error:function(){
				iziToast.error({
					title: '',
					message: 'Error during uploading process.Please reupload the document'
				});
				companycode = '';
				companyName = '';
				table.clear();
				uploadReq = false;

			}});

		}
	});
	
	$('#btnApprove').click(function(event){
		event.preventDefault();

		var sWage = $('#salaryWage').val();
		var coId = $('#company').val();
		var coCode = $('#companycode').val();
		
		if (!approveReq) {
			approveReq = true;
			$.ajax({
				url:"<?php echo base_url('/hr/payslip/ajxApprovePayslip'); ?>",
				type:'POST',
				dataType:'json',
				data:{
					salaryWage	: sWage,
					companyId	: coId,
					companycode : coCode,
					tableData 	: JSON.stringify(tableValue),
				},
				success:function(data){
					
					if(data.status_code != 200){
						iziToast.error({
							title: '',
							message: data.status_code+' - '+data.status
						});	
					}else{
						if (data.saveStatus != true) {
								iziToast.error({
									title: '',
									message: 'Unable to save payslip duplicate entry'
								});
							// $('#duplicateBody').empty();

							// for (var i = 0; i < ; i++) {
							// 	Things[i]
							// };

							// $('#pay').modal('show');	
						}else{
							iziToast.success({
									title: '',
									message: 'Payslip have been uploaded'
							});
						window.location.replace("<?php echo base_url('/hr/payslip/payslip_upload_list');?>");

						}
							
						// console.log(data);
					}
					approveReq = false;
				},error:function(){
					iziToast.error({
						title: '',
						message: 'Operations failed'
					});
					approveReq = false;
				}});
			};
	});

	function companyChoose (){
		disableBtnApprove();
		var company = $('#company').find('option:selected').data('roc');
		var uploadedFile = $('#fileUploadName').val();
		
		if(uploadedFile != ''){
			$("#btnUpload").click();
		}

		if(company != '-1'){
			$('#companycode').val(company);
		}else{
			$('#companycode').val('');
		}				
	}

	// function populateTable(data,salaryWage){
		
	// 	var table = $('.table').DataTable();
	// 	tableValue = '';
	// 	var tableContent = new Array();				
	// 	var index = 0;

	// 	var tmpArray = new Array();
	// 	//iterate json content and remove empty row objects
	// 	for (var k = 0; k < data.length; k++) {
	// 		for (var i = 0; i <= Object.keys(data[k][1]).length; i++) {
	// 			const tempObj = new Object();
	// 			const objTemp = new Object();

	// 			var employeeId = '';
	// 			var employeeName = '';
	// 			var netPay = 0;
	// 			var deduction = 0;
	// 			var grossPay = 0;
	// 			var status = '';
	// 			var action = '';
				
	// 			if(data[k][1][i] != null){

	// 				//get employee id
	// 				if(data[k][1][i].A != null){
	// 					employeeId = data[k][1][i].A;
	// 				}

	// 				//get employee name
	// 				if(data[k][1][i].C != null){
	// 					employeeName = data[k][1][i].C;
	// 				}

	// 				//get netpay
	// 				if(data[k][1][i].AC){
	// 					netPay = data[k][1][i].AD;
	// 				}

	// 				//get deduction 
	// 				if(data[k][1][i].AB){
	// 					deduction = data[k][1][i].AC;
	// 				}

	// 				//get grossPay
	// 				if(data[k][1][i].O){
	// 					grossPay = data[k][1][i].O;
	// 				}

	// 				if((employeeId != ''  || employeeName != '') && 
	// 					isNotHeaderEmployeeID(employeeId) && 
	// 					isNotCompanyCode(data[k][1][i].A,data[k][1][i].B) && 
	// 					isNotCompanyName(data[k][1][i].A,data[k][1][i].B)){


	// 					tempObj.companycode 	= 	companycode;
	// 					tempObj.companyName		=  	companyName;
	// 					tempObj.salaryWage 		= 	salaryWage;
	// 					tempObj.employeeId 		= 	employeeId;
	// 					tempObj.employeeName 	= 	employeeName;
	// 					tempObj.netPay 			= 	netPay;
	// 					tempObj.grossPay		= 	grossPay;
	// 					tempObj.deduction 		= 	deduction;
	// 					tempObj.dataStatus 		= 	getDataStatus(data[k][1][i]);
	// 					tempObj.dataAction		=	getDataAction(data[k][1][i]);
	// 					tableContent.push(tempObj);

	// 					objTemp.Employee_ID 				= data[k][1][i].A;
	// 					objTemp.Employee_IC 				= data[k][1][i].B;
	// 					objTemp.Employee_Name 				= data[k][1][i].C;
	// 					objTemp.Position 					= data[k][1][i].D;
	// 					objTemp.Department 					= data[k][1][i].E;
	// 					objTemp.Basic_Pay					= data[k][1][i].F;
	// 					objTemp.Fixed_Allowance 			= data[k][1][i].G;
	// 					objTemp.Other_Allowance 			= data[k][1][i].H;
	// 					objTemp.Phone_Allowance 			= data[k][1][i].I;
	// 					objTemp.Parking_Allowance 			= data[k][1][i].J;
	// 					objTemp.Professional_Allowance 		= data[k][1][i].K;
	// 					objTemp.License_Allowance			= data[k][1][i].L;
	// 					objTemp.Acting_Allowance 			= data[k][1][i].M;
	// 					objTemp.Travelling_Allowance 		= data[k][1][i].N;
	// 					objTemp.Gross_Pay 					= data[k][1][i].O;

	// 					objTemp.EMP_EPF_Contribution 		= data[k][1][i].P;
	// 					objTemp.EMP_SOCSO_Contribution 		= data[k][1][i].Q;
	// 					objTemp.EMP_SIP_Contribution 		= data[k][1][i].R;
	// 					objTemp.Zakat_Deduction 			= data[k][1][i].S;
	// 					objTemp.PCB_Deduction 				= data[k][1][i].T;
	// 					objTemp.Loan 						= data[k][1][i].U;
	// 					objTemp.Wakaf 						= data[k][1][i].V;
	// 					objTemp.YaPEIM 						= data[k][1][i].W;
	// 					objTemp.Club_Fee 					= data[k][1][i].X;
	// 					objTemp.Ptptn 						= data[k][1][i].Y;
	// 					objTemp.Tabung_Haji 				= data[k][1][i].Z;
	// 					objTemp.CP38						= data[k][1][i].AA;
	// 					objTemp.Other_Deduction 			= data[k][1][i].AB;

	// 					objTemp.Total_Deduction 			= data[k][1][i].AC;
	// 					objTemp.Nett_Pay 					= data[k][1][i].AD;
	// 					objTemp.Employer_EPF 				= data[k][1][i].AE;
	// 					objTemp.Employer_SOCSO 				= data[k][1][i].AF;
	// 					objTemp.Employer_SIP 				= data[k][1][i].AG;

	// 					tmpArray.push(objTemp);
	// 				}

					
	// 			}

	// 		};
	// 	};		
		
	// 	var isAllOk = true;
	// 	var errCounter = 0;
	
	// 	for (var i = 0; i < tableContent.length; i++) {
	// 		table.row.add({
	// 			0 	: i+1,
	// 			1 	: tableContent[i].companycode,
	// 			2 	: tableContent[i].companyName,  
	// 			3 	: tableContent[i].salaryWage,
	// 			4 	: tableContent[i].employeeId,
	// 			5 	: tableContent[i].employeeName,
	// 			6	: tableContent[i].netPay,
	// 			7	: tableContent[i].grossPay,
	// 			8	: tableContent[i].deduction,
	// 			9	: tableContent[i].dataStatus,
	// 			10 	: tableContent[i].dataAction
	// 		}).draw();

	// 		if(tableContent[i].dataStatus != '<span class="text-success">OK</span>'){
	// 			isAllOk = false;
	// 			errCounter++;
	// 		}

			

	// 	};
		
	// 	if(!isAllOk){
	// 		iziToast.error({
	// 			title: '',
	// 			message: errCounter+' errors founds'
	// 		});

	// 		$('#error-legend').removeClass('d-none');
	// 	}else{
	// 		iziToast.success({
	// 			title: '',
	// 			message:'All row pass'
	// 		});
	// 		tableValue = tmpArray;
	// 		enableBtnApprove();
	// 		$('#error-legend').addClass('d-none');

	// 	}
	// }

	function populateTable(data,salaryWage){
		
		var table = $('.table').DataTable();
		tableValue = '';
		var tableContent = new Array();				
		var index = 0;

		var tmpArray = new Array();
		//iterate json content and remove empty row objects
		for (var k = 0; k < data.length; k++) {
			for (var i = 0; i <= Object.keys(data[k][1]).length; i++) {
				const tempObj = new Object();
				const objTemp = new Object();

				var employeeId = '';
				var employeeName = '';
				var netPay = 0;
				var deduction = 0;
				var grossPay = 0;
				var status = '';
				var action = '';
				
				if(data[k][1][i] != null){

					//get employee id
					if(data[k][1][i].A != null){
						employeeId = data[k][1][i].A;
					}

					//get employee name
					if(data[k][1][i].C != null){
						employeeName = data[k][1][i].C;
					}

					//get netpay
					if(data[k][1][i].AC){
						netPay = data[k][1][i].AH;
					}

					//get deduction 
					if(data[k][1][i].AB){
						deduction = data[k][1][i].AG;
					}

					//get grossPay
					if(data[k][1][i].O){
						grossPay = data[k][1][i].S;
					}

					if((employeeId != ''  || employeeName != '') && 
						isNotHeaderEmployeeID(employeeId) && 
						isNotCompanyCode(data[k][1][i].A,data[k][1][i].B) && 
						isNotCompanyName(data[k][1][i].A,data[k][1][i].B)){


						tempObj.companycode 	= 	companycode;
						tempObj.companyName		=  	companyName;
						tempObj.salaryWage 		= 	salaryWage;
						tempObj.employeeId 		= 	employeeId;
						tempObj.employeeName 	= 	employeeName;
						tempObj.netPay 			= 	netPay;
						tempObj.grossPay		= 	grossPay;
						tempObj.deduction 		= 	deduction;
						tempObj.dataStatus 		= 	getDataStatus(data[k][1][i]);
						tempObj.dataAction		=	getDataAction(data[k][1][i]);
						tableContent.push(tempObj);

						objTemp.Employee_ID 				= data[k][1][i].A;
						objTemp.Employee_IC 				= data[k][1][i].B;
						objTemp.Employee_Name 				= data[k][1][i].C;
						objTemp.Position 					= data[k][1][i].D;
						objTemp.Department 					= data[k][1][i].E;

						objTemp.Basic_Pay					= data[k][1][i].F;
						objTemp.Fixed_Allowance 			= data[k][1][i].G;
						objTemp.HOD_Allowance 				= data[k][1][i].H;
						objTemp.Branch_Allowance 			= data[k][1][i].I;
						objTemp.Phone_Allowance 			= data[k][1][i].J;
						objTemp.Parking_Allowance 			= data[k][1][i].K;
						objTemp.Professional_Allowance 		= data[k][1][i].L;
						objTemp.License_Allowance			= data[k][1][i].M;
						objTemp.Acting_Allowance 			= data[k][1][i].N;
						objTemp.Travelling_Allowance 		= data[k][1][i].O;
						objTemp.Vehicle_Allowance 			= data[k][1][i].P;
						objTemp.Special_Allowance 			= data[k][1][i].Q;
						objTemp.Fuel_Allowance 				= data[k][1][i].R;
						objTemp.Gross_Pay 					= data[k][1][i].S;

						objTemp.EMP_EPF_Contribution 		= data[k][1][i].T;
						objTemp.EMP_SOCSO_Contribution 		= data[k][1][i].U;
						objTemp.EMP_SIP_Contribution 		= data[k][1][i].V;
						objTemp.Zakat_Deduction 			= data[k][1][i].W;
						objTemp.PCB_Deduction 				= data[k][1][i].X;
						objTemp.Loan 						= data[k][1][i].Y;
						objTemp.Wakaf 						= data[k][1][i].Z;
						objTemp.YaPEIM 						= data[k][1][i].AA;
						objTemp.Club_Fee 					= data[k][1][i].AB;
						objTemp.Ptptn 						= data[k][1][i].AC;
						objTemp.Tabung_Haji 				= data[k][1][i].AD;
						objTemp.CP38						= data[k][1][i].AE;
						objTemp.Other_Deduction 			= data[k][1][i].AF;

						objTemp.Total_Deduction 			= data[k][1][i].AG;
						objTemp.Nett_Pay 					= data[k][1][i].AH;
						objTemp.Employer_EPF 				= data[k][1][i].AI;
						objTemp.Employer_SOCSO 				= data[k][1][i].AJ;
						objTemp.Employer_SIP 				= data[k][1][i].AK;

						tmpArray.push(objTemp);
					}

					
				}

			};
		};		
		
		var isAllOk = true;
		var errCounter = 0;
	
		for (var i = 0; i < tableContent.length; i++) {
			table.row.add({
				0 	: i+1,
				1 	: tableContent[i].companycode,
				2 	: tableContent[i].companyName,  
				3 	: tableContent[i].salaryWage,
				4 	: tableContent[i].employeeId,
				5 	: tableContent[i].employeeName,
				6	: tableContent[i].netPay,
				7	: tableContent[i].grossPay,
				8	: tableContent[i].deduction,
				9	: tableContent[i].dataStatus,
				10 	: tableContent[i].dataAction
			}).draw();

			if(tableContent[i].dataStatus != '<span class="text-success">OK</span>'){
				isAllOk = false;
				errCounter++;
			}

			

		};
		
		if(!isAllOk){
			iziToast.error({
				title: '',
				message: errCounter+' errors founds'
			});

			$('#error-legend').removeClass('d-none');
		}else{
			iziToast.success({
				title: '',
				message:'All row pass'
			});
			tableValue = tmpArray;
			enableBtnApprove();
			$('#error-legend').addClass('d-none');

		}
	}

	function isNotHeaderEmployeeID(employeeId){
		result = false;

		if(employeeId != 'Employee_ID' && employeeId != 'employee id' && employeeId != 'EMPLOYEE_ID' && employeeId != 'Employee _ID' && employeeId != 'employee_id' ){
			result = true;
		}
		
		return result;
	}

	function  getDataStatus(data) {
		result = '<span class="text-danger">Error</span>';
		var passStatus = true;
		var conditions = [false,false,false,false,false,false,true,true]; //all true then status ok else error
		var fifthConditions = [false,false,false,false,false,false,false];
		var errorCode = ['E01','E02','E03','E04','E05','E06','E07','E08'];

		/*
		checking rules
		==================================================
		1. gross pay = total
		2. deduction = total
		3. netpay(total) = gross pay (total) - deduction (total)
		[4. staff id -> not a duplicate payslip //cannot be done in client side to avoid race conditions]
		4. no of column
		5. 6 items should not be empty
			- Name
			- Employee Id
			- Department
			- Salary / Wages
			- IC NO
			- Designation
		6. check company code
		7. check duplicate staff
		8. No user / ic is wrong
		*/

		//1.first conditions checking ......
		if(data.F == null){
			data.F = 0;
		}else{
			data.F = Number(data.F);
		}

		if(data.G == null){
			data.G = 0;
		}else{
			data.G = Number(data.G);
		}

		if(data.H == null){
			data.H = 0;
		}else{
			data.H = Number(data.H);
		}

		if(data.I == null){
			data.I = 0;
		}else{
			data.I = Number(data.I);
		}

		if(data.J == null){
			data.J = 0;
		}else{
			data.J = Number(data.J);
		}

		if(data.K == null){
			data.K = 0;
		}else{
			data.K = Number(data.K);
		}

		if(data.L == null){
			data.L = 0;
		}else{
			data.L = Number(data.L);
		}

		if(data.M == null){
			data.M = 0;
		}else{
			data.M = Number(data.M);
		}

		if(data.N == null){
			data.N = 0;
		}else{
			data.N = Number(data.N);
		}

		if(data.O == null){
			data.O = 0;
		}else{
			data.O = Number(data.O);
		}

		if(data.P == null){
			data.P = 0;
		}else{
			data.P = Number(data.P);
		}

		if(data.Q == null){
			data.Q = 0;
		}else{
			data.Q = Number(data.Q);
		}

		if(data.R == null){
			data.R = 0;
		}else{
			data.R = Number(data.R);
		}

		totalGrossPay = data.F + data.G + data.H + data.I + data.J + data.K + data.L + data.M + data.N + data.O + data.P + data.Q + data.R;
		if(totalGrossPay.toFixed(2) == Number(data.S)){
			conditions[0] = true; //1. first conditions is true
		}
		//1.first conditions checking end ..................


		//2. second conditions checking end ...............
			//P to AB			

		if(data.T == null){
			data.T = 0;
		}else{
			data.T = parseFloat(data.T);
		}
			

		if(data.U == null){
			data.U = 0;
		}else{
			data.U = parseFloat(data.U);
		}

		if(data.V == null){
			data.V = 0;
		}else{
			data.V = parseFloat(data.V);
		}
		

		if(data.W == null){
			data.W = 0;
		}else{
			data.W = parseFloat(data.W);
		}
			

		if(data.X == null){
			data.X = 0;
		}else{
			data.X = parseFloat(data.X);
		}
		

		if(data.Y == null){
			data.Y = 0;
		}else{
			data.Y = parseFloat(data.Y);
		}
			

		if(data.Z == null){
			data.Z = 0;
		}else{
			data.Z = parseFloat(data.Z);
		}
			

		if(data.AA == null){
			data.AA = 0;
		}else{
			data.AA = parseFloat(data.AA);
		}

		if(data.AB == null){
			data.AB = 0;
		}else{
			data.AB = parseFloat(data.AB);
		}

		if(data.AC == null){
			data.AC = 0;
		}else{
			data.AC = parseFloat(data.AC);
		}

		if(data.AD == null){
			data.AD = 0;
		}else{
			data.AD = parseFloat(data.AD);
		}

		if(data.AE == null){
			data.AE = 0;
		}else{
			data.AE = parseFloat(data.AE);
		}

		if(data.AF == null){
			data.AF = 0;
		}else{
			data.AF = parseFloat(data.AF);
		}	

		deduction = data.T + data.U + data.V + data.W + data.X + data.Y + data.Z + data.AA + data.AB + data.AC + data.AD + data.AE + data.AF;
		
		if(deduction.toFixed(2) == Number(data.AG)){
			conditions[1] = true;
		}
		//2. second contidons checking end ................

		//3. third conditions checking 
		netpay = totalGrossPay.toFixed(2) - deduction.toFixed(2);

		if(netpay.toFixed(2) == Number(data.AH)){
			conditions[2] = true;
		}
		//3. third conditions checking

		//4. fourth condition checking
		if(Object.keys(data).length == 37){
			conditions[3] = true;
		}
		//4. fourth condition checking
		
		//5. fifth condition checking 
			//done - Name
			//done - Employee Id
			//done - Department
			// - Salary / Wages
			// - IC NO
			// - Designation

			if(data.A != null && data.C != null && data.E != null && data.B != null && data.D != null){
				conditions[4] = true;
			}
		//5. fifth condition checking

		//6. condition checking
		var code = $('#companycode').val();
		// console.log('value for the code : '+code+' and company code '+companycode);
		if(code == companycode){
			conditions[5] = true;
		}
		//6. condition checking
		
		//7.checking duplicate staff
		if(duplicateEntry != false){
			var duplicateSize = duplicateEntry.length;
			for (var i = 0; i < duplicateSize; i++) {
				var ic_no = data.B;
				var pattern = /[^a-zA-Z0-9]/g;

				if(duplicateEntry[i].employeeIC == ic_no.replace(pattern,'')){
					conditions[6] = false;
				}
			};
			
		}else{
			conditions[6] = true;
		}
		//7.checking duplicate staff

		//8. check if user is exist or not 
		for (var i = 0; i < unknownIc.length; i++) {
			if(unknownIc[i] == data.B){
				conditions[7] = false;
			}
		};
		//8. check if user is exist or not

		//checking if all conditions pass
		for (var i = 0; i < conditions.length; i++) {
			if(conditions[i] != true){
				passStatus = false;
				result += '<li class="text-danger">'+errorCode[i]+'</li>';
			}
		};


		if(passStatus){
			return '<span class="text-success">OK</span>';
		}
		return result;
	}

	function getDataAction(res){
		console.log('Data action : ');
		console.log(res);



		var company_name = $('#company option:selected').text();
		var company_address = $('#company option:selected').data('address');
		var postcode = $('#company option:selected').data('postcode');
		var city = $('#company option:selected').data('city');
		var state = $('#company option:selected').data('state');
		var country = $('#company option:selected').data('country');
		
		var wage = $('#salaryWage').val();
		var string = '<td class="text-center">\
						<a href="<?php echo base_url();?>hr/payslip/uploadpayslip?A='+res.A+'&AA='+res.AA+'&AB='+res.AB+'&AC='+res.AC+'&AD='+res.AD+'&AE='+res.AE+'&AF='+res.AF+'&AG='+res.AG+'&AH='+res.AH+'&AI='+res.AI+'&AJ='+res.AJ+'&AK='+res.AK+'&B='+res.B+'&C='+res.C+'&D='+res.D+'&E='+res.E+'&F='+res.F+'&G='+res.G+'&H='+res.H+'&I='+res.I+'&J='+res.J+'&K='+res.K+'&L='+res.L+'&M='+res.M+'&N='+res.N+'&O='+res.O+'&P='+res.P+'&Q='+res.Q+'&R='+res.R+'&S='+res.S+'&T='+res.T+'&U='+res.U+'&V='+res.V+'&W='+res.W+'&X='+res.X+'&Y='+res.Y+'&Z='+res.Z+'&company_name='+company_name+'&company_address='+company_address+'&postcode='+postcode+'&city='+city+'&state='+state+'&country='+country+'&salary_wage='+wage+'" target="_blank" data-toggle="tooltip" data-placement="top" title="View">\
							<i class="fas fa-eye" style="color: #343a40;"></i>\
						</a>\
					</td>';
		return string;
	}

	function disableBtnApprove () {
		isBtnApprove = false;
		$('#btnApprove').attr('disabled',true);
	}

	function enableBtnApprove(){
		isBtnApprove = true;
		$('#btnApprove').removeAttr('disabled');
	}

	function isNotCompanyCode(key,value) {
		result = false;

		if(key != 'Company_No' && key != 'Company No' && key != 'company no' && key != 'company_no'){
			result = true;
		}else{
			companycode = value;
		}

		return result;
	}

	function isNotCompanyName(key,value){
		result = false;

		if(key != 'Company_Name' && key != 'company name' && key != 'Company Name' && key != 'company_name'){
			result = true;
		}else{
			companyName = value;
		}

		return result;
	}
</script>
