<?php
session_start();
ini_set('session.cookie_secure', 1);
require_once "../../includes/db.php";
header('Content-Type: application/json');

$comp_id = isset($_GET['comp_id']) ? (int)$_GET['comp_id'] : 0;
$uid  =$_SESSION['user_id'];

try {
    $loc_id = isset($_GET['loc_id']) ? intval($_GET['loc_id']) : 0;
    $ord_no = isset($_GET['ord_no']) ? intval($_GET['ord_no']) : 0;
    $t_type = isset($_GET['t_type']) ? trim($_GET['t_type']) : '';

    if ($ord_no === 0 || empty($t_type)) {
        echo '<tr><td colspan="4" class="text-center text-warning">Invalid parameters: ord_no or t_type is missing.</td></tr>';
        exit;
    }

    // First query: the first part of the UNION
    $stmt3_1 = $pdo->prepare("
       SELECT 
             NULL AS t_id, 
        NULL AS t_tt_doc,  
        NULL AS t_doc_no, 
        NULL AS t_date, 
        NULL AS M_date, 
        NULL AS t_cr, 
        NULL AS t_item,
            'RAISED BY' AS p_name, 
            CONCAT(a.firstname,' ', a.secondname) AS t_qty, 
           d.t_sub_date  AS t_credit, 
        NULL AS t_debit,
        NULL AS t_amount,
        NULL AS t_type, 
        NULL AS t_narration, 
        NULL AS t_status, 
        NULL AS t_sub_date, 
        NULL AS acc_name,  
        NULL AS acc_id, 
        NULL AS user_id,
            2 AS row_order 
        FROM transactions d
        JOIN accounts u ON d.t_cr = u.id  LEFT JOIN users a ON a.id= d.t_entry_by
        WHERE d.t_status IN ('Approved', 'Confirmed', 'Pending') 
        AND d.t_copy = 'O' 
        AND d.co_id = :loc_id 
        AND d.t_doc_no = :ord_no 
        AND d.t_type = :t_type
        GROUP BY d.t_doc_no;
    ");
    $stmt3_1->bindValue(':loc_id', $loc_id, PDO::PARAM_INT);
    $stmt3_1->bindValue(':ord_no', $ord_no, PDO::PARAM_INT);
    $stmt3_1->bindValue(':t_type', $t_type, PDO::PARAM_STR);
    $stmt3_1->execute();
    $data3_1 = $stmt3_1->fetchAll(PDO::FETCH_ASSOC);

    // Second query: the second part of the UNION
    $stmt3_2 = $pdo->prepare("
        SELECT 
             NULL AS t_id, 
        NULL AS t_tt_doc,  
        NULL AS t_doc_no, 
        NULL AS t_date, 
        NULL AS M_date, 
        NULL AS t_cr, 
        NULL AS t_item,
            'Confirmed BY' AS p_name, 
            CONCAT(a.firstname,' ', a.secondname) AS t_qty,
            d.t_conf_date  AS t_credit, 
        NULL AS t_debit,
        NULL AS t_amount,
         d.t_type AS t_type, 
        NULL AS t_narration, 
        NULL AS t_status, 
        NULL AS t_sub_date, 
        u.acc_name AS acc_name,  
        NULL AS acc_id, 
        NULL AS user_id,
            2 AS row_order 
        FROM transactions d
        JOIN accounts u ON d.t_cr = u.id  LEFT JOIN users a ON a.id= d.t_conf_by
        WHERE d.t_status IN ('Approved', 'Confirmed', 'Pending')
        AND d.t_copy = 'O' 
        AND d.co_id = :loc_id 
        AND d.t_doc_no = :ord_no 
        AND d.t_type = :t_type
        GROUP BY d.t_doc_no;
    ");
    $stmt3_2->bindValue(':loc_id', $loc_id, PDO::PARAM_INT);
    $stmt3_2->bindValue(':ord_no', $ord_no, PDO::PARAM_INT);
    $stmt3_2->bindValue(':t_type', $t_type, PDO::PARAM_STR);
    $stmt3_2->execute();
    $data3_2 = $stmt3_2->fetchAll(PDO::FETCH_ASSOC);
    
    
   

    // Combine the results from both queries
$data3 = array_merge($data3_1, $data3_2);

// Sort the results by 'row_order'
usort($data3, function ($a, $b) {
    return $b['row_order'] <=> $a['row_order'];
});

if (!empty($data3)) {
    foreach ($data3 as $row) {
        $t_type = $row['p_name'];

        // Default styles
        $lineStyle = "color: #6c757d;"; // Default gray
        $dateStyle = "color: #FFFFFF;";
        $statusStyle = "color: #6c757d;";

        // Assign styles based on transaction type
        switch ($t_type) {
            case 'RAISED BY':
                $lineStyle = " font-size: 14px; color: #6c757d;";
                $dateStyle = "font-weight: bold; font-size: 12px; color: #000;";
                $statusStyle = ($row['t_amount'] < 0) ? "font-weight: bold; font-size: 16px; color: #dc3545;" : "font-weight: bold; font-size: 16px; color: #000;";
                $dateStyle2 = "font-weight: bold; font-size: 10px; color: #0e2bbd;";
                break;
            case 'Confirmed BY':
                  $lineStyle = " font-size: 14px; color: #6c757d;";
                $dateStyle = "font-weight: bold; font-size: 12px; color: #000;";
                $statusStyle = ($row['t_amount'] < 0) ? "font-weight: bold; font-size: 16px; color: #dc3545;" : "font-weight: bold; font-size: 16px; color: #000;";
                break;
            case 'Item':
                $lineStyle = "color: #6c757d;";
                $dateStyle = "color: #000;";
                break;
            case 'SHT/EXC':
                $lineStyle = "font-weight: bold; font-size: 16px; color: #000;";
                $dateStyle = "font-weight: bold; font-size: 16px; color: #000;";
                $statusStyle = ($row['t_amount'] < 0) ? "font-weight: bold; font-size: 16px; color: #dc3545;" : "font-weight: bold; font-size: 16px; color: #000;";
                break;
            case 'TOTAL':
                $lineStyle = "font-weight: bold; font-size: 16px; color: #000;";
                $dateStyle = "font-weight: bold; font-size: 16px; color: #000;";
                $statusStyle = ($row['t_amount'] < 0) ? "font-weight: bold; font-size: 16px; color: #dc3545;" : "font-weight: bold; font-size: 16px; color: #000;";
                break;
        }

        echo '<tr>
        <td style="' . $lineStyle . '">' . htmlspecialchars($row["p_name"]) . '</td>
        <td style="' . $dateStyle . '">';

if (empty($row["t_qty"])) {
    // Display a submit button if t_qty is empty
  echo '<form id="DeleteForm" method="POST">
        <input type="hidden" name="loc_id" value="' . htmlspecialchars($loc_id) . '">
        <input type="hidden" name="ord_no" value="' . htmlspecialchars($ord_no) . '">
        <input type="hidden" name="t_type" value="' . htmlspecialchars($row['t_type']) . '">
        <input type="hidden" name="confby" value="' . htmlspecialchars($row['acc_name']) . '">

        <button type="submit" id="btn_submit" class="btn btn-danger btn-text-light">Delete</button>
    </form>';
} else {
    // Display t_qty value if not empty
    echo htmlspecialchars($row["t_qty"]);
}

echo '</td>

        <td style="' . $dateStyle2 . '">' . htmlspecialchars($row["t_credit"]) . '</td></tr>';

    }
} else {
    echo '<tr><td colspan="4" class="text-center text-warning">No matching records found.</td></tr>';
}


} catch (PDOException $e) {
    echo '<tr><td colspan="4" class="text-center text-danger">Error fetching data: ' . htmlspecialchars($e->getMessage()) . '</td></tr>';
    error_log("Database Error: " . $e->getMessage());
}
?>
