<?php
require '../db.php';
include "include/php-functions.php"; 

if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $query = "SELECT * FROM product WHERE pid = $pid";
    $result = mysqli_query($con, $query);
    $rows = mysqli_fetch_assoc($result);

    $client_name = fetchUserName($con, $rows['pClient']);
    $fran_name = fetchUserName($con, $rows['pFranchiee']);
    $subFran_name = fetchUserName($con, $rows['pSubFranchiee']);
    $employee_name = !empty($rows['pEmployee']) && $rows['pEmployee'] != 0 ? fetchUserName($con, $rows['pEmployee']) : '';

    $serviceDetails = fetchServiceDetails($con, $rows["product"]);
    $product = $serviceDetails['product'];
    $pricing = $serviceDetails['pricing'];

    $total_payment = fetchTotalPayment($con, $rows["pid"]);

    $pCat = fetchCategoryName($con, $rows["pCat"]);

    $pStatus = $rows["pStatus"];
    $statusName = '';

    if ($pStatus == 0) {
        $statusName = 'Pending';
    } elseif ($pStatus == 1) {
        $statusName = 'Ongoing';
    } elseif ($pStatus == 2) {
        $statusName = 'Completed';
    } elseif ($pStatus == 3) {
        $statusName = 'Postponed';
    } elseif ($pStatus == 4) {
        $statusName = 'Rejected';
    }
            
    ob_start();
    include 'view_product_content.php'; // Separate PHP file for modal HTML
    $html = ob_get_clean();

    echo json_encode(['html' => $html]);
}
?>