<?php include('partials/menu.php'); ?>
  <div class="main-content">
    <div class="wrapper">
      <h1>Update Sub Category</h1>
      <br><br>

      <?php
        //check whether the id is set or not
        if (isset($_GET['id'])) {
          // get the id and all other deteils
          $id=$_GET['id'];
          //create sql to get all other details
          $sql= "SELECT * FROM sub_categories WHERE id=$id";
          //execute the querry
          $res=mysqli_query($conn, $sql);
          //count rows to check whether the id is valid or not
          $count=mysqli_num_rows($res);
          if ($count==1) {
            // get all the data
            $row = mysqli_fetch_assoc($res);
            $title = $row['title'];
            $current_image = $row['image_name'];
            $featured = $row['featured'];
            $active = $row['active'];
          }else {
            //redirect to manage category
            $_SESSION['NO-Category-FOUND'] = "<div class='error'>Category Not Found</div>";
            header('location:'.SITEURL.'backend/manage-sub-category.php');
          }
        }else {
          //redirect to manage category
          header('location:'.SITEURL.'backend/women-category.php');
        }
       ?>


      <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>Current Image:</td>
            <td>
               <?php
                  if ($current_image != "") {
                     //display the img
                     ?>
                     <img src="<?php echo SITEURL; ?>images/sub-category/<?php echo $current_image; ?>" width="100px;">
                     <?php
                  }else {
                    // display msg
                    echo "<div class='error'>Image Not Added.</div>";
                  }
                ?>
            </td>

          </tr>
          <tr>
            <td>New Image:</td>
            <td>
                <input type="file" name="image">
            </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>
          <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="current_image" value="<?php echo $current_image; ?>">
              <input type="hidden" name="id" value="<?php echo $id;?>">
              <input type="submit" name="submit" value="Update Category" class="btnsecondary">
            </td>
          </tr>
        </table>
      </form>
      <?php
        if (isset($_POST['submit'])) {
          //get values from the form
           $id = $_POST['id'];
           $title = $_POST['title'];
           $current_image = $_POST['current_image'];
           $featured=$_POST['featured'];
           $active=$_POST['active'];

           //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_category_".rand(000, 999).'.'.$ext;//e.g food_category_436.jpg

                 $source_path = $_FILES['image']['tmp_name'];
                 $destination_path = "../images/sub-category/".$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.'backed/manage-sub-category.php');
                   //stop the process
                   dir();
                 }

               //B.remove the current img
              if ($current_image != "") {
                 // code...
                 $remove_path = "../images/sub-category/".$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-sub-category.php');
                     dir();//to stop the process
                   }
               }

             }else{
              $image_name= $current_image;
             }
           }else{
             $image_name= $current_image;
           }
           //3.update the db
            $sql2= "UPDATE sub_categories SET
              title = '$title',
              image_name = '$image_name',
              featured = '$featured',
              active = '$active'
              WHERE id=$id
            ";

            //execute the querry
            $res2 = mysqli_query($conn, $sql2);

           //4.redirect to manage category with msg
           //check whether executed or not
           if ($res2==true) {
             // category updated
             $_SESSION['update-category'] = "<div class='success'>Category Updated Successfully.</div>";
             header('location:'.SITEURL.'backend/manage-sub-category.php');
           }else{
             //failed to updated Category
             $_SESSION['update-category'] = "<div class='error'>Failed To Update Category.</div>";
             header('location:'.SITEURL.'backend/manage-sub-category.php');
           }
        }

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