<div class="buttons">
    <a href="/admin/lock_training/{$training->get_tr_id()}" class="addbutton {if !$training->get_tr_locked()}delete{else}add{/if}-big">
        {if !$training->get_tr_locked()}
            Lezárás
        {else}
            Feloldás
        {/if}
    </a>
</div>

<div class="list">
    <div class="list_item">
        <label class="desc" id="title1" for="tr_date">
            <img src="/_image/training.png">
            Dátum:
        </label>
        <div>
            {$training->get_tr_date()|substr:0:4}.
            {$months[$training->get_tr_date()|substr:5:2]}
            {$training->get_tr_date_day()}.
            {$days[$training->get_tr_date_day_of_week()]}
            {$training->get_tr_date_time()}
        </div>
    </div>

    <div class="list_item">
        <label class="desc" id="title2" for="tr_training_type_trt_id">
            <img src="/_image/tipus.png">
            Típus:
        </label>
        <div>
            {$training->get_tr_type_name_by_id()}
        </div>
    </div>

    <div class="list_item">
        <label class="desc" id="title1" for="tr_duration">
            <img src="/_image/time.png">
            Időtartam:
        </label>
        <div>{$training->get_tr_duration()} perc</div>
    </div>

    {if $trc_coaches || $trc_helpers}
    <div class="list_item">
        <label class="desc" id="title1" for="coaches">
            <img src="/_image/coach.png">
            Edző(k):
        </label>
        <div style="display: inline-block;">
        {foreach $trc_coaches as $coach}
            {$coach->get_ua_name()}<br>
        {/foreach}
        {foreach $trc_helpers as $coach}
            <span class="italic">{$coach->get_ua_name()}</span><br>
        {/foreach}
        </div>
    </div>
    {/if}

    <div class="list_item">
        <label class="desc" id="title1" for="tr_headcount">
            <img src="/_image/letszam.png">
            Létszám:
        </label>
        <div id="headcount">
            {$headcount} fő
        </div>
    </div>
</div>


<input type="hidden" id="tr_id" value="{$tr_id}">
<input type="hidden" id="admin_id" value="{$user_login->get_ua_id()}">

<br><br><br>

{if !$training->get_tr_locked()}
    <div class="list">
    {foreach $users as $user}
        {if $user@first ||
            $users[$user@index]->get_uk_presence_on_previous_trainings($trainings) !=
            $users[$user@index-1]->get_uk_presence_on_previous_trainings($trainings)
        }
        <span class="date_separator">
            {$users[$user@index]->get_uk_presence_on_previous_trainings($trainings)}/4
        </span>
        {/if}
        <div class="{if $user->get_uk_presence($tr_id)}name_tag_checked{else}name_tag{/if} width70" id="{$user->get_uk_id()}">
            <img src="/_image/shuttlecock.png">
            <div>{$user->get_uk_name()}</div>
        </div>

    {/foreach}
    </div>

    <br><br>

    <div class="list">
    {foreach $rest_users as $rest_user}
        <div class="{if $rest_user->get_uk_presence($tr_id)}name_tag_checked{else}name_tag{/if} width70" id="{$rest_user->get_uk_id()}">
            <img src="/_image/shuttlecock.png">
            <div>{$rest_user->get_uk_name()}</div>
        </div>
    {/foreach}
    </div>

{else}
    <div class="list">
        {foreach $sorted_users as $user}
            <div class="list_item">
                <img src="/_image/shuttlecock.png">
                {$user->get_uk_name()}
            </div>
        {/foreach}
    </div>

{/if}

<script>



$('.name_tag').click(function() {
    //alert($(this).css('backgroundColor'));
    var user_id = $(this).attr('id');
    var x = $(this).css('backgroundColor');
    hexc(x);
    var checked = (color == '#e2edff');
    var tr_id = $("#tr_id").val();
    //ha kék, akkor zöldre változtatja, egyébként fordítva
    if (color == '#e2edff') {
        $(this).css('backgroundColor', '#1eea0b');
        var el = $('#headcount');
        var num = parseInt(el.text());
        el.text(num+1).append(" fő");

    }
    else {
        $(this).css('backgroundColor', '#e2edff');
        var el = $('#headcount');
        var num = parseInt(el.text());
        el.text(num-1).append(" fő");
    }
    //alert(checked);

    var admin_id = $("#admin_id").val();

    $.ajax({
        type: "POST",
        url: '/_ajax/update_presence.php',
        data: { checked : checked, user_id : user_id, tr_id : tr_id, admin_id :  admin_id},
        success: function(data) {

        }
    });

});

$('.name_tag_checked').click(function() {
    //alert($(this).css('backgroundColor'));
    var user_id = $(this).attr('id');
    var x = $(this).css('backgroundColor');
    hexc(x);
    var checked = (color == '#e2edff');
    var tr_id = $("#tr_id").val();
    //ha kék, akkor zöldre változtatja, egyébként fordítva
    if (color == '#e2edff') {
        $(this).css('backgroundColor', '#1eea0b');
        var el = $('#headcount');
        var num = parseInt(el.text());
        el.text(num+1).append(" fő");

    }
    else {
        $(this).css('backgroundColor', '#e2edff');
        var el = $('#headcount');
        var num = parseInt(el.text());
        el.text(num-1).append(" fő");
    }
    //alert(checked);

    var admin_id = $("#admin_id").val();

    $.ajax({
        type: "POST",
        url: '/_ajax/update_presence.php',
        data: { checked : checked, user_id : user_id, tr_id : tr_id, admin_id :  admin_id},
        success: function(data) {

        }
    });

});


function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');
}


</script>
