<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Handle subcategory deletion
if (isset($_GET['del'])) {
    $subcatId = intval($_GET['id']);
    $subData = mysqli_query($con, "SELECT subcategory FROM subcategory WHERE id = $subcatId");
    $subRow = mysqli_fetch_array($subData);
    $subName = $subRow['subcategory'] ?? 'Unknown';

    mysqli_query($con, "DELETE FROM subcategory WHERE id = $subcatId");
    log_activity("Deleted Subcategory", "Subcategory Name: $subName (ID: $subcatId)");
    $_SESSION['delmsg'] = "Subcategory deleted successfully!";
    header('location: manage-subcategories.php');
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Manage Subcategories - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"
        crossorigin="anonymous"></script>


</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>



        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-layer-group me-2"></i> Manage Subcategories</h1>
                            <p>Organize product subcategories within categories</p>
                        </div>
                        <a href="add-subcategories.php" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i> Add New Subcategory
                        </a>
                    </div>

                    <!-- Success Message -->
                    <?php if (isset($_SESSION['msg'])) { ?>
                        <div class="alert alert-success">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo htmlentities($_SESSION['msg']); ?>
                            <?php $_SESSION['msg'] = ""; ?>
                        </div>
                    <?php } ?>

                    <?php if (isset($_SESSION['delmsg'])) { ?>
                        <div class="alert alert-danger">
                            <i class="fas fa-trash-alt me-2"></i>
                            <?php echo htmlentities($_SESSION['delmsg']); ?>
                            <?php $_SESSION['delmsg'] = ""; ?>
                        </div>
                    <?php } ?>

                    <!-- Subcategories Table -->
                    <div class="table-card">
                        <table class="table table-hover" id="datatablesSimple">
                            <thead>
                                <tr>
                                    <th width="8%">#</th>
                                    <th>Parent Category</th>
                                    <th>Subcategory Name</th>
                                    <th width="15%">Created</th>
                                    <th width="15%">Updated</th>
                                    <th width="15%">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $query = mysqli_query($con, "SELECT subcategory.*, category.categoryName 
                                                              FROM subcategory 
                                                              JOIN category ON category.id = subcategory.categoryid 
                                                              ORDER BY subcategory.id DESC");
                                $cnt = 1;
                                while ($row = mysqli_fetch_array($query)) {
                                    ?>
                                    <tr>
                                        <td><?php echo $cnt; ?></td>
                                        <td>
                                            <span class="badge bg-light text-dark border">
                                                <?php echo htmlentities($row['categoryName']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span class="badge bg-secondary">
                                                <?php echo htmlentities($row['subcategory']); ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('M d, Y', strtotime($row['creationDate'])); ?></td>
                                        <td><?php echo date('M d, Y', strtotime($row['updationDate'])); ?></td>
                                        <td>
                                            <a href="edit-subcategory.php?id=<?php echo $row['id']; ?>"
                                                class="btn btn-sm btn-outline-primary" title="Edit Subcategory">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="manage-subcategories.php?id=<?php echo $row['id']; ?>&del=delete"
                                                class="btn btn-sm btn-outline-danger" title="Delete Subcategory"
                                                onclick="return confirm('Are you sure you want to delete this subcategory?')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </td>
                                    </tr>
                                    <?php $cnt++;
                                } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        crossorigin="anonymous"></script>
    <script src="js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
    <script src="js/datatables-simple-demo.js"></script>
</body>

</html>