$( document ).ready(function() {
    var token = $('body').data('token');

    $('.js-task-save').on('click', function(){

      var data = {},
          $this          = $('.js-task-save'),
          task_id        = $('.js-table').find('.js-identify-id'),          
          status         = $('.js-table').find('.js-identify-status'),
          time_execution = $('.js-table').find('.js-identify-time');

      data['task_id'] = $.map(task_id, function(element){
        return $(element).data('task-id');
      });

      data['type'] = $.map(task_id, function(element){
        return $(element).data('task-type');
      });      

      data['status'] = $.map(status, function(element){
        var tagName = $(element).prop('tagName');
        if(tagName == 'SELECT'){
          return parseInt($(element).val());
        }else{
          return $(element).data('task-status');
        }        
      });

      data['time_execution'] = $.map(time_execution, function(element){
        return {control_id: $(element).data('task-control-id'), value: $(element).val()} ;
      });      


      $.ajax({
        url: 'index.php?route=module/task/save&token=' + token,
        type:'POST',
        data: data,
        dataType: 'json',
        beforeSend: function() {
          $('.js-task-save').addClass('btn-loading');
        },
        complete: function() {
          $('.js-task-save').removeClass('btn-loading');
        },
        success: function(json) {
          if(json['error']){
            $('#notification').append('<div class="warning full-width>'+json['error']+'</div>');
          }          
          if(json['redirect']){
            location.href = json['redirect'];
          }
        }
      });

    });

});