<?php
/***
Auth: Vastvikta Nishad
Date: 17 Jan 2024
Description: Display Subcategory and Delete It
*/
?>
<div class="style2-table">
   <div class="style-title">
      <h3>Sub Category </h3>
         <div class="row">
         <?php $new_sub_category = base64_encode('new_subcategory');?>
               <div class="col-sm-3" style="text-align: end;">
               <!-- Link to create a new subcategory -->
               <a href="admin_index.php?token=<?php echo $new_sub_category;?>" class="button-orange1">New Sub Category</a>
               </div>
         </div>   
<div id="success"></div>
      <form name="frmService" action="" method="post" id="frmService">
         <!-- Table to display subcategories -->
         <table class="tableview tableview-2" id="admin_table">
               <thead>
               <!-- Table header row -->
               <tr class="background">
                  <td align="left">S.No.</td>
                  <td align="left">Sub Category Name</td>
                  <td align="left">Category Name</td>
                  <td align="left">Level 1 Escalation</td>
                  <td align="left">Level 2 Escalation</td>
                  <td align="left">Level 3 Escalation</td>
                  <td align="left">Action</td>
               </tr>
            </thead>
               <tbody>
               <?php
                  // Retrieve and display subcategories from the database
                  $resultSet = subcategory_view();
                  // Check if there are rows in the result set
                  if (mysqli_num_rows($resultSet) > 0) {
                     $count = 0; // Reset count for the new iteration
                  // Output data for each row
                  while ($row_doc = mysqli_fetch_array($resultSet)) {
                     $ISubCategoryID = $row_doc['id'];
                     $VSubCategoryName = $row_doc['subcategory'];
                     $VCreatedBy = $row_doc['createdby'];
                     $resolution_time = $row_doc['resolution_time_in_hours'];
                     $second_resolution = $row_doc['second_resolution_time'];
                     $third_resolution = $row_doc['third_resolution_time'];
                     $iCategory = category($row_doc['category']);
                     $level1_users = explode(',', $row_doc['level1_users']);
                     $level2_users = explode(',', $row_doc['level2_users']);
                     $level3_users = explode(',', $row_doc['level3_users']);
                     $count++;
                     $new_subcategory = base64_encode('new_subcategory');
                     $new_sub_category = base64_encode('new_subcategory');
                     $id = base64_encode($row_doc['id']);
                  ?>
                  <!-- Display subcategory details in table rows -->
                  <tr>
                     <td align="left"><?php echo $count?></td>
                     <td align="left"><?php echo $VSubCategoryName?></td>
                     <td align="left"><?php echo $iCategory?></td>
                     <td align="left">
                     <?php echo (empty($resolution_time)) ? '' : $resolution_time.' hours<br>'?>
                     </td>
                     <td align="left">
                     <?php echo (empty($second_resolution)) ? '' : $second_resolution.' hours<br>'?>
                     </td>
                     <td align="left">
                     <?php echo (empty($third_resolution)) ? '' : $third_resolution.' hours<br>'?>
                     </td>
                     <td>
                     <!-- Edit and delete actions for each subcategory -->
                     <a href="javascript:void(0)" onclick="window.location.href='admin_index.php?token=<?php echo $new_sub_category;?>&id=<?php echo  $id ?>';">
                        <img src="<?php echo $SiteURL?>public/images/edit-icon.png" border="0" alt="Edit" />
                     </a>
                     <a href='javascript:void(0);' data-id="<?php echo $row_doc['id'] ?>" class="subcategory_delete">
                        <img src="<?php echo  $SiteURL ?>public/images/delete-icon.png" border="0" alt="delete">
                     </a>
                     </td>
                  </tr>
                  <?php 
                  }
               }else{
                  // Display a message if no subcategories are found
                  echo "<tr><td colspan='7'>No subcategories found.</td></tr>";
               }
               ?>
            </tbody>
         </table>
      </form>              
   </div>
</div>
