<?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['reason'];
			
			$up="UPDATE `items` SET `store_number`='$new_num' WHERE `code`='$c'";
			$conn->query($up);
		
			$conn->query("insert into `destroy`(`destroy_number`, `destroy_date`, `destroy_reason`, `destroy_code`)  values('$n','$d','$p','$c')");
	   echo "<script type='text/javascript'>
		           window.location.href='destroy.php'
		        </script>";
	  }
	  
	  
	  
	 
  }
  elseif(isset($_GET['del_id']))
  {
	  $id=$_GET['del_id'];$c=$_GET['c'];$num=$_GET['num'];
	  $conn->query("delete from `destroy` where `destroy_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);
	  }
  }
  $destroy="select * from `destroy`,`items` where `items`.`code`=`destroy`.`destroy_code` ";
  $destroy_r=$conn->query($destroy);
  
?>
<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="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="text" name="reason" required class="form-control" style="width:250px"></th>
		
		<th><input type="submit" name="add" value="زیادکردن" class="form-control" style="width:200px"></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($destroy_r)) { ?>
    <tr>
     <td><?php echo $c=$row['destroy_code'];?><br><?php echo $row['name'];?></td>
	 <td><?php echo $num=$row['destroy_number'];?></td>
	 <td><?php echo $row['destroy_date'];?></td>
	  <td><?php echo $row['destroy_reason'];?></td>
    <th  width=65px><a onclick='return confirm("are you sure?");' href="destroy.php?num=<?php echo $num;?>&&c=<?php echo $c;?>&&del_id=<?php echo $row['destroy_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';?>



  