import ModalMidias from "../classes/ModalMidias.js";

jQuery(function ($) {

  /* Adicionar arquivo */
  $(document).on('click', '.o2-file-field-add', function () {
    const field = $(this).closest('.o2-file-field');

    const fileModal = new ModalMidias((attachment) => {
      field.children('input').val(attachment.id);
      field.find('.o2-file-field-details-title').html(attachment.title || "Arquivo");
      field.find('.o2-file-field-details-filename').html(`<a target="_blank" href="${attachment.editLink}">${attachment.filename}</a>`);
      field.find('.o2-file-field-details-filesize').html(attachment.filesizeHumanReadable);
      field.find('.o2-file-field-details-filetype').html('.' + attachment.subtype);
      field.children('.o2-file-field-unselected').addClass('d-none');
      field.children('.o2-file-field-selected').removeClass('d-none');
    });

    fileModal.openModal();
  });

  /* Editar arquivo */
  $(document).on('click', '.o2-file-field-edit', function () {
    $(this).closest('.o2-file-field').find('.o2-file-field-add').trigger('click');
  });

  /* Remover arquivo */
  $(document).on('click', '.o2-file-field-remove', function () {
    const field = $(this).closest('.o2-file-field');
    field.children('input').val('');
    field.children('.o2-file-field-unselected').removeClass('d-none');
    field.children('.o2-file-field-selected').addClass('d-none');
  });
});