/******/ (() => { // webpackBootstrap
/*!***************************************!*\
  !*** ./resources/js/work_accident.js ***!
  \***************************************/
// WorkAccident
window.submitWorkAccidentHandler = function submitWorkAccidentHandler(el) {
  var business_unit_el = document.getElementById('business_unit');
  document.getElementsByName("business_unit_id")[0].value = business_unit_el.options[business_unit_el.selectedIndex].dataset.business_unit_id;
  document.getElementsByName("business_unit_type")[0].value = business_unit_el.options[business_unit_el.selectedIndex].dataset.business_unit_type;
};
$(document).on('submit', '#edit_work_accident', function (e) {
  e.preventDefault();
  e.stopPropagation();
  var formData = new FormData($(this)[0]);
  $.ajax({
    type: 'POST',
    url: $(this).attr('action'),
    data: formData,
    async: false,
    dataType: 'json',
    cache: false,
    contentType: false,
    processData: false,
    success: function success(data) {
      // Hide validation errors
      $('#edit_work_accident_modal_errors').addClass('d-none');

      // Close modal
      $('#edit_work_accident_modal').modal('hide');

      // Show feedback
      $.notify({
        message: 'Bedrijfsongeval opgeslagen'
      });

      // Reload datatable
      if ("work_accidents_table" in window.LaravelDataTables) {
        window.LaravelDataTables["work_accidents_table"].ajax.reload(null, false);
      }
    },
    error: function error(jqXHR) {
      // Show validation errors
      $('#edit_work_accident_modal_errors ul li').remove();
      $.each(jqXHR.responseJSON.errors, function (key, errors) {
        $.each(errors, function (key, error) {
          $('#edit_work_accident_modal_errors ul').append('<li>' + error + '</li>');
        });
      });
      $('#edit_work_accident_modal_errors').removeClass('d-none');
    }
  });
});
$(document).on('click', '.work_accident_destroy', function (e) {
  var _this = this;
  e.preventDefault();
  e.stopPropagation();

  // Ask for confirmation before destroying
  Swal.fire({
    title: 'Weet u zeker dat u dit bedrijfsongeval wilt verwijderen?',
    // @todo @lang
    text: "Dit kan niet ongedaan worden!",
    // @todo @lang
    icon: 'warning',
    showCancelButton: true,
    cancelButtonColor: '#d33',
    cancelButtonText: 'Annuleren',
    // @todo @lang
    confirmButtonColor: '#3085d6',
    confirmButtonText: 'Verwijderen' // @todo @lang
  }).then(function (result) {
    if (result.isConfirmed) {
      // Send ajax request for destroying the object
      $.ajax({
        type: 'POST',
        url: $(_this).data('route'),
        data: {
          "_method": "DELETE",
          "_token": $(_this).parents('form').find('input[name="_token"]').val()
        },
        dataType: 'json',
        success: function success(data) {
          // Show feedback
          $.notify({
            message: 'Bedrijfsongeval verwijderd' // @todo @lang
          });

          // Reload datatable
          if ("work_accidents_table" in window.LaravelDataTables) {
            window.LaravelDataTables["work_accidents_table"].ajax.reload(null, false);
          }
        },
        error: function error(jqXHR) {
          // Show feedback
          $.notify({
            message: 'Er is iets misgegaan met het verwijderen van het bedrijfsongeval. Probeer het nogmaals.' // @todo @lang
          }, {
            type: 'danger'
          });
        }
      });
    }
  });
});

/**
 * When selecting an item in the link_employee select add it the
 * the list of selected employees and disable the corresponding option
 */
$(document).on('change', 'select[name="link_employee"]', function (e) {
  var employee_id = $(this).find('option:selected').val();
  if (!employee_id.length) {
    // No employee selected, skip
    return;
  }
  var name = $(this).find('option:selected').text();
  $(this).find('option:selected').prop('disabled', true);
  $(this).val(null);
  initSelect2($(this).parents('.modal-content'));
  $clone = $('#work_accident_employee_template>div').clone();
  $clone.find('.name').text(name);
  $clone.find('input[name="linked_employee[]"]').val(employee_id);
  $('#linked_employees').append($clone);
});

/**
 * When removing an employee from the list of linked employees remove the
 * row and enable the corresponding option in the select field
 */
$(document).on('click', '.unlink_work_accident_employee', function (e) {
  var $item = $(this).parents('.linked_employee_element');
  var employee_id = $item.find('input[name="linked_employee[]"]').val();
  $item.remove();
  $('select[name="link_employee"]').find('option[value="' + employee_id + '"]').prop('disabled', false);
  initSelect2($(this).parents('.modal-content'));
});

/**
 * When selecting an item in the link_sick_leave select add it the
 * the list of selected sick_leaves and disable the corresponding option
 */
$(document).on('change', 'select[name="link_sick_leave"]', function (e) {
  var sick_leave_id = $(this).find('option:selected').val();
  if (!sick_leave_id.length) {
    // No sick_leave selected, skip
    return;
  }
  var name = $(this).find('option:selected').text();
  $(this).find('option:selected').prop('disabled', true);
  $(this).val(null);
  initSelect2($(this).parents('.modal-content'));
  $clone = $('#work_accident_sick_leave_template>div').clone();
  $clone.find('.name').text(name);
  $clone.find('input[name="linked_sick_leave[]"]').val(sick_leave_id);
  $('#linked_sick_leaves').append($clone);
});

/**
 * When removing an sick_leave from the list of linked sick_leaves remove the
 * row and enable the corresponding option in the select field
 */
$(document).on('click', '.unlink_work_accident_sick_leave', function (e) {
  var $item = $(this).parents('.linked_sick_leave_element');
  var sick_leave_id = $item.find('input[name="linked_sick_leave[]"]').val();
  $item.remove();
  $('select[name="link_sick_leave"]').find('option[value="' + sick_leave_id + '"]').prop('disabled', false);
  initSelect2($(this).parents('.modal-content'));
});
/******/ })()
;
//# sourceMappingURL=work_accident.js.map