import Vue from 'vue';
import initVue from '@assets/js/initVue.js';
import AdmVue from '@assets/js/adm-vue';
import copyContent from '@assets/js/copyContent';

const vm = new AdmVue({
  components: {
    BaseManual: () => import(/* webpackChunkName: "BaseManual" */ '@elements/BaseManual/index.vue'),
  },
  data() {
    return {
      configurations: {},   
    };
  },
  methods: {
    setData(configurations){
      this.configurations = configurations
    }
  }
});

$(document).ready(function(){
  if ($(".clearsale_tipo_integracao").val() == Type_ClearSaleIntegracao.Tp_Start){
    $(".clear_sale_total_config").hide();
  } else {
    $(".clear_sale_start_config").hide();
  }

  $(".clearsale_tipo_integracao").change(function(){
    if ($(this).val() == Type_ClearSaleIntegracao.Tp_Start){
      $(".clear_sale_total_config").hide();
      $(".clear_sale_start_config").show();
    } else {
      $(".clear_sale_total_config").show();
      $(".clear_sale_start_config").hide();
    }
  });

  document.getElementById('btnCallback').onclick = copyUrlCallback;
  document.getElementById('btn-validate').onclick = validateCredential;

  function copyUrlCallback(e) {
    copyContent($("#urlCallback")[0].innerText);
  }

  function validateCredential(e) {
    e.preventDefault();
    let urlValidate = $('#btn-validate').data('url');
    let data = {
      'clearsale_login': $('[name="clearsale_login"]').val(),
      'clearsale_senha': $('[name="clearsale_senha"]').val()
  };

    $.ajax({
      url: urlValidate,
      type: 'GET',
      data,
      beforeSend: function() {
        $('#btn-validate').addClass('btn-loading');
        $('#btn-validate').attr('disabled', true);
      },
      complete: function() {
        $('#btn-validate').removeClass('btn-loading');
        $('#btn-validate').attr('disabled', false);
      },
      success: function({type, message}) {
        $('.warning, .success').remove();

        if (type){
          if (type == 'error') {
            document.querySelector('.notification-container').innerHTML = '<div class="warning full-width">' + message + '</div>';
          } else if (type == 'success') {
            document.querySelector('.notification-container').innerHTML ='<div class="success full-width">' + message + '</div>';
          }
          $('html, body').animate({scrollTop: '0px'}, 300);
        }

      },
      error: function(err) {
        console.log(err);
      },
    });
  }
});

initVue(vm);
