<?php
session_start();
include('include/config.php');
if(strlen($_SESSION['alogin'])==0){	
    header('location:index.php');
} else {
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('d-m-Y h:i:s A', time());

    if(isset($_POST['submit'])) {
        $category = $_POST['category'];
        $subcat = $_POST['subcategory'];
        mysqli_query($bd, "INSERT INTO subcategory(categoryid, subcategory) VALUES('$category', '$subcat')");
        $_SESSION['msg'] = "SubCategory Created !!";
    }

    if(isset($_GET['del'])) {
        mysqli_query($bd, "DELETE FROM subcategory WHERE id = '".$_GET['id']."'");
        $_SESSION['delmsg'] = "SubCategory deleted !!";
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin | SubCategory</title>

<!-- ✅ Bootstrap 5 & Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

<style>
body {
    font-family: 'Poppins', sans-serif;
    background-color: #f7f8fa;
    color: #333;
    margin: 0;
    padding: 0;
}

/* ✅ Fixed Full-Width Header */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    background: #1f1f1f;
    color: #fff;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 25px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.15);
}

/* ✅ Sidebar */
.sidebar {
    background: linear-gradient(180deg, #ff7a00, #ffb366);
    color: #fff;
    min-height: calc(100vh - 60px);
    padding-top: 1rem;
    position: fixed;
    width: 250px;
    top: 60px;
    left: 0;
    overflow-y: auto;
    box-shadow: 4px 0 10px rgba(0,0,0,0.1);
}
.sidebar a {
    color: #fff;
    display: block;
    padding: 12px 20px;
    border-radius: 10px;
    text-decoration: none;
    transition: 0.3s;
}
.sidebar a:hover, .sidebar a.active {
    background: rgba(255,255,255,0.25);
}

/* ✅ Main Content */
.content {
    margin-left: 260px;
    padding: 100px 40px 40px 40px;
}

/* ✅ Card Styling */
.card {
    border: none;
    border-radius: 15px;
    box-shadow: 0 8px 20px rgba(0,0,0,0.08);
}
.card:hover {
    transform: translateY(-3px);
    transition: all 0.3s ease-in-out;
}

/* ✅ Table Styling */
.table thead th {
    background: #ff7a00;
    color: #fff;
    border: none;
}
.table-striped tbody tr:nth-of-type(odd) {
    background-color: #fff5ec;
}
.table-hover tbody tr:hover {
    background-color: #ffe3cc;
}

/* ✅ Responsive */
@media (max-width: 991px) {
    .navbar {
        position: relative;
    }
    .sidebar {
        position: relative;
        top: 0;
        width: 100%;
        min-height: auto;
    }
    .content {
        margin-left: 0;
        padding: 80px 20px;
    }
}
</style>
</head>
<body>

<!-- ✅ Header -->
<?php include('include/header.php'); ?>

<!-- ✅ Sidebar -->
<div class="sidebar">
    <?php include('include/sidebar.php'); ?>
</div>

<!-- ✅ Main Content -->
<div class="content">
    <div class="card p-4 mb-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h4 class="mb-0 fw-semibold">
                <i class="bi bi-diagram-3 text-warning"></i> Create SubCategory
            </h4>
            <span class="text-muted small"><?php echo $currentTime; ?></span>
        </div>

        <!-- ✅ Success Message -->
        <?php if(isset($_SESSION['msg']) && $_SESSION['msg']!=""){ ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle-fill me-2"></i><strong>Success!</strong> <?php echo htmlentities($_SESSION['msg']);?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php $_SESSION['msg']=""; } ?>

        <form method="post">
            <div class="mb-3">
                <label class="form-label fw-semibold">Category</label>
                <select name="category" class="form-select" required>
                    <option value="">Select Category</option>
                    <?php 
                    $query = mysqli_query($bd, "SELECT * FROM category");
                    while($row = mysqli_fetch_array($query)) { ?>
                        <option value="<?php echo $row['id'];?>"><?php echo $row['categoryName'];?></option>
                    <?php } ?>
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label fw-semibold">SubCategory Name</label>
                <input type="text" name="subcategory" class="form-control" placeholder="Enter SubCategory Name" required>
            </div>

            <button type="submit" name="submit" class="btn btn-primary">
                <i class="bi bi-save"></i> Create
            </button>
        </form>
    </div>

    <!-- ✅ Manage Subcategories -->
    <div class="card p-4">
        <h4 class="mb-3 fw-semibold">
            <i class="bi bi-list-ul text-warning"></i> Manage SubCategories
        </h4>

        <?php if(isset($_SESSION['delmsg']) && $_SESSION['delmsg']!=""){ ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-trash-fill me-2"></i><strong>Deleted!</strong> <?php echo htmlentities($_SESSION['delmsg']);?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php $_SESSION['delmsg']=""; } ?>

        <div class="table-responsive">
            <table class="table table-striped table-bordered table-hover align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Category</th>
                        <th>SubCategory</th>
                        <th>Creation Date</th>
                        <th>Last Updated</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $query = mysqli_query($bd, "SELECT subcategory.id, category.categoryName, subcategory.subcategory, subcategory.creationDate, subcategory.updationDate FROM subcategory JOIN category ON category.id=subcategory.categoryid");
                    $cnt = 1;
                    while($row = mysqli_fetch_array($query)) { ?>
                        <tr>
                            <td><?php echo htmlentities($cnt);?></td>
                            <td><?php echo htmlentities($row['categoryName']);?></td>
                            <td><?php echo htmlentities($row['subcategory']);?></td>
                            <td><?php echo htmlentities($row['creationDate']);?></td>
                            <td><?php echo htmlentities($row['updationDate']);?></td>
                            <td>
                                <a href="edit-subcategory.php?id=<?php echo $row['id']?>" class="btn btn-sm btn-primary me-2">
                                    <i class="bi bi-pencil-square"></i>
                                </a>
                                <a href="subcategory.php?id=<?php echo $row['id']?>&del=delete" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this subcategory?')">
                                    <i class="bi bi-trash"></i>
                                </a>
                            </td>
                        </tr>
                    <?php $cnt++; } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- ✅ Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
<?php } ?>
