<?php
//tag listánál ez adja vissza a keresés eredményét
ini_set('include_path', '../_class/');
include('class_sql.php');
if ($_SERVER['HTTP_HOST'] == 'badmintoncoach.hu') $sql = new sql('bc_mysql','root','','badminton_coach');
else $sql = new sql('localhost','tollashodos','uprRscU8bGpJ','tollashodos');


//ha members akkor minden AKTÍV, NEM TÖRÖLT kid-et lekéri, név szerint rendezve

$kid_query = "SELECT 
    *
FROM
    (SELECT 
        uk_id,
            uk_name,
            ugk_id,
            uk_is_active,
            uk_deleted,
            ugk_user_group_ug_id,
            ugk_category_ugc_id
    FROM
        user_kid
    LEFT JOIN user_group_kid ON (ugk_user_kid_uk_id = uk_id
        AND ugk_id NOT IN (SELECT 
            ugk_id
        FROM
            user_group_kid
        WHERE
            ugk_user_group_ug_id != ".$_POST['groupId']."))) subtbl
WHERE
    (ugk_user_group_ug_id = ".$_POST['groupId']."
        OR ugk_user_group_ug_id IS NULL)
        AND uk_is_active = 1
        AND uk_deleted = 0
        AND (ugk_category_ugc_id NOT IN (SELECT 
            ugc_id
        FROM
            user_group_category
        WHERE
            ugc_user_group_ug_id != ".$_POST['groupId'].")
        OR ugk_category_ugc_id IS NULL)";


if (!empty($_POST['searchStr'])) {
	if ('members' == $_POST['searchStr']) {
		$kid_query = "select uk_id, uk_name, ugk_id, ugk_user_group_ug_id, ugk_category_ugc_id from user_kid
		left join user_group_kid on ugk_user_kid_uk_id = uk_id
		where ugk_user_group_ug_id = ".$_POST['groupId'];
	}
	elseif ('not_members' == $_POST['searchStr']) {
		$kid_query = "	select uk_id, uk_name,
						(select count(*) from user_group_category where ugc_user_group_ug_id = ".$_POST['groupId']." and ugc_id = ugk_category_ugc_id) as thisGroup,
						if ((select count(*) from user_group_category where ugc_user_group_ug_id = ".$_POST['groupId']." and ugc_id = ugk_category_ugc_id), ugk_id, null) ugk_id,
						null ugk_user_group_ug_id,
						if ((select count(*) from user_group_category where ugc_user_group_ug_id = ".$_POST['groupId']." and ugc_id = ugk_category_ugc_id), ugk_category_ugc_id, null) ugk_category_ugc_id
						from user_kid main_uk
								left join user_group_kid main_ugk on ugk_user_kid_uk_id = uk_id
								where uk_id not in (        
						select ugk_user_kid_uk_id from user_group_kid where ugk_user_group_ug_id = ".$_POST['groupId'].") AND uk_is_active = 1 AND uk_deleted = 0";
	}
	elseif ('members_without_category' == $_POST['searchStr']) {
		$kid_query = "	SELECT 
						    *
						FROM
							user_kid
						JOIN 
						    user_group_kid ON ugk_user_kid_uk_id = uk_id
						WHERE
						    ugk_user_group_ug_id = ".$_POST["groupId"]."
						        AND ugk_category_ugc_id IS NULL";
	}
	elseif (is_numeric($_POST['searchStr'])) {
		# szűrés kategóriára
		$kid_query = "select uk_id, uk_name, ugk_id, ugk_user_group_ug_id, ugk_category_ugc_id from user_kid
		left join user_group_kid on ugk_user_kid_uk_id = uk_id
		where ugk_category_ugc_id = " . $_POST['searchStr'];
	}
	else {
		//keresés
		$kid_query .= " AND uk_name like '".$_POST['searchStr']."%'";
	}
}

$kid_query .= " ORDER BY uk_name ASC;";


$kid_assoc_array = $sql->assoc_array($kid_query);


echo json_encode($kid_assoc_array);

?>