<?php
session_start();
if (isset($_POST["empId"])) {
    $empId = $_POST["empId"];
    $date = $_POST["selectedDate"];
    $loggedInUserDepartment = $_SESSION['teamLeads'];
    $obj = new ImageDetails($empId, $date, $loggedInUserDepartment);
}else {
  header('location: ../../login.php');
}

class ImageDetails
{

    function __construct($empId, $date, $loggedInUserDepartment)
    {
        require_once dirname(__DIR__) . '../../php/DB_Connect.php';
        $DBclassObject = new DB_conncect(); //encapsulation
        $this->db = $DBclassObject->connectToDB();

        $this->fetchImgDetails($empId, $date, $loggedInUserDepartment);
    }

    function fetchImgDetails($empId, $date, $loggedInUserDepartment)
    {
        $imageDeleteStatus = "false";
        $siteVisitType = "NV";
        $billAmount = '';

        // if ($loggedInUserDepartment == TL_DEPARTMENT) {
        //     $billAmount = "`TL_APPROVED_AMOUNT`";
        // } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
        //     $billAmount = "`FINANCE_APPROVED_AMOUNT`";
        // }

        $query = $this->db->prepare("SELECT `IMAGE_URL`, `BILL_TITLE`,`DESCRIPTION`, `FINANCE_APPROVED_AMOUNT`, `AMOUNT`, `TL_APPROVED_AMOUNT` FROM `fta-app-images` WHERE `EMP_ID`=? AND `DATE`=? AND `IMAGE_DELETED`=? AND `SITE_VISIT_TYPE`=? ORDER BY `BILL_TITLE`");
        $query->bind_param("ssss", $empId, $date, $imageDeleteStatus, $siteVisitType);
        $query->execute();
        $query->bind_result($imageUrl, $billTitle, $description, $FinanceApprovedBillAmount, $enginnerBillAmount, $TLApprovedBillAmount);
        $query->store_result();
        $return_arr = array();
        if ($query->num_rows() > 0) {

            while ($query->fetch()) {
                $billTitleId=str_replace(" ","_",$billTitle);
                $return_arr[] =
                    array(
                        "imageUrl" => $imageUrl,
                        "billTitle" => $billTitle,
                        "billTitleId" => $billTitleId,
                        "FinanceApprovedBillAmount" => $FinanceApprovedBillAmount,
                        "engineerBillAmount" => $enginnerBillAmount,
                        "TLApprovedBillAmount" => $TLApprovedBillAmount,
                        "description" => $description
                    );
            }

            echo  json_encode($return_arr);
        } else {
            echo json_encode($return_arr);
        }
    }
}
?>
