import documentReady from '@assets/js/documentReady';
import FixedContentOnScroll from '../../fixed-content';
import axiosApi from '@assets/js/axios-api';
import param from '@assets/js/param';

documentReady(async () => {
  new FixedContentOnScroll(document.querySelector('.js-fixed-on-top'), {
    discountTop: [document.querySelector('.js-header__main')],
  });

  const menuAccount = document.querySelector('.js-account');
  if (menuAccount) {
    menuAccount.onclick = function(event) {
      if (this.getAttribute('href') === window.location.href) {
        event.preventDefault();
        document.querySelector('.js-account-aside').classList.toggle('hidden-md-down');
        document.querySelector('.js-account-container').classList.toggle('hidden-md-down');
      } else {
        window.location = this.getAttribute('href');
      }
    };
  }

  let checkFeedback = null;
  let loadingFeedback = null;
  const containerFeedback = document.querySelector('.js-feedback-container');
  if(containerFeedback){
    checkFeedback = containerFeedback.querySelector('.js-feedback-entry');
    loadingFeedback = containerFeedback.querySelector('.js-feedback-loading');
  }

  if (checkFeedback) {
    checkFeedback.addEventListener('change', async () => {
      loadingFeedback.classList.remove('hidden');
      const feedback_status = checkFeedback.checked ? Type_Status.Tp_Habilitado : Type_Status.Tp_Desabilitado;

      try {
        checkFeedback.disabled = true;

        const { data } = await axiosApi.post(
          'index.php?route=feedback/feedback/authorization',
          param({
            feedback_status,
          })
        );

        if (data['warning']) {
          notificationMsg(data['warning'], 'warning');
        }

        if (data['success']) {
          notificationMsg(data['success'], 'success');
        }
      } catch (e) {
        console.log(e);
      } finally {
        loadingFeedback.classList.add('hidden');
        checkFeedback.disabled = false;
      }
    });
  }
});
