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

 ?>
 <div class="main-content">
   <div class="wrapper">
     <h1>Add 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>status: </td>
           <td>
             <input type="radio" name="status" value="Yes">Yes
             <input type="radio" name="status" value="No">No
           </td>
         </tr>
         <tr>
           <td colspan="2">
             <input type="submit" name="submit" value="Add Category" 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'];
          //2.for radio-input we need to check first if the btn is selected or not

          if(isset($_POST['status'])){
            //get the value frm the form
            $active=$_POST['status'];
          }else{
            //set the default value
            $active="No";
          }

          //2. create sql to insert category into db
          $sql= "INSERT INTO tbl_category SET
          title='$title',
          status = '$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-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/add-category.php');
          }
        }
       ?>
   </div>
 </div>
 <?php
   include('partials/footer.php');
  ?>
