import { connect } from 'herculex';
import { getFloorList } from '../utils';
Component(connect({
  mapStateToProps: {
    passport: state => state.$global.passport
  }
})({
  props: {
    componentData: {}
  },
  data: {
    activatedRoomStep: 1,
    activatedTreeIndex: 0,
    focus: false,
    roomSteps: [{
      active: true,
      content: ''
    }, {
      active: false,
      content: ''
    }],
    trees: [],
    currentTree: []
  },

  didMount() {
    // 获取当前页面的参数信息
    const query = getCurrentPages().pop().options || {};
    this.init(query);
  },

  methods: {
    async init(query) {
      const params = {
        sceneCode: query.sceneCode,
        communityCode: query.communityCode,
        communityName: query.communityName
      };
      const data = await getFloorList(params);
      const roomSteps = [{
        active: true,
        content: ''
      }, {
        active: false,
        content: ''
      }];
      data[0].type = 'check';
      roomSteps[0].content = data[0].name;
      this.setData({
        trees: data,
        currentTree: data,
        roomSteps
      });
    },

    onRoomSelectorStepClick(step, index) {
      const {
        trees,
        activatedRoomStep,
        roomSteps
      } = this.data;
      if (index >= activatedRoomStep - 1) return;
      const currentTree = index > 0 ? trees[index - 1].children : trees;
      const defaultRoom = {
        active: false,
        content: ''
      };

      const _roomSteps = roomSteps.map((item, i) => {
        if (i > index) item = defaultRoom;
        return item;
      });

      _roomSteps[index] = step;

      const _currentTree = currentTree.map(item => {
        item.type = step.content && item.name === step.content ? 'check' : 'none';
        return item;
      });

      this.setData({
        activatedRoomStep: index + 1,
        currentTree: _currentTree,
        roomSteps: _roomSteps
      });
    },

    onTreeClick(tree) {
      const defaultRoom = {
        active: false,
        content: ''
      };
      let {
        currentTree,
        activatedRoomStep,
        // eslint-disable-next-line prefer-const
        roomSteps
      } = this.data;
      roomSteps[activatedRoomStep - 1].content = tree.name;

      if (tree.children) {
        roomSteps[activatedRoomStep] = defaultRoom;
        activatedRoomStep += 1;
        currentTree = tree.children;
      }

      const typeTree = currentTree.map(item => {
        const _ = roomSteps[activatedRoomStep - 1].content;
        item.type = _ && item.name === _ ? 'check' : 'none';
        return item;
      });
      this.setData({
        currentTree: [...typeTree],
        activatedRoomStep,
        roomSteps
      });

      if (!tree.children) {
        let roomInfo = '';
        roomSteps.forEach(item => {
          roomInfo += item.content;
        });
        const newPassport = { ...this.data.passport,
          buildingName: roomSteps[0].content,
          unitName: roomSteps[1].content,
          houseName: roomSteps[2].content,
          roomInfo
        };
        this.commit('$global:updateState', {
          passport: newPassport
        });
        my.navigateBack();
      }
    }

  }
}));