<?php require('includes/head.php');
  require 'connection/conn.php';
  if(isset($_POST['add']))
  {
	  $c=$_POST['code'];
	  $check="select * from `items` where `code`='$c'";
	  $check_r=$conn->query($check);
	  if($check_row=mysqli_fetch_array($check_r))
	  {
			$n=$_POST['num'];
			$old_num=$check_row['store_number'];
			$new_num=$old_num-$n;
			$d=$_POST['date'];
			$p=$_POST['person'];
			
			$up="UPDATE `items` SET `store_number`='$new_num' WHERE `code`='$c'";
			$conn->query($up);
		
			$conn->query("insert into `gift`(`gift_number`, `gift_date`, `gift_person`, `gift_code`)  values('$n','$d','$p','$c')");
	   echo "<script type='text/javascript'>
		           window.location.href='gift.php'
		        </script>";
	  }
	  
	  
	  
	 
  }
  elseif(isset($_GET['del_id']))
  {
	  $id=$_GET['del_id'];$c=$_GET['c'];$num=$_GET['num'];
	  $conn->query("delete from `gift` where `gift_id`=$id");
	  
	  $check="select * from `items` where `code`='$c'";
	  $check_r=$conn->query($check);
	  if($check_row=mysqli_fetch_array($check_r))
	  {
			
			$old_num=$check_row['store_number'];
			$new_num=$old_num+$num;
			
			
			$up="UPDATE `items` SET `store_number`='$new_num' WHERE `code`='$c'";
			$conn->query($up);
	  }
  }
  $gift="select * from `gift` ,`items` where `items`.`code`=`gift`.`gift_code`";
  $gift_r=$conn->query($gift);
  
?>
<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">
                      <table id="basic-datatables"  class="display table table-striped table-hover"  >
<thead>
<tr>
	<form action="" method="post">
		<th><input type="text" name="person" required class="form-control" style="width:250px"></th>
		<th><input type="text" name="code" required class="form-control" style="width:250px"></th>
		<th><input type="number" name="num" required class="form-control" style="width:250px"></th>
		<th><input type="date" name="date" required value=<?php echo date('Y-m-d');?> class="form-control" style="width:200px"></th>
		
		<th><input type="submit" name="add" value="زیادکردن" class="form-control"></th>
		</form></tr>
  <tr bgcolor="skyblue">
     
     <th>ناو</th>
     <th>کۆد</th>
	  <th>ژمارە</th>
     <th>بەروار</th>
	 
     <th colspan=2></th>
  </tr>
  </thead>
  <tbody>
  <?php while($row=mysqli_fetch_array($gift_r)) { ?>
    <tr><td><?php echo $row['gift_person'];?></td>
     <td><?php echo $c=$row['gift_code'];?><br><?php echo $row['name'];?></td>
	 <td><?php echo $num=$row['gift_number'];?></td>
	 <td><?php echo $row['gift_date'];?></td>
	  
    <th  width=65px><a onclick='return confirm("are you sure?");' href="gift.php?num=<?php echo $num;?>&&c=<?php echo $c;?>&&del_id=<?php echo $row['gift_id'];?>"><img src="icon/delete.png" width=25px></a></th>
  
     </tr>
  <?php } ?>
  </tbody>
</table>
</div>
                  </div>
                </div>
              </div>
			  
			  </div>
          </div>
        </div>
<?php require'includes/foot.php';?>