import maskOptions from '@assets/js/masks.js';
import IMask from 'imask';

$(document).ready(function() {
  var
    token = $('body').data('token'),
    info = $('[data-info]').data('info');

  $('.js-filter').on('submit', function(event) {
    event.preventDefault();
    filter();
  });

  var data_added_url = document.getElementById('order_list_data_added');
  if (data_added_url){
    data_added_url.onchange = function() {
      window.location.href = this.value;
    }
  }

  IMask(document.querySelector('[name=filter_cpf_cnpj]'), maskOptions.cpfCnpj);

  function filter() {
    url = 'index.php?route=sale/customer&token=' + token;

    var filter_codl = $('input[name=\'filter_codl\']').attr('value');

    if ((typeof filter_codl != 'undefined') && filter_codl) {
      url += '&filter_codl=' + encodeURIComponent(filter_codl);
    }

    var filter_codf = $('input[name=\'filter_codf\']').attr('value');

    if ((typeof filter_codf != 'undefined') && filter_codf) {
      url += '&filter_codf=' + encodeURIComponent(filter_codf);
    }

    var filter_name = $('input[name=\'filter_name\']').attr('value');

    if ((typeof filter_name != 'undefined') && filter_name) {
      url += '&filter_name=' + encodeURIComponent(filter_name);
    }

    var filter_email = $('input[name=\'filter_email\']').attr('value');

    if ((typeof filter_email != 'undefined') && filter_email) {
      url += '&filter_email=' + encodeURIComponent(filter_email);
    }

    var filter_cpf_cnpj = $('input[name=\'filter_cpf_cnpj\']').attr('value');

    if ((typeof filter_cpf_cnpj != 'undefined') && filter_cpf_cnpj) {
      filter_cpf_cnpj = filter_cpf_cnpj.replace(/[.\-/]/g,'');
      url += '&filter_cpf_cnpj=' + encodeURIComponent(filter_cpf_cnpj);
    }

    var filter_pessoa = $('select[name=\'filter_pessoa\']').attr('value');

    if ((typeof filter_pessoa != 'undefined') && filter_pessoa != '*') {
      url += '&filter_pessoa=' + encodeURIComponent(filter_pessoa);
    }

    var filter_uf = $('select[name=\'filter_uf\']').attr('value');

    if ((typeof filter_uf != 'undefined') && filter_uf != '*') {
      url += '&filter_uf=' + encodeURIComponent(filter_uf);
    }

    var filter_customer_group_id = $('select[name=\'filter_customer_group_id\']').attr('value');

    if ((typeof filter_customer_group_id != 'undefined') && filter_customer_group_id != '*') {
      url += '&filter_customer_group_id=' + encodeURIComponent(filter_customer_group_id);
    }

    var filter_status = $('input[name=\'filter_status\']:checked').attr('value');

    if ((typeof filter_status != 'undefined') && filter_status != '*') {
      url += '&filter_status=' + encodeURIComponent(filter_status);
    }

    var filter_email_validate = $('input[name=\'filter_email_validate\']:checked').attr('value');

    if ((typeof filter_email_validate != 'undefined') && filter_email_validate != '*') {
      url += '&filter_email_validate=' + encodeURIComponent(filter_email_validate);
    }

    var filter_approved = $('input[name=\'filter_approved\']:checked').attr('value');

    if ((typeof filter_approved != 'undefined') && filter_approved != '*') {
      url += '&filter_approved=' + encodeURIComponent(filter_approved);
    }

    var filter_date_added = $('input[name=\'filter_date_added\']').attr('value');
    if ((typeof filter_date_added != 'undefined') && filter_date_added) {
      url += '&filter_date_added=' + encodeURIComponent(filter_date_added);
    }

    var filter_origem_cadastro = $('select[name=\'filter_origem_cadastro\']').attr('value');
    if ((typeof filter_origem_cadastro != 'undefined') && filter_origem_cadastro != '*') {
      url += '&filter_origem_cadastro=' + encodeURIComponent(filter_origem_cadastro);
    }

    var filter_integracao = $('select[name=\'filter_integracao\']').attr('value');
    if ((typeof filter_integracao != 'undefined') && filter_integracao != '*') {
      url += '&filter_integracao=' + encodeURIComponent(filter_integracao);
    }

    var filter_marketplace = $('select[name=\'filter_marketplace\']').attr('value');
    if ((typeof filter_marketplace != 'undefined') && filter_marketplace != '*') {
      url += '&filter_marketplace=' + encodeURIComponent(filter_marketplace);
    }

    var filter_limiter_lock = $('input[name=\'filter_limiter_lock\']').attr('checked');
    if ((typeof filter_limiter_lock != 'undefined') && filter_limiter_lock) {
      url += '&filter_limiter_lock=' + encodeURIComponent(filter_limiter_lock);
    }

    var filter_shopping_cart = $('input[name=\'filter_shopping_cart\']').attr('checked');
    if ((typeof filter_shopping_cart != 'undefined') && filter_shopping_cart) {
      url += '&filter_shopping_cart=' + encodeURIComponent(filter_shopping_cart);
    } 

    location = url;
  }


  var token = document.body.getAttribute('data-token'),
      btnLogin = document.querySelectorAll('.js-costumer-login');

  for (let i = 0; i < btnLogin.length; i++) {
    btnLogin[i].addEventListener('click', openAcount);
  }

  function openAcount(e) {
    e.preventDefault();
    var costumerLogin = this.getAttribute('data-customer-id');

    window.open('index.php?route=sale/customer/login&token='+ token +'&customer_id='+ costumerLogin);
  }

  $(".hasDatePicker").myDatePicker({
    maxDate:'today'
  });

});
