<?php include('partials/menu.php');?>

<?php
  //check whether id is set or not
  if(isset($_GET['id'])){
    //get the details
    $id=$_GET['id'];
    //create sql to get the details
    $sql2 = "SELECT * FROM products WHERE id=$id";
    //execute the query
    $res2 = mysqli_query($conn, $sql2);
    $count2=mysqli_num_rows($res2);
    //count rows to check whether the id is valid or not
    if ($count2==1) {
      // code...
      //get the value based on query executed
      $row2 = mysqli_fetch_assoc($res2);
      //get the individual value of selected food
      $title = $row2['title'];
      $description = $row2['description'];
      $price = $row2['price'];
      $current_image = $row2['image_name'];
      $current_category = $row2['sub_category_id'];
      $featured = $row2['featured'];
      $active = $row2['active'];
      $size = $row2['size'];

    }else {
      header('location:'.SITEURL.'backend/manage-products.php');
    }

  }else {
    header('location:'.SITEURL.'backend/manage-products.php');
  }

?>

<div class="main-content">
  <div class="wrapper">
    <h1>Update Cloth</h1>
    <br><br><br>
    <form action="" method="post" enctype="multipart/form-data">
      <table class="tbl">
        <tr>
          <td>Title:</td>
          <td>
            <input type="text" name="title" value="<?php echo $title; ?>" >
          </td>
        </tr>
        <tr>
          <td>Description:</td>
          <td>
            <textarea cols="30" rows="10" name="description"><?php echo $description; ?></textarea>
          </td>
        </tr>
        <tr>
          <td>Price:</td>
          <td>
              <input type="number" name="price" value="<?php echo $price ?>">
          </td>

        </tr>
        <tr>
          <td>Current Image:</td>
          <td>
            <?php
              if ($current_image=="") {
                // img not available
                echo "<div class='error'>Image Not Added.</div>";
              }else {
                //img is available
                ?>
                  <img src="<?php echo SITEURL; ?>images/products/<?php echo $current_image; ?>" alt='<?php echo $title; ?>' width="100px">
                <?php
              }
             ?>
          </td>
        </tr>
        <tr>
          <td>Size:</td>
          <td>
              <input type="text" name="size" value="<?php echo $size ?>">
          </td>

        </tr>
        <tr>
          <td>Select New Image:</td>
          <td>
            <input type="file" name="image">
          </td>
        </tr>
        <tr>
          <td>Category:</td>
          <td>
            <select name='category'>
              <?php
                //query to get active categories
                $sql = "SELECT * FROM sub_categories WHERE active='Yes'";
                //execute the query
                $res = mysqli_query($conn, $sql);
                //count rows
                $count = mysqli_num_rows($res);
                //check whether category available or not
                if($count>0){

                  //category available
                  while ($row=mysqli_fetch_assoc($res)) {
                    $category_title = $row['title'];
                    $category_id = $row['id'];
                    ?>
                    <!--we add if statement to get the category which the current food is in-->
                      <option <?php if($current_category==$category_id){echo "selected";} ?> value="<?php echo $category_id; ?>"><?php echo $category_title; ?></option>
                    <?php
                  }
                }else {
                  //category not available
                  echo "<option value='0'>Category Not available.</option>";
                }

              ?>

            </select>
          </td>
        </tr>
        <tr>
          <td>Featured:</td>
          <td>
            <input <?php if($featured=="Yes"){echo "checked";} ?> type="radio" name="featured" value="Yes">Yes
            <input <?php if($featured=="No"){echo "checked";}?> type="radio" name="featured" value="No">No
        </td>
        <tr>
          <td>Active:</td>
          <td>
            <input <?php if($active=="Yes"){echo 'checked';} ?> type="radio" name="active" value="Yes">Yes
            <input <?php if($active=="No"){echo 'checked';} ?> type="radio" name="active" value="No">No
          </td>
        </tr>
        <tr>
          <td >
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input type="hidden" name="current_image" value="<?php echo $current_image; ?>">

            <input type="submit" name="update" value="Update Cloth" class="btnsecondary">
          </td>
        </tr>
      </table>
    </form>
    <?php
      //check if btn is clicked
      if (isset($_POST['update'])) {
        //1.GET ALL THE DETAILS FRM THE FORM
        $id = mysqli_real_escape_string($conn,$_POST['id']);
        $title = mysqli_real_escape_string($conn,$_POST['title']);
        $description = mysqli_real_escape_string($conn,$_POST['description']);
        $price = mysqli_real_escape_string($conn,$_POST['price']);
        $current_image = mysqli_real_escape_string($conn,$_POST['current_image']);
        $category = mysqli_real_escape_string($conn,$_POST['category']);
        $featured = mysqli_real_escape_string($conn,$_POST['featured']);
        $active = mysqli_real_escape_string($conn,$_POST['active']);
        $size = mysqli_real_escape_string($conn,$_POST['size']);


        //2. UPLOAD THE IMG IF SELECTED
          //check whether upload btn is clicked or not
          //2. updating new img if selected
            //check whether the img is selected or not
            if (isset($_FILES['image']['name'])) {
              // get the img details
              $image_name = $_FILES['image']['name'];
              //check whether the img is available or not
              if ($image_name != "") {
                // img is available

                  //A. upload the img
                  //Auto rename our image
                  //get the extension of our image (jpg,png etc)
                  $ext = end(explode('.', $image_name));
                  //rename the image
                  $image_name = "women_clothes_name_".rand(000, 999).'.'.$ext;//e.g food_category_436.jpg

                  $source_path = $_FILES['image']['tmp_name'];
                  $destination_path = "../images/products/".$image_name;
                  //finally upload the image
                  $upload = move_uploaded_file($source_path, $destination_path);

                  //check whether the image is uploaded or not
                  //if the image is not uploaded thn we will stop the proccess and redirect with error msg
                  if ($upload==false) {
                    // set msg
                    $_SESSION['upload'] = "<div class='error'>Failed to upload the image</div>";
                    //redirect to add-category
                    header('location:'.SITEURL.'backend/manage-products.php');
                    //stop the process
                    dir();
                  }

                //B.remove the current img
               if ($current_image != "") {
                  // code...
                  $remove_path = "../images/products/".$current_image;
                  $remove = unlink($remove_path);
                   //check whether the image is removed or not
                   //if failed to remove the display msg and stop the process
                    if ($remove==false) {
                      //Failed to remove the img
                      $_SESSION['remove-failed'] = "<div class='error'>Failed to remove current image.</div>";
                      header('location:'.SITEURL.'backend/manage-products.php');
                      dir();//to stop the process
                    }
                }

              }else{
               $image_name= $current_image;
              }
            }else{
              $image_name= $current_image;
            }

        //4.UPDATE THE FOOD IN database
        $sql3 = "UPDATE products SET
          title = '$title',
          description = '$description',
          price = $price,
          image_name = '$image_name',
          sub_category_id = '$category',
          featured = '$featured',
          active = '$active',
          size = '$size'
          WHERE id=$id
        ";
        //execute the querry
        $res3 = mysqli_query($conn, $sql3);

       //4.redirect to manage category with msg
       //check whether executed or not
         if ($res3==true) {
           // category updated

           $_SESSION['update-food'] = "<div class='success'>Cloth Updated Successfully.</div>";
           header('location:'.SITEURL.'backend/manage-products.php');
         }else{
           //failed to updated Category
           $_SESSION['update-food'] = "<div class='error'>Failed To Update Cloth.</div>";
           header('location:'.SITEURL.'backend/manage-products.php');
         }
        //5. REDIRECT TO MANAGE-FOOD
      }

     ?>


  </div>
</div>

<?php include('partials/footer.php');?>
