<div class="form_wrapper">
    <form method="post" id="training-create">
        <input type="hidden" name="action" id="action" value="training_data_create">
        <input type="hidden" name="jumpTo" id="jumpTo" value="0">

        <div>
            <label class="desc" for="training_templates">Sablon:</label>
            <div>
                <select name="training_templates" id="training_templates">
                    <option value="null"> - </option>
                    {foreach $training_templates as $template}
                        <option value="{$template.tt_id}">
                            {$template.tt_name}
                        </option>
                    {/foreach}
                </select>
            </div>
        </div>

        <br><br>

        <div>
            <label class="desc" id="title1" for="tr_date">Dátum:</label>
            <div><input type="text" name="tr_date" id="tr_date" title="ÉÉÉÉ-HH-NN ÓÓ:PP" placeholder="ÉÉÉÉ-HH-NN ÓÓ:PP" required></div>
        </div>


        <div>
            <label class="desc" id="title2" for="tr_training_type_trt_id">Típus:</label>
            <div>
                <select name="tr_training_type_trt_id" id="tr_training_type_trt_id">
                    <option value="null"> - </option>
                    {foreach $training_type_assoc_array as $training_type_array}
                        <option value="{$training_type_array.trt_id}">
                            {$training_type_array.trt_name}
                        </option>
                    {/foreach}
                </select>
            </div>
        </div>

        <div>
            <label class="desc" id="title1" for="tr_duration">Időtartam (perc):</label>
            <div><input type="text" name="tr_duration" id="tr_duration" required></div>
        </div>


        <div>
            <label class="desc" id="title1" for="every_week">Minden héten ebben az időpontban: (az adott hónapban)</label>
            <div><input type="checkbox" name="every_week" id="every_week" value="1"></div>
        </div>

        <div>
            <label class="desc" for="tr_note">Megjegyzés:</label>
            <div>
                <textarea rows="4" name="tr_note" id="tr_note"></textarea>
            </div>
        </div>

        <div>
            <label class="desc" id="title1" for="coaches">Edző(k):</label>
            <table>
                <tr>
                    <td class="bold">Név</td>
                    <td class="bold center">E</td>
                    <td class="bold center">SE</td>
                </tr>
                {foreach $coach_array as $coach}
                <tr>
                    <td class="coach">{$coach->get_ua_name()}</td>
                    <td><input type="checkbox" name="coaches[]" value="{$coach->get_ua_id()}" class="coach_type"></td>
                    <td><input type="checkbox" name="helpers[]" value="{$coach->get_ua_id()}" class="coach_type"></td>
                </tr>
                {/foreach}
            </table>
        </div>

        <div>
            <div>
                <input class="button black save" type="submit" value="Létrehozás">
                <input class="button black jump-to" type="button" value="Létrehozás és ugrás a jelenléthez">
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    $('.jump-to').click(function () {
        $('#jumpTo').val(1);

        if ($('#tr_date').val().length && $('#tr_training_type_trt_id').val() != 'null' && $('#tr_duration').val().length) {
            //$('#training-create').submit();
            $('.save').trigger('click');
        }
    });

    $('#training_templates').on('change', function(e) {
        let selectedTemplate = $('#training_templates option:selected').val();
        console.log(selectedTemplate);
        $.ajax({
            url: '/_ajax/get_training_template.php',
            //method: 'GET',
            data: {
                'template_id' : selectedTemplate
            },
            success: function(data, status, jqXHR) {
                var pdata = JSON.parse(data);

                if (null == pdata) {

                    $('#tr_date').val('');
                    $('#tr_training_type_trt_id').val('null');
                    $('#tr_duration').val('');

                    return;
                }

                console.log(pdata);

                var d = new Date();

                var month = d.getMonth()+1;
                var day = d.getDate();

                var output = d.getFullYear() + '-' +
                    ((''+month).length<2 ? '0' : '') + month + '-' +
                    ((''+day).length<2 ? '0' : '') + day;


                if (null != pdata['tt_time']) $('#tr_date').val(output + ' ' + pdata['tt_time']);
                if (null != pdata['tt_training_type']) $('#tr_training_type_trt_id').val(pdata['tt_training_type']);
                if (null != pdata['tt_duration']) $('#tr_duration').val(pdata['tt_duration']);
            }
        });
    });

</script>
