import AdmVue from '@assets/js/adm-vue';
import initVue from '@assets/js/initVue';
import axios from "axios";
import formMixin from "@mixins/formMixin";

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'),
    AsideContent: () => import(/* webpackChunkName: "AsideContent" */ '@components/AsideContent/index.vue'),
    BaseTextarea: () => import(/* webpackChunkName: "BaseTextarea" */ '@elements/BaseTextarea/index.vue'),
  },
  data() {
    return {
      configurations: {
        buy_later_status: '',
        buy_later_title: '',
        buy_later_module: []
      },
      layoutsApp: [],
      layouts: [],
      disableForm: false,
      loadingBtn: false,
    };
  },
  created(){},
  watch: {
    'configurations.buy_later_status' : function(val, oldVal) {
      if (val == Type_Status.Tp_Habilitado) {
        if (!this.configurations.buy_later_module[0]) {
          this.configurations.buy_later_module[0] = {
            layout_id: Type_IdLayout.Tp_IdCart,
            position: Type_ModulePosition.Tp_ContentBottom,
            entry_type: 1,
            status:Type_Status.Tp_Habilitado,
            limit: '24',
            sort_order: 0,
          };
        }
      }
    },
  },
  computed: {
    sendData() {
      let {configurations} = this;

      if (configurations.buy_later_status == Type_Status.Tp_Desabilitado) {
        configurations.buy_later_module = [];
      }

      return {...configurations};
    }
  },
  beforeMount() {},
  mounted() {},
  methods: {
    onAddModule() {
      if (this.configurations.buy_later_status == Type_Status.Tp_Habilitado) {
        const {configurations} = this;
        const buyLaterModule = configurations.buy_later_module;

        let sortOrder = 0;
        if (buyLaterModule[buyLaterModule.length - 1]) {
          const {sort_order} = buyLaterModule[buyLaterModule.length - 1];
          sortOrder = Number(sort_order) + 1;
        }

        buyLaterModule.push({
          layout_id: Type_IdLayout.Tp_IdProduct,
          limit: '24',
          sort_order: sortOrder,
        });

        this.configurations = {...configurations};
      }
    },
    onRemoveModule(moduleIndex){
      const { configurations } = this;
      const buyLaterModule = configurations.buy_later_module;
      buyLaterModule.splice(moduleIndex, 1);
      this.configurations = {...configurations};
    },
    onPositionChange(moduleIndex) {
      const module = this.configurations.buy_later_module[moduleIndex];
      if (module.position === Type_ModulePosition.Tp_AppMobile && Object.keys(this.layoutsApp).findIndex((el) => el === module.layout_id) === -1) {
        this.configurations.buy_later_module[moduleIndex].layout_id = this.layoutsApp.Tp_Principal || 1;
      }
    }
  }
});

initVue(vm);
