


<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	$c2=$_POST['customer'];
	$n=$_POST['note'];
	//$d=$_POST['d'];
	//$d=date("Y-m-d h:i:s");
	$d=$_POST['d'];
	$d=$d." 12:00:00";
	$a=$_POST['amount'];
	
	$add="INSERT INTO `sell_j`( `sell_j_customer`, `sell_j_date`,  `type`,  `amount`, `note`) 
	VALUES ('$c2','$d','q','$a','$n')";
	$conn->query($add);
	echo "<script type='text/javascript'>window.location.href = 'old_debt.php'</script>";	
	
}


if(isset($_GET['t_id']))
{
	$t_id=$_GET['t_id'];
   
	
	$del2="delete from  `sell_j` where `sell_j_id`=$t_id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'old_debt.php'</script>";	
	
}
if(isset($_GET['done_id']))
{
	$t_id=$_GET['done_id'];
	$c_id=$_GET['c_id'];
	$c_date=$_GET['c_date'];
	$a=$_GET['amount'];
	
	
	$s_j=$conn->query("select * from `customer` where `customer_id`=$c_id ");
		$s_j_row=mysqli_fetch_array($s_j);
		$old_num=$s_j_row['statement'];
		$new_num=$old_num+$a;
		
		$up=$conn->query("update customer set `statement`='$new_num' where `customer_id`=$c_id ");
	
	$up="update `sell_j` set status=1 where `sell_j_id`=$t_id";
	$conn->query($up);
	
	$conn->query("INSERT INTO `sell_j_inv`( `inv_type`, `inv_code`, `customer`, `date`, `user`) 
										VALUES ('debt',$t_id,'$c_id','$c_date','$login_user')");
	echo "<script type='text/javascript'>window.location.href = 'old_debt.php'</script>";	
	
}
$c="select * from `customer` where active=1";
$c_r=$conn->query($c);


$check="select * from `sell_j`,`customer` where `sell_j_customer`=`customer_id` and `status`=0 and `type`='q'";
$check_r=$conn->query($check);
?>
<div class="container">
          <div class="page-inner">
            <div class="page-header">
              <h3 class="fw-bold mb-3">قەرزی کۆن</h3>
              <ul class="breadcrumbs mb-3">
                <li class="nav-home">
                  <a href="#">
                    <i class="icon-home"></i>
                  </a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="#"></a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="#"></a>
                </li>
              </ul>
            </div>
			
			<div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <div class="card-title"></div>
                  </div>
<?php if($check_row=mysqli_fetch_array($check_r))
{ $c_id=$check_row['customer_id'];
$c_date=$check_row['sell_j_date'];?>
	<table
                        id="basic-datatables"
                        class="display table table-striped table-hover"
                      >
			<tr>
				<th style="background-color:#d6ebff; font-size:16pt" >کڕیار: <?php echo $check_row['customer_name'];?></th>
				<th style="background-color:#d6ebff; font-size:16pt">بڕی: <?php echo $check_row['amount']."$ /".$check_row['note'];$amount=$check_row['amount']?></th>
				<th style="background-color:#d6ebff; font-size:16pt"><a href="old_debt.php?t_id=<?php echo $check_row['sell_j_id'];?>"><img src="icon/delete.png" width=20px></a></th>
			
				<th style="background-color:#d6ebff; font-size:16pt"><a href="old_debt.php?done_id=<?php echo $check_row['sell_j_id'];?>&&amount=<?php echo $amount;?>&&c_id=<?php echo $c_id;?>&&c_date=<?php echo $c_date;?>"><img src="icon/done.png" width=20px></a></th>
			</tr>
	</table>
	
	
	
	
<?php }
else{ 
if(isset($_POST['search'])){ 
	$c_name=$_POST['n'];
	$get="select `customer_id`,`statement`,`currency` from `customer` where customer_name='$c_name'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$c_id=$get_row['customer_id'];
	$st=$get_row['statement'];
	$cur=$get_row['currency'];
	


?>


			



        <form action="" method="post">
		<input type="hidden" name="customer" value=<?php echo $c_id;?>>
		 <table
                        id="basic-datatables"
                        class="display table table-striped table-hover"
                      >
			
			<tr>
				
				<td  style="background-color:#d6ebff; font-size:16pt">
					<input type="text" style=" font-size:16pt" value="<?php echo $c_name;?>" disabled class="form-control">
				</td>
				
				<td style="background-color:#d6ebff; font-size:16pt">
					<input step="any" type="number" name="amount" placeholder="بڕی پارە" class="form-control">
				</td>
				<td style="background-color:#d6ebff; font-size:16pt"><input type="date" name="d"  required class="form-control"></td>
				<td style="background-color:#d6ebff; font-size:16pt"><input type="text" name="note" class="form-control"></td>

			

				<td style="background-color:#d6ebff; font-size:16pt"><input type="submit" name="submit" value="داخلکردن" class="form-control"></td>
			</tr>
			</table>
        </form>
<?php 
}
else{ ?>
	 <form action="" method="post">
		<table>
			<tr>
				<th>کڕیارەکان</th>
			</tr>
			<tr>
				<td><td><div class="form-group">
					<input type="text" name="n" id="tag" class="form-control"></div>
				</td>
				

			

				<td><input type="submit" name="search" value="گەڕان" class="form-control"></td>
			</tr>
			</table>
        </form>
<?php	
}

} ?>
</div>
</div>
</div>



 <div class="row">
              <div class="col-md-12">
                <div class="card">
                  
                  <div class="card-body">
                    <div class="table-responsive">
                      <table
                        id="basic-datatables"
                        class="display table table-striped table-hover"
                      >
<tr bgcolor="lightblue"><th></th>
				<th>فرۆشیار</th>
				<th>ڕێکەوت</th>
				<th>بڕی پارە</th>
				
			</tr>
<?php
$check2="select * from `sell_j`,`customer` where `sell_j`.`sell_j_customer`=`customer`.`customer_id` and `status`=1 and type='q'  order by `sell_j_date` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
?>
			<tr style="border: solid 1px red;">
				<td style="font-size:18pt"><?php echo $row2['customer_name'];?></td>
				<td><?php echo $row2['sell_j_date'];?></td>
				<td><?php echo $row2['amount'];?></td>
				<td><?php echo $row2['note'];?></td>
				
				
			<th><a onclick='return confirm("دڵنیای لە سڕینەوەی ئەم پسووڵەیە؟؟؟؟");' href="inv_del.php?debt_id=<?php echo $row2['sell_j_id'];?>&&amount=<?php echo $row2['amount'];?>&&c_id=<?php echo $row2['customer_id'];?>"><img src="icon/delete.png" width=30px></a></th>
			</tr>
<?php }?>
</table>  
</div>
                  </div>
                </div>
              </div>
			  
			  </div>
          </div>
        </div>
<?php require'includes/foot.php';?>

<style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="search/jquery.autocomplete.css" />
<script type="text/javascript" src="search/jquery.js"></script>
<script type="text/javascript" src="search/jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
 $("#tag").autocomplete("search/autocomplete_customer_all.php", {
		selectFirst: true
	});
});
</script>