<?php include('partials/menu.php'); ?>
  <div class="main-content">
    <div class="wrapper">
      <h1>Update 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 tbl_category 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'];

            $active = $row['status'];
          }else {
            //redirect to manage category
            $_SESSION['NO-Category-FOUND'] = "<div class='error'>Category Not Found</div>";
            header('location:'.SITEURL.'admin/manage-category.php');
          }
        }else {
          //redirect to manage category
          header('location:'.SITEURL.'admin/manage-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>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="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'];

           $active=$_POST['active'];


           //3.update the db
            $sql2= "UPDATE tbl_category SET
              title = '$title',

              status = '$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-category.php');
           }else{
             //failed to updated Category
             $_SESSION['update-category'] = "<div class='error'>Failed To Update Category.</div>";
             header('location:'.SITEURL.'backend/manage-category.php');
           }
        }

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