<div class="form_wrapper">
    <form method="post">
        <input type="hidden" name="action" value="money_deposit_create">

        <div>
            <label class="desc" id="title1" for="mod_user_kid_uk_id">Befizető:</label>
            <div>
                <select name="mod_user_kid_uk_id" id="mod_user_kid_uk_id">
                    {foreach $user_kids as $user_kid}
                        <option value="{$user_kid->get_uk_id()}"{if isset($user_kid_id) && $user_kid_id == $user_kid->get_uk_id()} selected{/if}>{$user_kid->get_uk_name()}</option>>
                    {/foreach}
                </select>
            </div>
        </div>

        {* <div>
            <label class="desc" for="mod_payment_type">Fizetés típusa:</label>
            <div>
                <select name="mod_payment_type" id="mod_payment_type">
                    {foreach $pt_assoc_array as $pt_array}
                        <option value="{$pt_array.pt_id}">
                            {$pt_array.pt_name}
                        </option>
                    {/foreach}
                </select>
            </div>
        </div> *}

        <div>
            <label class="desc" id="title1" for="mod_date">Dátum:</label>
            <div><input type="text" name="mod_date" id="mod_date" value="{$today}" required></div>
        </div>

        <div>
            <label for="mod_lease_l_id">Bérlettípus:</label>
            <div>
                <select name="mod_lease_l_id" id="mod_lease_l_id">
                    {* <option value="null">Nincs beállítva</option> *}
                    {foreach $lease_array as $lease}
                        <option value="{$lease->get_l_id()}">
                            {$lease->get_l_name()}
                        </option>
                    {/foreach}
                </select>
            </div>
        </div>

        <div>
            <label class="desc" id="title1" for="mod_sum">Összeg:</label>
            <div><input type="text" name="mod_sum" id="mod_sum" required></div>
        </div>

        <div>
            <label for="mod_sum">Lejár:</label>
            <input type="hidden" name="mod_expire_date" id="mod_expire_date">
            <div id="expire_date" style="padding-top: 6px;">
                Következő hónapban (alapértelmezett beállítás)
            </div>
        </div>

        <div>
            <label for="mod_sets_expire_date">Frissítse az egyenleg lejárati dátumát:</label>
            <div><input type="checkbox" name="mod_sets_expire_date" id="mod_sets_expire_date" value="1" checked="checked"></div>
        </div>

        <div>
            <div>
                <input class="button black" type="submit" value="Mentés">
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    $('#mod_lease_l_id').on('change', function() {
        var leaseId = $('#mod_lease_l_id option:selected').val();
        $.ajax({
            url: '/_ajax/get_lease_sum.php',
            data: {
                'lease_id' : leaseId
            },
            success: function(data, status, jqXHR) {
                var pdata = JSON.parse(data);

                if (null == pdata) {
                    $('#expire_date').html('Következő hónapban (alapértelmezett beállítás)');
                    $('#mod_sum').val('');
                    return;
                }

                if (null == pdata['l_sum']) {
                    $('#mod_sum').val('');
                }
                else {
                    $('#mod_sum').val(pdata['l_sum']);
                }

                if (pdata['l_expire_type'] == '1') {
                    //var today = new Date();
                    var today = new Date($('#mod_date').val() + " 00:00:00");
                    //var nextMonthBeginning = new Date(today.getFullYear(), today.getMonth() + 1, 1);
                    var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
                    //console.log(lastDayOfMonth, today, lastDayOfMonth.getTime() === today.getTime());
                    $('#expire_date').html(lastDayOfMonth.getFullYear() + '-' + ('0' + (lastDayOfMonth.getMonth()+1)).slice(-2) + '-' + ('0' + lastDayOfMonth.getDate()).slice(-2));
                    $('#mod_expire_date').val(lastDayOfMonth.getFullYear() + '-' + ('0' + (lastDayOfMonth.getMonth()+1)).slice(-2) + '-' + ('0' + lastDayOfMonth.getDate()).slice(-2));

                }
                else if (pdata['l_expire_type'] == '2') {
                    //var today = new Date();
                    var today = new Date($('#mod_date').val());
                    var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+2, 0);
                    $('#expire_date').html(lastDayOfMonth.getFullYear() + '-' + ('0' + (lastDayOfMonth.getMonth()+1)).slice(-2) + '-' + ('0' + lastDayOfMonth.getDate()).slice(-2));
                    $('#mod_expire_date').val(lastDayOfMonth.getFullYear() + '-' + ('0' + (lastDayOfMonth.getMonth()+1)).slice(-2) + '-' + ('0' + lastDayOfMonth.getDate()).slice(-2));
                }
                else if (pdata['l_expire_type'] == '3') {
                    $('#expire_date').html(pdata['l_expire_date']);
                    $('#mod_expire_date').val(pdata['l_expire_date']);
                }
            }
        });
    });

    $(document).ready(function() {
        $('#mod_lease_l_id').trigger('change');
    });

    $('#mod_date').on('change', function() {
        $('#mod_lease_l_id').trigger('change');
    });
</script>
