<?php
include "../db.php";
session_start();

// Get the page and items per page from the request
$page = isset($_POST['page']) ? (int)$_POST['page'] : 1;
$perPage = isset($_POST['perPage']) ? (int)$_POST['perPage'] : 2;
$offset = ($page - 1) * $perPage;

$AndWhere = '';
$FirstFilterValue = !empty($_POST['FirstFilterValue']) ? mysqli_real_escape_string($con, $_POST['FirstFilterValue']) : '';
$catFilterValue = !empty($_POST['catFilterValue']) ? mysqli_real_escape_string($con, $_POST['catFilterValue']) : '';

// Build filter conditions
if ($FirstFilterValue && !$catFilterValue) {
    $AndWhere = "AND data_title = '$FirstFilterValue'";
} elseif (!$FirstFilterValue && $catFilterValue) {
    $cat_sql = mysqli_query($con, "SELECT cid FROM category WHERE cname = '$catFilterValue'");
    if ($cat_row = mysqli_fetch_assoc($cat_sql)) {
        $cat_id = $cat_row["cid"];
        $AndWhere = "AND data_subCategory = '$cat_id'";
    }
} elseif ($FirstFilterValue && $catFilterValue) {
    $cat_sql = mysqli_query($con, "SELECT cid FROM category WHERE cname = '$catFilterValue'");
    if ($cat_row = mysqli_fetch_assoc($cat_sql)) {
        $cat_id = $cat_row["cid"];
        $AndWhere = "AND data_title = '$FirstFilterValue' AND data_subCategory = '$cat_id'";
    }
}

// Count total filtered categories for pagination
$total_sql = "SELECT COUNT(*) as total FROM category WHERE 1=1 ";
if ($AndWhere) {
    $total_sql .= "AND cid IN (SELECT DISTINCT data_subCategory FROM data WHERE data_catagory = 'service' $AndWhere)";
}
$total_result = mysqli_query($con, $total_sql);
$total_row = mysqli_fetch_assoc($total_result);
$total_categories = $total_row['total'];
$total_pages = ceil($total_categories / $perPage);

// Fetch filtered categories for the current page
$sql = "SELECT * FROM category WHERE 1=1 ";
if ($AndWhere) {
    $sql .= "AND cid IN (SELECT DISTINCT data_subCategory FROM data WHERE data_catagory = 'service' $AndWhere)";
}
$sql .= " ORDER BY cname ASC LIMIT $offset, $perPage";

$categories = mysqli_query($con, $sql);

if (mysqli_num_rows($categories) > 0) {
    while ($category = mysqli_fetch_assoc($categories)) {
        $cat_name = $category['cname'];
        $cat_id = $category['cid'];

        // Fetch services for the current category
        $service_sql = "SELECT * FROM data WHERE data_catagory = 'service' AND data_up_by = '1' AND data_recycle = '1' AND data_subCategory = '$cat_id' $AndWhere ORDER BY data_title ASC";
        $services = mysqli_query($con, $service_sql);

        if (mysqli_num_rows($services) > 0) {
            echo "<h3 style='color:blue;'>Category: $cat_name</h3><ul>";
            while ($rows = mysqli_fetch_assoc($services)) {
                ?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-9 col-xs-9'>
                <div class="d-flex justify-content-between align-items-center">
                    <div class="intro-admin">
                        <a style='color:orange;font-size:17px;' class='viewDetails' data-toggle='modal'
                            data-target='#adminDetailsModal'>
                            <i class='fa fa-bell' aria-hidden='true'></i> <b><?php echo $rows["data_title"]; ?></b>
                        </a>
                    </div>
                </div>
            </div>
            <div class='col-md-3 d-flex justify-content-between'>
                <a href='service.php?view_id=<?php echo $rows["data_sl_no"]; ?>' class='btn btn-info text-white btn-sm'
                    title='Click to View'><i class='fa fa-eye' aria-hidden='true'></i></a>
                <br>
                <a href='service.php?edit_id=<?php echo $rows["data_sl_no"]; ?>' class='btn btn-primary btn-sm'
                    title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i></a>
                <br>
                <a class="btn btn-primary btn-sm"
                    href="service.php?thumbnail_id=<?php echo $rows['data_sl_no']; ?>">Thumbnail</a>
                <br>
                <a href='service.php?rec_id=<?php echo $rows["data_sl_no"]; ?>' class='delete btn btn-danger btn-sm'
                    title='Click to Delete'><i class='fa fa-trash' aria-hidden='true'></i></a>
            </div>
        </div>
    </div>
</li>
<?php
            }
            echo "</ul>";
        }
    }

    // Pagination controls
    echo "<nav aria-label='Page navigation'><ul class='pagination justify-content-center'>";
    if ($page > 1) {
        echo "<li class='page-item'><a class='page-link' href='#' data-page='" . ($page - 1) . "'>Previous</a></li>";
    }
    for ($i = 1; $i <= $total_pages; $i++) {
        $active = $i == $page ? 'active' : '';
        echo "<li class='page-item $active'><a class='page-link' href='#' data-page='$i'>$i</a></li>";
    }
    if ($page < $total_pages) {
        echo "<li class='page-item'><a class='page-link' href='#' data-page='" . ($page + 1) . "'>Next</a></li>";
    }
    echo "</ul></nav>";
} else {
    echo "<p>No categories found.</p>";
}
?>