<?php

# HA VAN ID, AKKOR AZ ADOTT EDZÉSRE LEHET GYEREKEKET BEJELEÖLNI
# HA NINCS ID, AKKOR EDZÉSLITA

if ($this->is_id()) {

    # GYEREKEK BEJELÖLÉSE
    # lekérjük, hogy az elmúlt 4 héten kik voltak ilyen típusú edzésen [egyeznie kell a dayofweeknek és a type-nak]

    //alg.: lekérjük, hogy az elmúlt 4 hétben, ezen a napon, ezzel az edzés típussal milyen edzés ID-k vannak
    //presence-ből countoljuk az user_kid ID-kat

    if (!$user->has_authority_by_name('admin') && !user::user_can_edit_training($user->get_ua_id(), $this->get_id()) && training::has_any_coach($this->get_id())) {
        $smarty->display('access_denied.tpl');
    }
    else {
        $presence_query = "SELECT * FROM user_kid ORDER BY uk_name;";
        $presence_assoc_array = $sql->assoc_array($presence_query);
        //végig kell menni rajta h legeneráljuk az usereket

        $users = array();
        //var_dump($presence_assoc_array);
        /*
        foreach ($presence_assoc_array as $presence) {
            $user = new user_kid();
            $user->set_user_data_by_id($presence['uk_id']);
            $users[] = $user;
        }
        */

        //training data
        $training = new training();
        $training->set_training_data_by_id($this->get_id());

        //SZŰRÉS
        $tr_ids = array();
        /*for ($i=1; $i <=4 ; $i++) {
            $actual_date = date("Y-m-d H:i:s" , strtotime($training->get_tr_date() . " - " . $i . " weeks"));
            $tr_ids_query = "SELECT * FROM training WHERE tr_deleted = 0 AND tr_date = '" . $actual_date . "' and tr_training_type_trt_id = " . $training->get_tr_training_type_trt_id() . ";";
            $tr_id = $sql->single_variable($tr_ids_query);
            if ($tr_id) $tr_ids[] = $tr_id;
        }
        //végigmegyünk az edzésekkel a presencen
        $presence_query = "
        SELECT `pr_user_kid_uk_id` , count( `pr_id` ) AS 'presence'
        FROM `presence`
        JOIN user_kid ON uk_id = pr_user_kid_uk_id
        JOIN training ON tr_id = pr_training_tr_id
        WHERE `pr_training_tr_id`".
        (!empty($tr_ids)?"IN ( " . implode(',', $tr_ids) . " ) ":" ")
        ."AND `uk_deleted` = 0
        AND `tr_deleted` = 0
        AND `uk_is_active` = 1
        GROUP BY `pr_user_kid_uk_id`
        ORDER BY count( `pr_id` ) DESC, uk_name ASC;
        ";
        //echo $presence_query;
        if (!empty($tr_ids)) {
            $presence_assoc_array = $sql->assoc_array($presence_query);
            foreach ($presence_assoc_array as $presence) {
                $user = new user_kid();
                $user->set_user_data_by_id($presence['pr_user_kid_uk_id']);
                $users[] = $user;
            }
        }*/

        //legeneráljuk a maradékot
        $exeptions = array();
        $rest_users = array();
        foreach ($users as $user) {
            $exeptions[] = $user->get_uk_id();
        }

        /*
        $rest_user_query = "
            SELECT
                uk_id
            FROM
                user_kid
                    LEFT JOIN
                money_deposit ON mod_user_kid_uk_id = uk_id
                    AND mod_expire_date > NOW()
                    AND mod_deleted = 0
                    LEFT JOIN
                lease ON l_id = mod_lease_l_id
                    LEFT JOIN
                lease_training_type ON ltt_lease_l_id = l_id
            WHERE
                uk_deleted = 0 AND uk_is_active = 1
                    AND ((uk_lease = 1 AND mod_id IS NOT NULL AND ltt_training_type_trt_id = {$training->get_tr_training_type_trt_id()})
                    OR (uk_lease = 0 AND mod_lease_l_id IS NULL))
            GROUP BY uk_id
            ORDER BY uk_name;
        ";
        */
        $rest_user_query = "
            SELECT
                *
            FROM
                (SELECT DISTINCT
                    uk_id, uk_name
                FROM
                    user_kid
                -- LEFT JOIN user_kid_training_type ON uktt_user_kid_uk_id = uk_id
                LEFT JOIN user_kid_lease ON ukl_user_kid_uk_id = uk_id
                LEFT JOIN lease_training_type ON ltt_lease_l_id = ukl_lease_l_id
                WHERE
                    (uk_deleted = 0 AND uk_is_active = 1)
                        AND (ltt_training_type_trt_id = {$training->get_tr_training_type_trt_id()}
                        OR uk_lease = 0) UNION SELECT DISTINCT
                    uk_id, uk_name
                FROM
                    user_kid
                LEFT JOIN money_deposit ON mod_user_kid_uk_id = uk_id
                    AND mod_expire_date > NOW()
                    AND mod_deleted = 0
                LEFT JOIN lease ON l_id = mod_lease_l_id
                LEFT JOIN lease_training_type ON ltt_lease_l_id = l_id
                WHERE
                    uk_deleted = 0 AND uk_is_active = 1
                        AND ((uk_lease = 1 AND mod_id IS NOT NULL
                        AND ltt_training_type_trt_id = {$training->get_tr_training_type_trt_id()})
                        OR (uk_lease = 0 AND mod_lease_l_id IS NULL))  UNION SELECT DISTINCT
                        uk_id, uk_name
                    FROM
                        presence
                    JOIN user_kid ON pr_user_kid_uk_id = uk_id
                    WHERE
                        pr_training_tr_id = {$training->get_tr_id()}) AS res
            ORDER BY uk_name
            ;
        ";

        //var_dump($rest_user_query);
        $rest_user_assoc_array = $sql->assoc_array($rest_user_query);
        foreach ($rest_user_assoc_array as $rest_user) {
            $user = new user_kid();
            $user->set_user_data_by_id($rest_user['uk_id']);
            $rest_users[] = $user;
        }

        //TRAINING-COACH ARRAY
        $trc_query = "SELECT * FROM training_coach JOIN user_coach ON ua_id = trc_coach_uc_id WHERE trc_helper = 0 AND ua_deleted = 0 AND trc_training_tr_id = " . $this->get_id();
        $trc_coaches = array();
        $trc_assoc_array = $sql->assoc_array($trc_query);
        foreach ($trc_assoc_array as $trc_data) {
            $new_coach = new user();
            $new_coach->set_user_data_by_id($trc_data['trc_coach_uc_id']);
            $trc_coaches[] = $new_coach;
        }

        //TRAINING-HELPER ARRAY
        $trc_query = "SELECT * FROM training_coach JOIN user_coach ON ua_id = trc_coach_uc_id WHERE trc_helper = 1 AND ua_deleted = 0 AND trc_training_tr_id = " . $this->get_id();
        $trc_helpers = array();
        $trc_assoc_array = $sql->assoc_array($trc_query);
        foreach ($trc_assoc_array as $trc_data) {
            $new_coach = new user();
            $new_coach->set_user_data_by_id($trc_data['trc_coach_uc_id']);
            $trc_helpers[] = $new_coach;
        }

        //var_dump($users);

        //headcount
        $headcount_query = "SELECT * FROM presence WHERE pr_training_tr_id = " . $this->get_id() . ";";
        $headcount = $sql->num_of_rows($headcount_query);


        //csak lista
        $s_users = array();
        $presence_query = "
        SELECT `pr_user_kid_uk_id`
        FROM `presence`
        JOIN user_kid ON uk_id = pr_user_kid_uk_id
        WHERE `pr_training_tr_id` = ".$this->get_id()."
        AND uk_deleted = 0
        ORDER BY uk_name ASC;
        ";


        $presence_assoc_array = $sql->assoc_array($presence_query);
        foreach ($presence_assoc_array as $presence) {
            $s_user = new user_kid();
            $s_user->set_user_data_by_id($presence['pr_user_kid_uk_id']);
            $s_users[] = $s_user;
        }

        //lekérjük a hitelalkalom beállítást
        $credit = $sql->single_variable("SELECT setv_int FROM setting_value JOIN setting ON setv_setting_set_id = set_id WHERE set_name = 'Hitelalkalom';");


        $smarty->assign('training', $training);
        $smarty->assign('trainings', $tr_ids);
        $smarty->assign('headcount', count($presence_assoc_array));
        $smarty->assign("trc_coaches", $trc_coaches);
        $smarty->assign("trc_helpers", $trc_helpers);
        $smarty->assign('presence_assoc_array', $presence_assoc_array);
        $smarty->assign('users', $users);
        $smarty->assign('sorted_users', $s_users);
        $smarty->assign('rest_users', $rest_users);
        $smarty->assign('tr_id', $this->get_id());
        $smarty->assign('has_credit', $credit !== null);
        $smarty->assign('credit', $credit);

        $smarty->display("presence.tpl");
    }
}

else {
    if ($user->has_authority_by_name('admin')) {
        $traning_list_query = "SELECT * FROM training WHERE tr_deleted = 0 ORDER BY tr_date DESC;";
    }
    else {
        $traning_list_query = "
            SELECT DISTINCT
                tr_id
            FROM
                training
                    LEFT JOIN
                training_coach ON trc_training_tr_id = tr_id
            WHERE
                tr_deleted = 0 AND (trc_coach_uc_id = ". $user->get_ua_id() ." OR trc_coach_uc_id IS NULL)
            ORDER BY tr_date DESC;
        ";
    }
    $training_list_assoc_array = $sql->assoc_array($traning_list_query);

    $training_array = array();
    foreach ($training_list_assoc_array as $training_list_array) {
        $training = new training();
        $training->set_training_data_by_id($training_list_array['tr_id']);
        $training_array[] = $training;
    }
    //var_dump($training_array);
    $smarty->assign('edit', $tpl);
    $smarty->assign("training_array", $training_array);
    $smarty->display("presence_list.tpl");


}





?>
