<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	$c2=$_POST['store'];
	$n=$_POST['note'];
	$d=$_POST['d'];
	$a=$_POST['amount'];
	
	$add="INSERT INTO `pay`(`pay_store`, `pay_date`, `note`, `amount`) 
	VALUES ('$c2','$d','$n','$a')";
	$conn->query($add);
	echo "<script type='text/javascript'>window.location.href = 'pay.php'</script>";	
	
}


if(isset($_GET['t_id']))
{
	$t_id=$_GET['t_id'];
   
	
	$del2="delete from  `pay` where `pay_id`=$t_id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'pay.php'</script>";	
	
}
if(isset($_GET['done_id']))
{
	$t_id=$_GET['done_id'];
	$c_id=$_GET['c_id'];
	$c_date=$_GET['c_date'];
	
	$up="update `pay` set status=1 where `pay_id`=$t_id";
	$conn->query($up);
	
	$conn->query("INSERT INTO `purchase_inv`( `inv_type`, `inv_code`, `store`, `date`, `p_i_user`) 
										VALUES ('pay',$t_id,'$c_id','$c_date','$login_user')");
	echo "<script type='text/javascript'>window.location.href = 'pay.php'</script>";	
	
}
$c="select * from `store` where active=1";
$c_r=$conn->query($c);


$check="select * from `pay`,`store` where `pay`.`pay_store`=`store`.`store_id` and `status`=0";
$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="home.php">
                    <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-body">
                    <div class="table-responsive">
<?php if($check_row=mysqli_fetch_array($check_r))
{ $c_id=$check_row['store_id'];
$c_date=$check_row['pay_date'];?>
	 <table id="basic-datatables"class="display table table-striped table-hover">
			<tr>
				<th>فرۆشیار: <?php echo $check_row['store_name'];?></th>
				<th>بڕی: <?php echo $check_row['amount']."$ /".$check_row['note'];?></th>
				<th><a href="pay.php?t_id=<?php echo $check_row['pay_id'];?>"><img src="icon/delete.png" width=20px></a></th>
			
				<th><a href="pay.php?done_id=<?php echo $check_row['pay_id'];?>&&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{ ?>
        <form action="" method="post">
		<table>
			<tr>
				<th>فرۆشیارەکان</th>
			</tr>
			<tr>
				<td>
					<select name="store" required class="form-select">
							<option value=""></option>
							<?php while($row2=mysqli_fetch_array($c_r)){ ?>
						<option value="<?php echo $row2['store_id'];?>"><?php echo $row2['store_name'];?></option>
						 <?php }?>
					</select>
				</td>
				<td>
					<input type="text" name="note" placeholder="Note" class="form-control">
				</td>
				<td>
					<input step="any" type="number" name="amount" placeholder="بڕی پارە" class="form-control">
				</td>
				<td><input type="date" name="d" value="<?php echo date("Y-m-d");?>" required class="form-control"></td>

			

				<td><input type="submit" name="submit" value="واصلکردن" class="form-control"></td>
			</tr>
			</table>
        </form>
<?php } ?>
</div>
</div>


<table border=1 dir=rtl width=80% align="center" style="font-size:18pt" id="basic-datatables"
                        class="display table table-striped table-hover">
<tr bgcolor="lightblue">
				<th>فرۆشیار</th>
				<th>ڕێکەوت</th>
				<th>بڕی پارە</th>
				<th></th><th></th>
			</tr>
<?php
$check2="select * from `pay`,`store` where `pay`.`pay_store`=`store`.`store_id` and `status`=1 order by `pay_date` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
?>
			<tr>
				<td><?php echo $row2['store_name'];?></td>
				<td><?php echo $row2['pay_date'];?></td>
				<td><?php echo $row2['amount'];?></td>
				<td><?php echo $row2['note'];?></td>
				<th><a onclick='return confirm("دڵنیای لە سڕینەوەی ئەم پسووڵەیە؟؟؟؟");' href="inv_del.php?pay_id=<?php echo $row2['pay_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';?>