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

 ?>
 <div class="main-content">
   <div class="wrapper">
     <h1>Add Sub Category</h1>
     <br><br>
     <?php
     if (isset($_SESSION['add'])) {
       echo $_SESSION['add'];//displaying session msg
       unset($_SESSION['add']);//REMOVING SESSION MSG
       // code...
     }
     if (isset($_SESSION['upload'])) {
       echo $_SESSION['upload'];//displaying session msg
       unset($_SESSION['upload']);//REMOVING SESSION MSG
       // code...
     }
      ?>
     <!--add category form starts here-->
     <form action="" method="post" enctype="multipart/form-data">
       <table class="tbl">
         <tr>
           <td>Title</td>
           <td>
             <input type="text" name="title" placeholder="Category Title">
           </td>
         </tr>
         <tr>
           <td>Select Image:</td>
           <td>
             <input type="file" name="image">
           </td>
         </tr>
         <tr>
          <td>Category:</td>
          <td>
            <select name="category">
              <?php
                //create php code to display categories from db
                //1.create sql to get all active categories frm db
                $sql4 ="SELECT * FROM tbl_category WHERE status='Yes'";
                //execute the sql
                $res4= mysqli_query($conn, $sql4);
                //count rows to check whether we have categories or not
                $count4 = mysqli_num_rows($res4);
                if ($count4>0) {

                  // we have categories
                  while ( $row4=mysqli_fetch_assoc($res4)) {
                  // get the details of categories
                  $id = $row4['id'];
                  $title = $row4['title'];

                  ?>
                  <option value="<?php echo $id; ?>"><?php echo $title; ?></option>
                  <?php
                  }
                }else{
                  //we don't have categories
                  ?>
                  <option value="1">No Category Found</option>
                  <?php
                }
                //2. display on dropdown
               ?>

            </select>
          </td>
        </tr>
         <tr>
           <td>Featured:</td>
           <td>
             <input type="radio" name="featured" value="Yes">Yes
             <input type="radio" name="featured" value="No">No
           </td>
         </tr>
         <tr>
           <td>Active: </td>
           <td>
             <input type="radio" name="active" value="Yes">Yes
             <input type="radio" name="active" value="No">No
           </td>
         </tr>
         <tr>
           <td colspan="2">
             <input type="submit" name="submit" value="Add Category" class="btnsecondary">
              <input type="hidden" name="category-id" value="<?php echo $id ?>;" class="btnsecondary">
           </td>
         </tr>
       </table>
     </form>
      <!--add category form ends here-->
      <?php
        //check if the submit btn is clicked or not
        if (isset($_POST['submit'])) {
          //echo "clicked";
          //1. get the values from form
          $title = $_POST['title'];
          $id = $_POST['category-id'];
          //2.for radio-input we need to check first if the btn is selected or not
          if(isset($_POST['featured'])){
            //get the value frm the form
            $featured=$_POST['featured'];
          }else{
            //set the default value
            $featured="No";
          }
          if(isset($_POST['active'])){
            //get the value frm the form
            $active=$_POST['active'];
          }else{
            //set the default value
            $active="No";
          }
          //check whether the image is selected or not and add the value for image accordingly
          //print_r($_FILES['image']);
          //dir();//BREAK THE CODE HERE
          if (isset($_FILES['image']['name'])) {
            // UPLOAD the image
             //to upload the image we need image_name, source path and destination path
             $image_name=$_FILES['image']['name'];
             //upload the img only if img is selected
             if ($image_name != "") {
               // 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 = "men_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.'backend/manage-sub-category.php');
                 //stop the process
                 dir();
               }
             }

          }else{
            //don't upload the image and set the iamge_name value as blank
             $image_name="";
          }
          //2. create sql to insert category into db
          $sql= "INSERT INTO sub_categories SET
          title='$title',
          image_name = '$image_name',
          featured='$featured',
          category_id = '$id',
          active = '$active'

          ";
          //3.execute the querry and save in db
          $res=mysqli_query($conn, $sql);
          //4.check whether the query executed or not and data addded or not
          if ($res==true) {
            // querry executed and category added
            $_SESSION['add'] = "<div class='success'>Category Added Successfully.</div>";
            //redirect to the category page
            header('location:'.SITEURL.'backend/manage-sub-category.php');
          }else{
            //failed to add category
            $_SESSION['add'] = "<div class='error'>failed to add category .</div>";
            //redirect to the category page
            header('location:'.SITEURL.'backend/manage-sub-category.php');
          }
        }
       ?>
   </div>
 </div>
 <?php
   include('partials/footer.php');
  ?>
