<?php
// Session and Database Connection
if (!isset($_SESSION)) {
    session_start();
    include('../includes/dbcon.php');
}

if (empty($_SESSION['rm_id'])):
    header('Location:../index.php');
    exit;
endif;

// Toggle Category Active Status
if (isset($_GET['cat_id'])) {
    $rid = $_GET['cat_id'];
    list($cat_id, $loc_status) = explode("*", $rid);
    $cval = $loc_status === "Yes" ? 0 : 1;

    $updateQuery = "UPDATE item_category SET cat_active = $cval WHERE cat_id = $cat_id";
    mysqli_query($con, $updateQuery) or die(mysqli_error($con));
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Sales Management | Item Management</title>
    <?php include('../includes/commonheader.php'); ?>
</head>

<body>
    <?php include('../includes/header.php'); ?>
    <?php include('../includes/leftsidebar.php'); ?>

    <div class="main-panel">
        <div class="content-wrapper">
            <!-- Page Header -->
            <div class="page-header">
                <h3 class="page-title">New Item Category</h3>
            </div>

            <!-- Form Section -->
            <div class="col-12 grid-margin">
                <div class="card">
                    <div class="card-body">
                        <form method="post" action="admin_category_add.php" class="needs-validation" enctype="multipart/form-data" onsubmit="return validateForm()" novalidate>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Category Name</label>
                                        <input type="text" class="form-control" id="catname" name="catname" placeholder="Enter category name" required>
                                    </div>
                                </div>
                            </div>
                            <button type="submit" class="btn btn-primary">Save</button>
                            <button type="reset" class="btn btn-light">Cancel</button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Table Section -->
            <div class="col-12 grid-margin stretch-card">
                <div class="card">
                    <div class="card-body">
                        <h4>Item Category - List</h4>
                        <button class="btn btn-warning" onclick="window.location='home.php'">
                            <i class="fas fa-arrow-left"></i> Go Back
                        </button>
                        <div class="table-responsive mt-3">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Category</th>
                                        <th>Active</th>
                                        <th>Edit</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $query = mysqli_query($con, "SELECT * FROM item_category") or die(mysqli_error($con));
                                    $rcount = 0;

                                    while ($row = mysqli_fetch_array($query)) {
                                        $rcount++;
                                        $cat_id = $row['cat_id'];
                                        $cat_des = $row['cat_des'];
                                        $cat_active = $row['cat_active'];
                                        $locactive = $cat_active ? "Yes" : "No";
                                        ?>
                                        <tr>
                                            <td><?php echo $rcount ?></td>
                                            <td><?php echo htmlspecialchars($cat_des) ?></td>
                                            <td>
                                                <?php echo $cat_active ? '&#10004;' : '&#10006;' ?>
                                                <a href="admin_cat_view.php?cat_id=<?= $cat_id . "*" . $locactive ?>">Change</a>
                                            </td>
                                           
                                            
                                              <td><a href="#addvoucher<?php echo $cat_id;?>" data-target="#addvoucher<?php echo $cat_id;?>"data-toggle="modal"     </div> <i class="fas fa-edit "></i>  </i> </a> </td>
                                            </td> 

                             <!----------------------Model ------------------------------->
     <div id="addvoucher<?php echo $row['cat_id'];?>" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
          <div class="modal-dialog" style="width:80%; aria-hidden="true>
          <div class="modal-content" style="height:auto">        
          <div class="modal-body">

             <form class="form-horizontal" method="post" action="admin_cat_edit.php" enctype='multipart/form-data'>

               <div class="form-group">
              <label for="exampleInputEmail1">Category </label>
         

              
              <input type="hidden" class="form-control" id="catid_edit" name="catid_edit" value="<?php echo $row['cat_id'];?>">
                <input type="text" class="form-control" id="cat_des" name="cat_des" value="<?php echo $row['cat_des'];?>">

               </div>

                   <div class="modal-footer">
                   <button type="submit" class="btn btn-primary">Save changes</button>
                   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                   </div>                   
          </form>

   


          </div>
        </div>
      </div>
    </div>
<!--------------- End of Model ------------------------------------>
                                            
                                            
                                            
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php include('../includes/footer.php'); ?>
    </div>

    <?php include('../includes/commonjs.php'); ?>

    <script>
        function validateForm() {
            const categoryName = document.getElementById('catname').value.trim();
            if (!categoryName) {
                alert("Please enter a category name.");
                return false;
            }
            return true;
        }
    </script>
</body>

</html>
