import Store from 'herculex';
import { getComponentDetail } from '../../utils/service';

const app = getApp();
export default new Store({
  connectGlobal: true,
  state: {
    imgSrcPrefix: app.globalData.imgSrcPrefix,
    userName: state => state.$global.userInfo.userName,
    timeTxt: state => state.$global.timeTxt,
    componentUUID: '',
    templateUUID: '',
    componentType: '',
    serviceList: [],
    errorType: '',
    errorMsg: '',
  },
  actions: {
    async getServiceData({ state, commit }) {
      try {
        my.showLoading();
        const { componentUUID, componentType, templateUUID } = state;
        const [err, response] = await getComponentDetail({
          componentUUID,
          componentType,
          templateUUID,
        });
        if (err || !response || !response.serviceList) throw err;
        if (response.serviceList) {
          commit({ serviceList: response.serviceList });
        }
        my.hideLoading();
      } catch (error) {
        my.hideLoading();
        commit({
          errorType: 'SYSTEM_ERROR',
          errMsg: error && error.msg,
        });
      }
    },
  },
});
