<?php
ini_set('session.cookie_secure', 1);
session_start(['read_and_close' => true]);
require_once "../../includes/db.php";
header('Content-Type: text/html; charset=UTF-8');

$comp_id = $_GET['comp_id'] ?? 0;
$user_id = $_SESSION['user_id'];

session_write_close();

try {
   $store_id = isset($_GET['store_id']) ? intval($_GET['store_id']) : 0;

    $stmt3 = $pdo->prepare("
SELECT 
    u.co_id,
    u.id,
    u.acc_name,
    u.acc_categ
FROM accounts u
WHERE u.acc_categ = 'cash_wallet'

AND EXISTS (
    SELECT 1
    FROM permissions p
    WHERE p.co_id = u.co_id
      AND p.user_id = ?    
      AND p.co_id = ?
      AND p.pm_acc_lvl IN (0,4,5,6,7)

      AND (
            -- account match
            p.pm_acc_id = u.id
            
          
            OR (
                (p.pm_acc_id IS NULL OR p.pm_acc_id = '' OR p.pm_acc_id = 0)
                AND TRIM(p.pm_acc_categ) = TRIM(u.acc_categ)
            )
      )
)
    ");

    //$stmt3->bindParam(':comp_id', $comp_id, PDO::PARAM_INT);
    $stmt3->execute([$user_id,$comp_id]);
    $stmt3->execute();
    
    $data3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);

    if (!empty($data3)) {
        foreach ($data3 as $row) {
            $t_type = $row['t_type'] ?? ''; // Ensure $t_type is defined
            
    // Default styles
            $lineStyle = "color: #6c757d;"; // Default gray
            $dateStyle = "color: #008080;";
            $statusStyle = "color: #6c757d;";


            echo '<tr class="data-row" style="border: none;">
                <td style="' . $dateStyle . '">
                    <a data-toggle="modal" class="item fetch-acc_Statement_run" data-target="#Search_Statement_run"
                       data-loc_id="' . htmlspecialchars($row["co_id"]) . '"
                       data-aid="' . htmlspecialchars($row["id"]) . '"
                       data-accname="' . htmlspecialchars($row["acc_name"]) . '">
                      ' . htmlspecialchars($row["acc_name"] ?? '') . '
                    </a>
                </td>
                
                
            </tr>';

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