import { navigateTo } from '../../utils';
import { getIn } from 'herculex/dist/utils/manipulate';
import { connect } from 'herculex';

Component(connect({
  mapStateToProps: {
  },
})({
  props: {
    componentData: {},
    serviceList: [],
    imgSrcPrefix: '',
  },
  data: {
    indicatorDots: true,
    lists: [],
  },
  didMount() {
    const lists = this.sliceArray(this.props.serviceList, 4);
    this.setData({ lists });
  },
  methods: {
    sliceArray(array, size) {
      const result = [];
      for (let x = 0; x < Math.ceil(array.length / size); x++) {
        const start = x * size;
        const end = start + size;
        result.push(array.slice(start, end));
      }
      return result;
    },
    onSubmit(e) {
      const item = getIn(e, ['buttonTarget', 'dataset', 'item'], {});
      const formId = getIn(e, ['detail', 'formId'], '');

      this.commit('$global:updateFormId', {
        formId,
      });
      if (item.accessMode === 'customer' && item.from !== 'webview') {
        const customerCheckInPage = this.dispatch.bind(this, 'customerCheck');
        return navigateTo.call(this, {
          ...item,
          customerCheckInPage,
          isToService: true,
        });
      }
      navigateTo.call(this, {
        ...item,
        isToService: true,
      });
    },
  },
}));
