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

$stmt = $pdo->prepare("
    SELECT 
        COUNT(*) OVER () AS count, 
        p.pm_id, 
        p.pm_acc_id, 
        p.pm_acc_categ,  
        a.id,
        a.acc_name,
        a.acc_categ
    FROM permissions p
    JOIN accounts a 
        ON p.co_id = a.co_id
        AND (
            p.pm_acc_id = a.id 
            OR p.pm_acc_categ = a.acc_categ
        )
    WHERE p.user_id = :uid
        AND p.co_id = :comp_id
        AND p.pm_acc_lvl IN (3, 4, 5, 6, 7) 
        AND p.pm_acc_categ = 'sales_person'
        AND a.status='active' ORDER BY a.acc_name
");
    
$stmt->execute([
    'uid' => $uid,
    'comp_id' => $comp_id
]);

echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));