var elementUrl = 'index.php?option=com_formea&task=element.edit&layout=modal&tmpl=component';
document.addEventListener('DOMContentLoaded', function () {

    window.addEventListener('FormeaEditElement', (evt) => {
        console.log('evt', evt);
        if (evt.detail !== undefined) {
            var elId = 0;
            var action = 'add';
            if (!evt.detail.isNew) {
                elId = evt.detail.element_id;
                action = 'edit';
            }
            var mdl = document.getElementById('modal-element');
            var iframeString = '<iframe class="iframe" name="test" title="test" height="100%" width="100%"';
            iframeString += ' src="index.php?option=com_formea&amp;task=element.edit&amp;type=' + evt.detail.type + '&amp;id=' + elId + '&amp;layout=modal&amp;tmpl=component"';
            iframeString += '></iframe>';

            mdl.setAttribute('data-url', elementUrl + '&id=' + elId + '&type=' + evt.detail.type);
            mdl.setAttribute('data-iframe', iframeString);

            var mdlTitle = mdl.querySelector('.modal-title');
            mdlTitle.innerHTML = evt.detail.type;
            const myModal = new bootstrap.Modal(document.getElementById('modal-element'), {});
            myModal.show();

            var elementIdInput = document.getElementById('edit_element_id');
            elementIdInput.setAttribute('data-detail', JSON.stringify(evt.detail));

            var cancel_edit_element = document.getElementById('cancel_edit_element');
            cancel_edit_element.setAttribute('data-action', action);

            var save_edit_element = document.getElementById('save_edit_element');
            save_edit_element.setAttribute('data-action', action);
        }

    });

    window.addEventListener('FormeaGroupTypeChanged', (evt) => {
        if (evt.detail.groupType !== undefined && evt.detail.groupType !== null) {
            const groupTypeEl = document.getElementById('jform_group_type');
            if (groupTypeEl !== undefined) {
                groupTypeEl.value = evt.detail.groupType;
            }
        }
    });

    /* var labelSettingEls  =document.getElementById('formea-labelSetting-jform');
     const inputElements = labelSettingEls.querySelectorAll("input");

   // Add an event listener to each input element
     inputElements.forEach((input) => {
       input.addEventListener("input", (event) => {
         var test = getJoomlaLabelSetting(labelSettingEls);
         console.log('test',test);
         console.log('inputElements',inputElements);
         /!* var panel = document.querySelector('grouped-elements');
         if (panel !== undefined) {
           //panel.externalElementUpdate(detail);
         }*!/
         // This function will be called whenever an input element changes
         console.log(`Input with ID ${input.id} changed. New value: ${input.value}`);
         // You can perform any actions you want here
       });
     });*/

});

/*var getJoomlaLabelSetting = function(labelSettingEls){
  const formData = new FormData(labelSettingEls);
  const inputElements = labelSettingEls.querySelectorAll("input");
  const serializedData = [];

  inputElements.forEach((input) => {

    //serializedData.push({ name: key, value });
  });

  return serializedData;
}*/

var formeaProcessModal = function (el) {
    var props = {
        'fieldPrefix': '',
        'action': '',
        'itemType': '',
        'task': '',
        'formId': '',
        'idFieldId': '',
        'titleFieldId': '',
    };
    Object.keys(props).forEach(function (key) {
        var t = el.getAttribute('data-' + key.toLowerCase());
        if (t !== undefined && t !== null) {
            props[key] = t;
        }
    });
    //(element, fieldPrefix, action, itemType, task, formId, idFieldId, titleFieldId)
    window.processModalEdit(el, props.fieldPrefix, props.action, props.itemType, props.task, props.formId, props.idFieldId, props.titleFieldId);
    setTimeout(function () {
        FormeaElementIdChange();
    }, 800)
    return false;
};

var FormeaElementIdChange = function () {
    var el = document.getElementById('edit_element_id');
    var element_id = el.value;
    if (element_id !== undefined && element_id !== null) {
        var detail = el.getAttribute('data-detail');
        if (detail !== undefined && detail !== null) {
            detail = JSON.parse(detail);
            detail['element_id'] = parseInt(element_id);
            // console.log('FormeaElementIdChange',detail);
            var panel = document.querySelector('grouped-elements');
            if (panel !== undefined) {
                panel.externalElementUpdate(detail);
            }
        }
    } else {
        setTimeout(function () {
            FormeaElementIdChange();
        }, 500);
    }

}
