import { connect } from 'herculex';
import { getIn } from 'herculex/dist/utils/manipulate';
import { getCourtlistInfo } from './../utils';
import { setPagesQuery } from "../../../../core/utils/index";
const app = getApp();
Component(connect({
  mapStateToProps: {
    userInfo: state => state.$global.userInfo,
    currentCourt: state => state.currentCourt
  }
})({
  props: {
    componentData: {}
  },
  data: {
    imgSrcPrefix: app.globalData.imgSrcPrefix
  },

  async didMount() {
    const {
      longitude,
      latitude
    } = getApp().globalData.regionData;
    const [err, res] = await getCourtlistInfo({
      lng: longitude,
      lat: latitude
    });
    if (err || !res) return [err, null];
    let currentCourt = null;

    for (const item of res.list) {
      currentCourt = item.list.find(every => every.id === res.currentId);
      currentCourt.courtName = item.name;
      if (currentCourt) break;
    }

    this.commit('$global:updateState', {
      courtInfo: res
    }); // 全部小区列表信息

    this.commit('$global:updateState', {
      currentCourt
    }); // 全部小区列表信息
    // 设置app.js中全局的参数

    const {
      id,
      name
    } = currentCourt;
    setPagesQuery({
      communityCode: id,
      communityName: name
    });
  },

  didUpdate(preProps) {
    // eslint-disable-next-line max-len
    if (preProps.componentData.componentExtInfo.memberInfo && this.props.componentData.componentExtInfo.memberInfo) {
      const preFlag = preProps.componentData.componentExtInfo.memberInfo.flag || false;
      const flag = this.props.componentData.componentExtInfo.memberInfo.flag || false;

      if (!preFlag && flag) {
        const {
          serviceList = []
        } = this.props.componentData;
        const {
          url,
          urlType
        } = serviceList[0] || {};
        const payload = {
          urlType,
          url
        };

        if (url) {
          return this.$page.dispatchGlobal('handleJumpService', payload);
        }
      }
    }
  },

  methods: {
    async addCard(e) {
      const {
        componentExtInfo: {
          applyUrl,
          cardTemplateId
        }
      } = this.props.componentData;
      const index = getIn(e, ['target', 'dataset', 'index']);
      const result = await this.dispatch('addCard', {
        index,
        applyUrl,
        cardTemplateId
      });
      if (result.code && result.code === 'USER_BACK') return; // 失败

      if (result !== 'addCardSuccess') {
        const {
          serviceList = []
        } = this.props.componentData;
        const {
          url,
          urlType
        } = serviceList[1] || {};
        const payload = {
          urlType,
          url
        };

        if (url) {
          return this.$page.dispatchGlobal('handleJumpService', payload);
        }
      }
    },

    goToCardDetail(e) {
      const memberInfo = getIn(e, ['target', 'dataset', 'memberInfo'], {});
      const {
        passId
      } = memberInfo;

      if (passId) {
        this.commit({
          memberInfo: {
            isOpenCardDeail: true,
            ...memberInfo
          }
        });
        my.openCardDetail({
          passId
        });
      }
    },

    onJumpAreaSelect() {
      my.navigateTo({
        url: '/antbuilder/industry/smartcommunity/pages/area-select/index'
      });
    }

  }
}));