<?php
require_once("dbconn.php");
require_once("phpFunctions.php");

$result = executeQuery($conn, "SELECT * FROM transactions WHERE is_deleted = FALSE AND is_current = TRUE ORDER BY lastupdateby_user DESC");

if ($result === false) { 
    throw new Exception("Error executing query: " . $conn->error);
} 

echo "<div id='CardContainer' class='card-container'>";

if ($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
    {
        echo "<div class='card shadow-sm dr-clickable" . ($row["dr_status"] === 1 ? " border-primary" : "") . "' data-id=" . $row["id"] . " data-status=" . $row["dr_status"] . ">";
			echo "<div class='card-body'>";
				echo "<h4 class='card-title'>";
					echo $row["dr_customer_name"];
				echo "</h4>";
				echo "<h6 class='card-subtitle mb-2 text-muted'>" . ($row["dr_type"] === 1 ? "Delivery" : "Invoice") . "</h6>";
				echo "<hr>";
				echo "<p class='card-text'>Date of Receipt:<br>" . (!empty($row["dr_date_created"]) ? $row["dr_date_created"] : "NA") . "</p>";
				if ($row["dr_type"] === 1) {
					
					echo "<p class='card-text'>DR Number:<br>" . (!empty($row["dr_number"]) ? $row["dr_number"] : "NA") . "</p>";
					
					if($row["dr_status"] === 2) {
						echo "<p class='card-text'>Received Date:<br>" . (!empty($row["received_date"]) ? $row["received_date"] : "NA") . "</p>";
						echo "<p class='card-text'>Receiver:<br>" . (!empty($row["receiver"]) ? $row["receiver"] : "NA") . "</p>";
						echo "<p class='card-text'>Remarks:<br>" . (!empty($row["remarks"]) ? $row["remarks"] : "NA") . "</p>";
					}
				} else {
					echo "<p class='card-text'>Invoice Number:<br>" . (!empty($row["si_number"]) ? $row["si_number"] : "NA") . "</p>";
					
					if($row["dr_status"] === 2) {
						echo "<p class='card-text'>Received Date:<br>" . (!empty($row["received_date"]) ? $row["received_date"] : "NA") . "</p>";
						echo "<p class='card-text'>Receiver:<br>" . (!empty($row["receiver"]) ? $row["receiver"] : "NA") . "</p>";
						echo "<p class='card-text'>Remarks:<br>" . (!empty($row["remarks"]) ? $row["remarks"] : "NA") . "</p>";
					}
				}
			echo "</div>";
        echo "</div>";
    }
}
echo "</div>";
?>