function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31 
    && (charCode < 48 || charCode > 57))
    return false;
    return true;
}
$(function(){
    $('#task_add').submit(function(){
        var url = '/ajax.php?action=task&type='+$("#type").val();//$(this).attr('action');
        var data = $(this).serialize()+'&ajax=1';
        $.post(url, data, function(result){
            if(result['id'] != ''){
                $('#'+result['id']).addClass('is-invalid');
            }
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                if(result['redirect'] != ''){
                    setTimeout(function(){
                        window.location.replace(result['redirect']);
                    }, 1000);
                }
            } 
        }, 'json');
        return false;
    });
    $('#task_edit').submit(function(){
        var url = '/ajax.php?action=task&type='+$("#type").val();//$(this).attr('action');
        var data = $(this).serialize()+'&ajax=1';
        $.post(url, data, function(result){
            if(result['id'] != ''){
                $('#'+result['id']).addClass('is-invalid');
            }
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                if(result['redirect'] != ''){
                    setTimeout(function(){
                        window.location.replace(result['redirect']);
                    }, 1000);
                }
            } 
        }, 'json');
        return false;
    });
    $('form[name="balance_add"]').submit(function(){
        var values = {};
        $.each($(this).serializeArray(), function(i, field) {
            values[field.name] = field.value;
        });
        var number_count = $('#number_count'+values['task_id']).text();
        var url = '/ajax.php?action=task&type='+values['type'];//$(this).attr('action');
        var data = $(this).serialize()+'&number_count='+number_count+'&ajax=1';
        $.post(url, data, function(result){
            if(result['id'] != ''){
                $('#'+result['id']).addClass('is-invalid');
            }
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                $('#number_count'+values['task_id']).html(result['number_count']);
                if(result['redirect'] != ''){
                    setTimeout(function(){
                        window.location.replace(result['redirect']);
                    }, 1000);
                }
            } 
        }, 'json');
        return false;
    });
    $('#send_check').submit(function(){
        var url = '/ajax.php?action=task&type='+$("#type").val();//$(this).attr('action');
        var data = $(this).serialize()+'&ajax=1';
        console.log(url);
        $.post(url, data, function(result){
            if(result['id'] != ''){
                $('#'+result['id']).addClass('is-invalid');
            }
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                if(result['redirect'] != ''){
                    setTimeout(function(){
                        window.location.replace(result['redirect']);
                    }, 1000);
                }
            } 
        }, 'json');
        return false;
    });
    $(document).on('click', '.delete_task',function() {
        delItem = $(this);
        var task_id = $(this).attr('task_id');
        swal({
            title: "Вы уверены?",
            text: "Это действие нельзя будет отменить!",
            icon: "warning",
            buttons: ["Нет, я передумал!", "Да, удалить!"],
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
                $.post('/ajax.php?action=task&type=delete', {'task_id': task_id}, function(result) {
                    swal(result['title'], result['error'], result['status']);
                    if(result['status'] == 'success'){
                        $('#task_'+task_id).remove();
                    }
                }, 'json');
            }
          });
        return false;
    });
    $(document).on('click', '.status_task',function() {
        var button = $(this);
        var task_id = button.attr('task_id');
        var status = button.attr('action');
        $.post('/ajax.php?action=task&type=status', {'task_id': task_id,'status':status}, function(result) {
            if(result['status'] == 'success'){
                if(status === 'active'){
                    button.attr('action','deactive');
                    button.html('<i class="fa fa-pause"></i>');
                    button.removeClass('btn-success');
                    button.addClass('btn-warning');
                }else if(status === 'deactive'){
                    button.attr('action','active');
                    button.html('<i class="fa fa-play"></i>');
                    button.removeClass('btn-warning');
                    button.addClass('btn-success');
                }  
            }
        }, 'json');
        return false;
    });
    $(document).on('click', '.approve_task',function() {
        delItem = $(this);
        var task_id = $(this).attr('task_id');
        $.post('/ajax.php?action=task&type=approve', {'task_id': task_id}, function(result) {
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                $('#task_'+task_id).remove();
            }
        }, 'json');
        return false;
    });
    $(document).on('click', '.run_task',function() {
        var task_id = $(this).attr('task_id');
        $.post('/ajax.php?action=task&type=run', {'task_id': task_id}, function(result) {
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                setTimeout(function(){
                    window.location.replace(result['redirect']);
                }, 1000);
            }
        }, 'json');
        return false;
    });
    $(document).on('click', '.do_progress',function() {
        var task_id = $(this).attr('task_id');
        $('#do_progress_'+task_id).submit();
    });
    $(document).on('click', '.details',function() {
        var task_id = $(this).attr('task_id');
        var display = '';
        display = $('.details-'+task_id).css('display');
        if(display === 'none'){
            display = $('.details-'+task_id).css('display','block');
        }else if(display === 'block'){
            display = $('.details-'+task_id).css('display','none');
        }
    });
    $(document).on('click', '.check-progress',function() {
        var progress_id = $(this).attr('progress_id');
        var action = $(this).attr('action');
        var display = '';
        var remake = '';
        if(action === 'remake'){
            display = $('#form-remake-'+progress_id).css('display');
            if(display === 'none'){
                $('#form-remake-'+progress_id).css('display','block');
                $('#btn-remake-'+progress_id).html('Отправить');
                return false;
            }else if(display === 'block'){
                remake = $('#remake').val();
                if(remake === ''){
                    swal('Ошибка', 'Заполните поле "Замечание"', 'error');
                    return false;
                }
            }
        }
        $.post('/ajax.php?action=task&type=check', {'progress_id': progress_id,'action':action,'remake':remake}, function(result) {
            swal(result['title'], result['error'], result['status']);
            if(result['status'] == 'success'){
                $('#progress_'+progress_id).remove();
            }
        }, 'json');
        return false;
    });
    
});


