<?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'];
    $description=$_POST['description'];
    $id=intval($_POST['id']); // hidden input from modal
    $sql=mysqli_query($bd, "update category set categoryName='$category',categoryDescription='$description',updationDate='$currentTime' where id='$id'");
    $_SESSION['msg']="✅ Category Updated !!";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin | Category</title>

<!-- Bootstrap & 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.10.5/font/bootstrap-icons.css" rel="stylesheet">

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

/* Header & Sidebar */
.navbar {
    background: linear-gradient(45deg, #ff6600, #ff9933);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
}
.sidebar {
    height: 100vh;
    background: #ff6600;
    color: #fff;
    padding-top: 1rem;
    position: fixed;
    width: 250px;
    top: 60px;
    overflow-y: auto;
}
.sidebar a { color:#fff; display:block; padding:10px 20px; text-decoration:none;}
.sidebar a:hover, .sidebar a.active { background:#ff9933; color:#fff; border-radius:5px; }

.main-content {
    margin-left: 260px;
    margin-top: 70px;
    padding: 30px;
}

.card { border-radius:10px; box-shadow:0 4px 15px rgba(0,0,0,0.1); background:#fff; }
.card-header { background: linear-gradient(90deg, #ff6600, #ff9933); color:#fff; font-weight:600; border-radius:10px 10px 0 0; }
.btn-custom { background:#ff6600; color:#fff; border:none; border-radius:6px; transition:0.3s; }
.btn-custom:hover { background:#ff9933; }

footer { background:#fff; padding:15px; text-align:center; margin-left:260px; border-top:1px solid #ddd; color:#333;}
</style>
</head>
<body>

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

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

<!-- Main Content -->
<div class="main-content">
    <div class="container-fluid">
        <h4>Categories</h4>

        <?php
        $query=mysqli_query($bd,"SELECT * FROM category");
        while($row=mysqli_fetch_array($query)){ ?>
            <div class="card mb-2">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <strong><?php echo htmlentities($row['categoryName']);?></strong>
                        <p class="mb-0"><?php echo htmlentities($row['categoryDescription']);?></p>
                    </div>
                    <button class="btn btn-custom btn-sm" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $row['id']; ?>">
                        <i class="bi bi-pencil-fill"></i> Edit
                    </button>
                </div>
            </div>

            <!-- Modal -->
            <div class="modal fade" id="editModal<?php echo $row['id']; ?>" tabindex="-1" aria-labelledby="editModalLabel<?php echo $row['id']; ?>" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header" style="background: linear-gradient(90deg, #ff6600, #ff9933); color:white;">
                    <h5 class="modal-title" id="editModalLabel<?php echo $row['id']; ?>">Edit Category</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                  </div>
                  <div class="modal-body">
                    <form method="post">
                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                        <div class="mb-3">
                            <label class="form-label">Category Name</label>
                            <input type="text" name="category" class="form-control" value="<?php echo htmlentities($row['categoryName']);?>" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Description</label>
                            <textarea name="description" class="form-control" rows="4" required><?php echo htmlentities($row['categoryDescription']);?></textarea>
                        </div>
                        <button type="submit" name="submit" class="btn btn-custom w-100">Update</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>
        <?php } ?>
    </div>
</div>

<!-- Footer -->
<footer>© <?php echo date("Y"); ?> Admin Dashboard. All rights reserved.</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php } ?>
