<?php
	$general->pageHeader($rModule['title']);
	$employees=$db->selectAll($general->table(74),'where isActive =1 order by eName asc'); 
	$general->arrayIndexChange($employees,'eID');
?>
<div class="white-box border-box">
	<div class="row">
		<div class="col-md-12">           
			<?php
				$designations=$db->selectAll($general->table(72));
				$general->arrayIndexChange($designations,'edID');
				$tSalary=0;
				$empDatas=array();
				$seIDs=$db->selectAll($general->table(74),'where isActive =1 and eSalary>0');
				$general->arrayIndexChange($seIDs,'eID');
				$eIDs=[];
				if(!empty($seIDs)){
					foreach($seIDs as $s){
						$eIDs[$s['eID']]=$s['eID'];                  
					}
				}
				if(!empty($eIDs)){
					$sdDue=$db->selectAll($general->table(20),'where sdRef in ('.implode(',',$eIDs).') group by sdRef' ,'sum(sdAmount)-sum(sdCollect) as due, sdRef as eID');
				$general->arrayIndexChange($sdDue,'eID');
				}

				$eTSalayr=0;
			?>
			<div class="col-sm-12 col-md-12 col-lg-12">
				<table class="table table-striped table-bordered table-hover only_show">
					<thead>
						<tr>
							<th style="width: 5%;">SN</th>
							<th>Employee</th>
							<th>Designation</th>
							<th class="amount_td">Salary</th>
							<th class="amount_td">Balance</th>
							<th class="amount_td">Balance-Salary</th>
							<th class="amount_td">Sale Due</th>     
							<th class="amount_td">Pay</th>
							<th class="amount_td">statement </th>

						</tr>
					</thead> 
					<tbody>
						<?php
							$total=1;
							$tBalance=0;
							$teBalanceSalary=0;
							foreach($seIDs as $eID=>$e){
								$balance=0;
								if($e['hID']>0){
									$balance=-$acc->headBalance($e['hID']);
								}
								$tBalance+=$balance;
								$eTSalayr+=$e['eSalary'];
								$data=[
									'eName'     => $e['eName'], 
									'edTitle'   => $designations[$e['edID']]['edTitle'],
									'eSalary'   => $e['eSalary'],
									'balance'   => $balance,
									//'bs'        => $e['eSalary']-$balance,
									'due'       => @$sdDue[$e['eID']]['due']
								];
								$empDatas[$eID]=$data;
								$teBalanceSalary+=$e['eSalary']+$balance;

						?>
							<tr id="emp_<?php echo $eID;?>">
								<td><?php echo $total++;?></td>
								<td><?php echo $e['eName'];?></td>
								<td><?php echo $designations[$e['edID']]['edTitle'];?></td>
								<td class="amount_td"><?php echo $general->numberFormat($e['eSalary'],0);?></td>
								<td class="amount_td balance"><?php echo $general->numberFormat($balance,0);?></td>
								<td class="amount_td balance"><?php echo $general->numberFormat($e['eSalary']+$balance,0);?></td>
								<td class="amount_td due"><?php echo (float)@$sdDue[$e['eID']]['due'];?></td>
								<td class="amount_td">
									<a href="javascript:void()" class="btn btn-success" onclick="payEmployeeSalaryInit('<?php echo $eID;?>')">Pay</a>
								</td>
								<td><a href="<?php echo URL.'/?mdl=headStatement&hID='.$e['hID'].'&scId='.$e['scID'] ;?>" class="btn btn-success" target="_blank">Statement</a></td>
							</tr>
						<?php
							}
						?>
						<tr>
							<td>&nbsp;</td>
							<td colspan="2"><b>Total</b></td>
							<td class="amount_td"><b><?php echo $general->numberFormat($eTSalayr);?></b></td>
							<td class="amount_td"><b><?php echo $general->numberFormat($tBalance);?></b></td>
							<td class="amount_td"><b><?php echo $general->numberFormat($teBalanceSalary);?></b></td>
							<td>&nbsp;</td>
						</tr>
					</tbody>
				</table>

				<script type="text/javascript">
					<?php echo 'var empDatas='.json_encode($empDatas).';';?>
				</script>
			</div>
		</div>
	</div>
</div>

<div style="display: none;">
	<a href="javascript:void()" data-toggle="modal" data-target="#payEmployeeSalaryModel" id="payEmployeeSalaryModelAct"></a>
</div>
<div class="modal fade" id="payEmployeeSalaryModel" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="modalTitle"></h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body">
				<div class="receiveAmount">
					<input type="hidden" value="" id="eIDe">
					<?php $general->inputBoxText('employe','Employee','','','','disabled');?>
					<?php $general->inputBoxText('employeDesignation','Designation','','','','disabled');?>
					<?php $general->inputBoxText('employeeSalayr','Salary','','','amount_td','disabled');?>
					<?php $general->inputBoxText('employeeBalance','Balance','','','amount_td','disabled');?>
					<?php $general->inputBoxText('employeeDue','Due','','','amount_td','disabled');?>
					<?php $general->inputBoxText('payAmount','Amount','','','amount_td');?>
					<?php $general->inputBoxText('payNote','Note');?>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
					<button type="button" onclick="payEmployeeSalary()" class="btn btn-info" id="salaryPayBtn">Submit</button>
				</div>
			</div>
		</div>
	</div>
</div>