/* 
 * Author       : Jai K
 * Purpose      : Main script for "Kiosk"
 * Created On   : 2016-09-12 19:00
 */


$(document).on('ready', function(){
    
    
    $('.opening_amt_dialog #open-submit-btn').click(function(){
        var user_id = $('.dialog .user_role_id').attr('data-id');
        var user_role_id = $('.dialog .user_role_id').val();
        var amount = $('.opening_amt').val();
        if( amount != '') {
            $.ajax({
                async: false,
                url: baseURL + 'billing_common/add_open_balance',
                type: 'POST',
                data: { user_id: user_id, user_role_id: user_role_id, amount: amount  },
                dataType: "json"
            })
            .done(function(data) {
                if(data > 0)
                    window.location.href = $('.opening_amt_dialog #open-submit-btn').attr('data-url');
                
            })
            .fail(function(jqXHR, status_code, error_thrown) {
                console.log(jqXHR.responseText);
            });
        } else {
            msgBox('Please enter Opening Balance Amount!');
        }
    })
    
})



function open_balance_dialog(_this) {
    var user_role_id = $('.user_role_id').val();
    var url = $(_this).attr('data-url');
    $.ajax({
        async: false,
        url: baseURL + 'billing_common/open_balance_exists',
        type: 'POST',
        dataType: "json"
    })
    .done(function(data) {
        if(data == 0) {
            //if(user_role_id != 1) {
                dialog($('.opening_amt_dialog'));
                $(_this).parents().find('#ksk-menu-dialog').hide();
            /* } else if(user_role_id == 1) {
                dialog($('.opening_amt_dialog'));
                $('.opening_amt_dialog .options').html('<b style="color:#c1ddde;">No Opening Balance in Counter. Please contact the administrator!</b><span style="display: none;"><input type="submit" class="opening_logout" name="opening_logout" /></span>');
                $('.opening_amt_dialog form').attr('action',baseURL + 'kiosk/logout/');
                
                window.setTimeout(function(){
                    $('.opening_amt_dialog .opening_logout').trigger('click');
                }, 8000);   
                return false;
            } */
        }
        else {
            window.location.href = url;
        }
    })
    .fail(function(jqXHR, status_code, error_thrown) {
        console.log(jqXHR.responseText);
    });
    $('.opening_amt_dialog #open-submit-btn').attr('data-url',url);
    
}

