import AdmVue from '@assets/js/adm-vue';
import axiosApi from "@assets/js/axios-api";
import param from "@assets/js/param";


const vm = new AdmVue({
  components: {
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    BaseInputFile: () => import(/* webpackChunkName: "BaseInputFile" */ '@elements/BaseInputFile/index.vue'),
    BaseTabs: () => import(/* webpackChunkName: "BaseTabs" */ '@components/BaseTabs/index.vue'),
    BaseTextarea: () => import(/* webpackChunkName: "BaseTextarea" */ '@elements/BaseTextarea/index.vue'),
    GroupInput: () => import(/* webpackChunkName: "BaseTabs" */ '@components/GroupInput/index.vue'),
    listPagination: () => import(/* webpackChunkName: "listPagination" */ '@components/ThePagination/index.vue'),
  },
  data() {
    return {
      list:{
        //campos que vao ser usados na tabela de medidas
      },
      filters:{
        dateStart : '',
        dateEnd : '',
        description : '',
        //Filtros Vindo do V-model
      },
      pagination : '',
      currentPage : 1,
      limit : 12,
      btnSubmitLoading: false,
      disableForm: false,
      initialDate: '' ,
      finalDate: ''
    };
  },
  created() {},
  beforeMount() {},
  mounted() {},
  watch: {},
  computed: {
    sendData() {
      return {
        ...this.list, ...this.filters, ...this.pagination
      };
    },
  },
  methods: {
    setErrors(errors) {
      this.$refs.validationObserver.setErrors(errors);
    },
    formError(data) {
      const { errors } = data;
      this.$refs.validationObserver.setErrors(errors);
      if (errors.warning) {
        this.alert = {
          type: 'warning',
          message: errors.warning,
        };
      }
    },
    filter(selectedPage = 1){
      this.btnSubmitLoading = true;
      this.disableForm = true;

      if(selectedPage){
        this.currentPage = selectedPage;
      }
      axiosApi({
        method: 'POST',
        url: this.actionFilter,
        data: param({filters: this.filters, page: selectedPage}),
        // params: { filters: this.filters, page: selectedPage },
      })
        .then(({data}) => {
          if(data.list){
            this.list = data.list;
            if(data.pagination){
              this.pagination = data.pagination;
            }
            if(data.limit){
              this.limit = parseInt(data.limit);
            }
          }
          this.btnSubmitLoading = false;
          this.disableForm = false;
        })
        .catch((err) => console.log(err, err.response))
    },
    disabledFinalDate(date) {
      const disabledFinalDate = new Date(date.getTime() + 0 * 24 * 3600 * 1000);
      return disabledFinalDate > new Date(new Date().setHours(0, 0, 0, 0));
    },
    formartDate(date) {
      const day = ('0' + date.getDate()).slice(-2);
      const month = ('0' + (date.getMonth() + 1)).slice(-2);
      const year = date.getFullYear();

      return day + '/' + month + '/' + year;
    },
    onInitialDate(value) {
      if (value.length >= 10) {
        const [day, month, year] = value.split('/');
        const date = new Date(year + '-' + month + '-' + day);
        const finalDate = new Date();
        if(date > finalDate) {
          notificationMsg(this.error_dataMax, 'warning');
          this.filters.dateStart = this.formartDate(finalDate);
        }
      }
    },
    onFinalDate(value) {
      if (value.length >= 10) {
        const [day, month, year] = value.split('/');
        const date = new Date(year + '-' + month + '-' + day);
        const finalDate = new Date();
        if(date > finalDate) {
          notificationMsg(this.error_dataMax, 'warning');
          this.filters.dateEnd = this.formartDate(finalDate);
        }
      }
    },
  },
});

AdmVue.init(vm);
