/* eslint-disable no-undef */
import { connect } from 'herculex';
import { setPagesQuery } from "../../../../core/utils/index";
Component(connect({
  mapStateToProps: {
    courtInfo: state => state.$global.courtInfo,
    currentCourt: state => state.$global.currentCourt,
    passport: state => state.$global.passport
  }
})({
  props: {
    src: '',
    // 判断来源，扫码进入的页面会更新passport相关数据
    communityCode: '' // 扫码进入的时候，或者传入参数有小区id的时候，小区id

  },
  data: {
    activeTab: 0,
    // 选中的大菜单
    currentId: 1,
    // 当前小区id
    list: [] // 小区列表

  },

  didMount() {
    // 获取小区列表, 如果小区列表数据获取失败，即报提示错误
    // eslint-disable-next-line max-len
    if (!this.data.courtInfo || !this.data.courtInfo.list || this.data.courtInfo.list.length === 0) {
      my.showToast({
        type: 'fail',
        content: '获取数据失败',
        duration: 3000
      });
      return;
    }

    const {
      list
    } = this.data.courtInfo;
    const ch_small = 'a';

    for (let i = 0, len = list.length; i < len; i++) {
      const item = list[i];
      item.index = i;
      item.title = item.name;
      item.anchor = String.fromCharCode(ch_small.charCodeAt(0) + i);
    } // 获取当前选中的小区


    let {
      courtName,
      id: currentId
    } = this.data.currentCourt; // 外部传入的小区id,如果外部传入了小区id,优先用外部传入的

    if (this.props.communityCode) {
      currentId = this.props.communityCode;
      let currentCourt = null;

      for (const item of this.data.courtInfo) {
        // eslint-disable-next-line no-loop-func
        currentCourt = item.list.find(every => every.id === id);

        if (currentCourt) {
          courtName = item.name;
          break;
        }
      }
    }

    const _current = list.find(item => item.title === courtName);

    let activeTab = 0;

    if (_current) {
      activeTab = _current.index;
    }

    this.setData({
      activeTab,
      currentId,
      list
    });
  },

  methods: {
    handleChange(index) {
      this.setData({
        activeTab: index
      });
    },

    onChange(index) {
      this.setData({
        activeTab: index
      });
    },

    handleSelect(e) {
      const data = e.target.dataset.value;
      data.courtName = this.data.list[this.data.activeTab].title;
      const {
        courtInfo
      } = this.data;
      courtInfo.currentId = data.id;
      this.setData({
        currentId: data.id
      });
      this.commit('$global:updateState', {
        currentCourt: data
      });
      this.commit('$global:updateState', courtInfo); // 设置app.js中全局的参数

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

      if (this.props.src === 'visit-user') {
        // 更改passport
        const passport = { ...this.data.passport,
          communityCode: id,
          communityName: name
        };
        my.removeStorage({
          key: 'passport'
        });
        my.setStorage({
          key: 'passport',
          data: passport
        });
        this.commit('$global:updateState', passport);
      } // 返回上级页面


      my.navigateBack();
    }

  }
}));