<?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']) : '';



    $clg_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '{$rows["pCollege"]}'");

    if($clg_row = mysqli_fetch_assoc($clg_sql)){

        $clg_name = $clg_row["uname"];

    }



    $course_sql = mysqli_query($con, "SELECT * FROM clg_course WHERE sl_no = '{$rows["product"]}' ");

    while($course_row = mysqli_fetch_assoc($course_sql)){

        $course_name = $course_row["course_name"];

    }

    $cat_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE sl_no = '{$rows["pCat"]}' ");

    while($cat_row = mysqli_fetch_assoc($cat_sql)){

        $pCat = $cat_row["cat_name"];

    }



    $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]);

}

?>