<div id="page-wrapper">
    <div id="page-inner">
        <div class="row g-4">

            <!-- Add / Edit Form -->
            <div class="col-md-6">
                <?php
            if (session_status() == PHP_SESSION_NONE) session_start();
            if (!empty($_SESSION['message'])): ?>
                <div class="custom-alert alert alert-info" id="customAlert">
                    <?= $_SESSION['message'] ?>
                    <span class="close-btn float-end" onclick="this.parentElement.style.display='none'">&times;</span>
                </div>
                <?php unset($_SESSION['message']); endif; ?>

                <div class="card">
                    <div class="card-header text-white">
                        <?= isset($_GET['edit']) ? 'Edit Category' : 'Add Category' ?>
                    </div>
                    <div class="card-body">
                        <?php
                    $editData = null;
                    if (isset($_GET['edit'])) {
                        foreach ($categorys as $cat) {
                            if ($cat['id'] == $_GET['edit']) {
                                $editData = $cat;
                                break;
                            }
                        }
                    }
                    ?>
                        <form method="post" enctype="multipart/form-data">
                            <input type="hidden" name="id" value="<?= $editData['id'] ?? '' ?>">
                            <input type="hidden" name="old_image" value="<?= $editData['cat_image'] ?? '' ?>">
                            <div class="mb-3">
                                <label>Parent Category</label>
                                <select name="parent_id" class="form-control">
                                    <option value="">Main Category</option>
                                    <?php foreach($categorys as $cat): ?>
                                    <option value="<?= $cat['id'] ?>"
                                        <?= (isset($editData['parent_id']) && $editData['parent_id'] == $cat['id']) ? 'selected' : '' ?>>
                                        <?= $cat['cat_title'] ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="mb-3">
                                <label>Status</label>
                                <select name="status" class="form-control">
                                    <option value="1"
                                        <?= (isset($editData['status']) && $editData['status']==1) ? 'selected' : '' ?>>
                                        Active</option>
                                    <option value="0"
                                        <?= (isset($editData['status']) && $editData['status']==0) ? 'selected' : '' ?>>
                                        Inactive</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label>Category Title</label>
                                <input type="text" name="cat_title" class="form-control" required
                                    placeholder="Enter Category Title" value="<?= $editData['cat_title'] ?? '' ?>">
                            </div>

                            <!-- <div class="mb-3">
                                <label>Description</label>
                                <textarea name="cat_description" class="form-control"
                                    rows="3"><?= $editData['cat_description'] ?? '' ?></textarea>
                            </div> -->

                            <div class="mb-3">
                                <label>Upload Image</label>
                                <input type="file" name="cat_image" class="form-control" accept=".png,.jpg,.jpeg,.webp">
                                <?php if (!empty($editData['cat_image'])): ?>
                                <img src="../assets/images/category/<?= $editData['cat_image'] ?>" width="80"
                                    class="mt-2 rounded">
                                <?php endif; ?>
                            </div>

                            <button type="submit" name="<?= isset($editData) ? 'edit' : 'add' ?>"
                                class="btn btn-primary w-100">
                                <?= isset($editData) ? 'Update Category' : 'Add Category' ?>
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Category List -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-dark text-white">Category List</div>
                    <div class="card-body">
                        <table class="table table-bordered table-striped">
                            <thead class="table-dark">
                                <tr>
                                    <th>#</th>
                                    <th>Parent</th>
                                    <th>Status</th>
                                    <th>Title</th>
                                    <th>Image</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (!empty($categorys)): ?>
                                <?php foreach($categorys as $index => $cat): ?>
                                <tr>
                                    <td><?= $index + 1 ?></td>
                                    <td>
                                        <?php
                                        if ($cat['parent_id']) {
                                            foreach($categorys as $p){
                                                if($p['id'] == $cat['parent_id']){
                                                    echo $p['cat_title'];
                                                }
                                            }
                                        } else {
                                            echo "Main";
                                        }
                                        ?>
                                    </td>

                                    <td>
                                        <?= $cat['status'] ? 'Active' : 'Inactive' ?>
                                    </td>
                                    <td><?= htmlspecialchars($cat['cat_title']) ?></td>
                                    <td>
                                        <?php if (!empty($cat['cat_image'])): ?>
                                        <img src="../assets/images/category/<?= $cat['cat_image'] ?>" width="50">
                                        <?php else: ?>
                                        <span class="text-muted">No image</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="?edit=<?= $cat['id'] ?>" class="btn btn-sm btn-warning"><i
                                                class="fas fa-edit"></i></a>
                                        <a href="?delete=<?= $cat['id'] ?>" class="btn btn-sm btn-danger"
                                            onclick="return confirm('Are you sure to delete this category?')">
                                            <i class="fas fa-trash-alt"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php else: ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted">No categories found</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>