<?php require_once('../auth/auth.php');   
 $debitor_id=$_GET['debitor_id'];
?>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-4">
				<div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
					<div class="card-header" style="padding: 4px 8px;">
						<h3 class="card-title"> <?php echo language(34);?> </h3>
						<div class="card-tools">
							<button type="button" class="btn btn-tool" data-card-widget="remove">
							<i class="fas fa-times"></i>
							</button>
						</div>
					</div>
                    	<div class="card-body ">
					
							<div class="col-md-12" >
    						   <div class="info-box mt-1" style="background:#e9ecef;">
							<span class="info-box-icon bg-success text-white"><i class="nav-icon fa fa-fw fa-user-secret"></i></span>
							<div class="info-box-content">
								<span class="info-box-number text-bold text-success ">
								   <span>Dr. <ss id="drtval"></ss> <?php echo $currency ?></span>
								   <hr style="background:black;">
							       <span class="text-danger">Cr. <ss id="crtval"></ss> <?php echo $currency ?></span> 
							    </span>
								
    							<small>
    							   <b> 
    								<span  style="float: left;"><?php echo language(244); ?> :  <?php 
									echo $branch_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE  coid='$coid' AND brid='$brid' LIMIT 1 "))['branch_name'];?>  </span>
    								<span  style="float: right;"> <?php echo language(140); ?> : 
    								<?php	echo $count_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM accounts 
    								WHERE coid='$coid' AND bid='$bid'  AND brid='$brid' AND gcatid='$debitor_id' AND active=1  "))['count_row'];  ?></span>
    							   </b>
    							</small>
							</div>
						</div>
						
								<div class="row">
									<?php if( per('cus_add')==1) {?>
									<div class="col-sm-9">
										<div class="form-group">
											<input type="text" class="form-control form-control-sm inputselect" id="trsearch">
										</div>
									</div>
									<div class="col-sm-3">
										<div class="card" style="margin:;">
											<button type="button" class="btn btn-success btn-xs" 
											onclick="pagecall('../customer/customer_add.php?gcatid=<?php echo $debitor_id ?>'+'&ac_type=su-deb','#customer_view')"> <i class="fa fa-plus"></i></button>
										</div>
									</div>
								<?php } else { ?> 
										<div class="col-sm-12">
										<div class="form-group">
											<input type="text" class="form-control form-control-sm inputselect" id="trsearch">
										</div>
									</div>
								<?php } ?>
								</div>
							</div>
						</div>
				
					<div class="card-body table-responsive p-0 cscroll" style="max-height: 450px;">
						<table class="table table-sm table-head-fixed table-hover" >
							<thead>
								<tr>
                                    <th class="text-center" style="width:2%;background:#e9ecef !important;"><?php echo language(15);?></th>
									<th style="width:70%;text-align:left;background:#e9ecef !important;"><?php echo language(215)?></th>
									<th style="width:28%;text-align:right;background:#e9ecef !important;"><?php echo language(233)?></th>
								</tr>
							</thead>
							<?php
								$accoung_query= mysqli_query($conn,"SELECT acid,ac_name,ac_number,outlet_id,balance FROM accounts WHERE coid='$coid'  AND bid='$bid' AND brid='$brid' AND gcatid='$debitor_id' AND active=1   ") or die (mysql_error());
						      	$data = [];
						      	while ($em_row= mysqli_fetch_assoc($accoung_query)){ $data[]=$em_row;}
							?>
						<tbody class="actived" id="debitor_show"></tbody>
					</table>
				</div>
			</div>
		</div>
		<div class="col-md-8" id="customer_view"></div>
	</div>
</div>
</section>
<script type="text/javascript">
	$(document).ready(function(){
		let itdata = '';
		let i = 0;
		let drtval = 0;
		let crtval = 0;

     	$.each(<?php echo json_encode($data) ?>, function(key,value){
	    if(Number(value.balance)>=0) {
	      drtval  += Number(value.balance);
	    } else if(Number(value.balance)<0) {
	      crtval  += Number(-value.balance);  
	    }
	    
	    
    if(Number(value.balance)>=0) {
		itdata += '<tr  onclick="acprofile('+value.acid+')">';
	} else if(Number(value.balance)<0) {
		itdata += '<tr class="bg-danger text-white" onclick="acprofile('+value.acid+')">';
	}
		
		
		
		itdata += '<td style="padding: ;text-align:center;">'+ ++i +'</td>';
		itdata += '<td style="padding: ;text-align:left;">'+ value.ac_name + ','+ value.outlet_id + ',' + value.ac_number +'</td>';
		itdata += '<td style="padding: ;text-align:right;">'+ bden.format(value.balance) +'</td>';
		itdata += '</tr>';
	});
		$("#debitor_show").append(itdata);
		$("#drtval").text(bden.format(drtval));
		$("#crtval").text(bden.format(crtval));

	});

	function acprofile(acid){    
		pagecall('../accounts/ac_profile.php?acid='+acid,'#customer_view');   
	}  
</script>
<script>
	$(document).ready(function(){
		$('#trsearch').on('keyup',function(){
			var searchTerm = $(this).val().toLowerCase();
			$('#debitor_show tr').each(function(){
				var lineStr = $(this).text().toLowerCase();
				if(lineStr.indexOf(searchTerm) === -1){
					$(this).hide();
				}else{
					$(this).show();
				}
			});
		});
	});
</script>				


