<?php require('includes/head.php');
require 'connection/conn.php';
 if(isset($_POST['submit']))
 {
     $c=$_POST['code'];
	 $n=$_POST['name'];
	  $g=$_POST['g'];
	  $num2=$_POST['num2'];
	 $old_image=$_POST['old_image'];
	 
	 $target_dir = "item_image/";
    $timestamp = date("Y-m-d_H-i-s"); 
    $file_name = $timestamp . "_" . basename($_FILES["image"]["name"]);
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir . $file_name)) {
        $image = $file_name; 
    } else {
        $image = ""; 
    }
	 if($image=="")
	 {
	$add="update  `items`    set  `group_code`='$g',`sell_price`='$num2' ,`name`='$n', `user`='$login_user' where `code`='$c'";
	 }
	 else
	 {
		$add="update  `items`    set `group_code`='$g',`sell_price`='$num2' ,`name`='$n', `name_latin`='$n2',`user`='$login_user',`item_image`='$image' where `code`='$c'"; 
		unlink('item_image/'.$old_image);
		
	 }
	$conn->query($add);
	echo "<script type='text/javascript'>
		           window.location.href='item_list.php'
		        </script>";
 }
 
 $id=$_GET['id'];
 $check="SELECT * FROM `items` WHERE `code`='$id'";
	$check_r=$conn->query($check);
	$row=mysqli_fetch_array($check_r);
?>


<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"
                      >
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="code" value="<?php echo $id;?>">
<input type="hidden" name="old_image" value="<?php echo $row['item_image'];?>">
</tr>
	
	<tr >
		<td>ناو</td><td><input type="text" name="name" required value="<?php echo $row['name'];?>"></td>
	</tr>
	
	
	
	<tr >
		<td><img height=50px src="item_image/<?php echo $row['item_image'];?>"></td><td><input type="file"  name="image"></td>
	</tr>
	
	<tr >
		<td>نرخی فرۆشتن</td><td><input type="number" step="0.01" name="num2" required value="<?php echo $row['sell_price'];?>"></td>
	</tr>
	<tr >
		<td>کۆدی گروپ</td><td><input type="number" step="0.01" name="g" required value="<?php echo $row['group_code'];?>"></td>
	</tr>
	<tr >
		<td></td><td><input type="submit" name="submit" value="گۆڕین"></td>
	</tr>
  </table>
 
</form>
</div>
                  </div>
                </div>
              </div>
			  
			  </div>
          </div>
        </div>
<?php require'includes/foot.php';?>