<?php
require_once "../../includes/db.php";
header('Content-Type: application/json');
session_start(['read_and_close' => true]);

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

session_write_close();

$acc_limit  = $_GET['acc_limit'] ?? 10;
$acc_offset = $_GET['acc_offset'] ?? 0;
$storeid = $_GET['storeid'] ?? 0;

$stmt = $pdo->prepare("
SELECT
    'ID' AS id,
    'Company Name' AS org_name,
    'Tele' AS phone,
    'Country' AS country,
    'City' AS city,
    'Business Code' AS biz_code,
    'Satus' AS status,
    1 AS sort_order

UNION ALL

SELECT
    c.id,
    c.org_name,
    c.phone,
    c.country,
    c.city,
    c.biz_code,
    c.status,
    2 AS sort_order
FROM company c
INNER JOIN permissions p
    ON p.co_id = c.id
WHERE p.user_id = :user_id
  AND p.pm_acc_lvl IN (0,2,4,5,6,7)
GROUP BY c.id
ORDER BY sort_order ASC
        LIMIT :acc_limit OFFSET :acc_offset
");

//$stmt->bindValue(':storeid', $storeid, PDO::PARAM_INT);
//$stmt->bindValue(':comp_id', $comp_id, PDO::PARAM_INT);
$stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
//$stmt->bindValue(':comp_id1', $comp_id, PDO::PARAM_INT);
$stmt->bindValue(':acc_limit', (int)$acc_limit, PDO::PARAM_INT);
$stmt->bindValue(':acc_offset', (int)$acc_offset, PDO::PARAM_INT);
$stmt->execute();

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