<?php include('partials/menu.php');?>
<div class="main-content">
  <div class="wrapper">
    <h1>Add Product</h1>

    <br><br>
    <?php
      if (isset($_SESSION['upload'])) {
        echo $_SESSION['upload'];//displaying session msg
        unset($_SESSION['upload']);//REMOVING SESSION MSG
        // code...
      }

     ?>
     <?php
         //check whether the btn is clicked or not
         if (isset($_POST['submit'])) {
           //1.GET THE DATA FRO FORM
            $title =mysqli_real_escape_string($conn, $_POST['title']);
            $description =mysqli_real_escape_string($conn,$_POST['description']);
            $price = mysqli_real_escape_string($conn, $_POST['price']);
            $category = mysqli_real_escape_string($conn,$_POST['category']);
            $size = mysqli_real_escape_string($conn,$_POST['size']);
            //CHECK if featured and active are checked or not
            if (isset($_POST['featured'])) {
              $featured = $_POST['featured'];
            }else {
               $featured = "No";//setting default value
            }
            if (isset($_POST['active'])) {
              $active = $_POST['active'];
            }else {
               $active = "No";//setting default value
            }
           //2UPLOAD IMG IF SELECTED
           //check if the select img is clicked or not and upload the img only if the img selected
           if (isset($_FILES['image']['name'])) {
             // get the details of the selected img
             $image_name = $_FILES['image']['name'];
             //check whether the img is selected or not and upload img only if selected
             if ($image_name != "") {
               // image is selected
               //A. rename the img
               //get the ext of selected img e.g jpg,png
               $ext = end(explode('.', $image_name));
               //create the name for img
               $image_name = "products-name-".rand(0000, 9999).".".$ext;

               //B. upload the img
               //get the sourcepath and the destination path
               //source path is the current location of the img
               $src = $_FILES['image']['tmp_name'];
               //destination path for the img to be uploaded
               $dst = "../images/products/".$image_name;
               //finally upload food img
               $upload = move_uploaded_file($src, $dst);
               //check whether the img is uploaded or not
               if ($upload==false) {
                 // failed to upload the img
                 //redirect to add food with error msg
                  $_SESSION['upload'] = "<div class='error'>Failed to upload the image</div>";
                  header('location:'.SITEURL.'backend/manage-products.php');
                 //stop the process
                 dir();//to stop the process
               }
             }
           }else {
              $image_name = "";//setting default value as blank
           }
           //3. INSERT data TO DB
           //create sqlto save data in the db
           $sql2 = "INSERT INTO products SET
           title = '$title',
           description = '$description',
           price = $price,
           qty = $qty,
           image_name = '$image_name',
           sub_category_id = '$category',
           featured = '$featured',
           active = '$active',
           size  ='$size'
           ";
           //execute the querry
           $res2 = mysqli_query($conn, $sql2);
           //check whether the data is inserted or not
           //4.REDIRECT WITH MSG TO MANAGE FOOD PAGE
           if ($res2 == true) {
             // data is inserted Successfully
             //echo "food added Successfully";
             $_SESSION['ADD'] = "<div class='success'>Food added Successfully.</div>";
             header('location:'.SITEURL.'backend/manage-products.php');
           }else {
             //failed to insert data
             $_SESSION['ADD'] = "<div class='error'>Failed to Add food.</div>";
             header('location:'.SITEURL.'backend/manage-products.php');
           }

         }
     ?>
    <form action="" method="post" enctype="multipart/form-data">
      <table class="tbl">
        <tr>
          <td>Title:</td>
          <td>
            <input type="text" name="title" placeholder="Title of the cloth">
          </td>
        </tr>
        <tr>
          <td>Description:</td>
          <td>
             <textarea name="description" cols="30" rows="10" placeholder="Description Of The Cloth"></textarea>
          </td>
        </tr>
        <tr>
          <td>Price:</td>
          <td>
            <input type="number" name="price">
          </td>
        </tr>
        <tr>
          <td>Qty:</td>
          <td>
            <input type="number" name="qty">
          </td>
        </tr>

        <tr>
          <td>Select Image:</td>
          <td>
            <input type="file" name="image">
          </td>
        </tr>
        <tr>
          <td>size:</td>
          <td>
            <select name='size'>
              <option>Small</option>
              <option>Medium</option>
              <option>Large</option>
              <option>Extra Large</option>
            </select>
          </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
                $sql ="SELECT * FROM sub_categories WHERE active='Yes'";
                //execute the sql
                $res= mysqli_query($conn, $sql);
                //count rows to check whether we have categories or not
                $count = mysqli_num_rows($res);
                if ($count>0) {

                  // we have categories
                  while ( $row=mysqli_fetch_assoc($res)) {
                  // get the details of categories
                  $id = $row['id'];
                  $title = $row['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 Cloth" class="btnsecondary">
          </td>
        </tr>
      </table>
    </form>

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