import { connect } from 'herculex';
import { getIn } from 'herculex/dist/utils/manipulate';
import { getDepList } from '../utils/service';
import { history } from '../utils';
import { reportCmPV_YL } from '../utils/cloudMonitorHelper';
Component(connect({
  mapStateToProps: {
    bookingInfo: state => getIn(state, ['$global', 'bookingInfo'], {}),
    titleBarHeight: state => state.$global.titleBarHeight
  }
})({
  props: {
    componentData: {}
  },
  data: {
    showLoading: true,
    noData: false,
    keyWord: '',
    // 关键词
    // 选择科室
    deparmentList: [],
    chidrenList: [],
    // 预约信息
    bookInfo: {}
  },

  didMount() {
    console.log(this.data.titleBarHeight);
    const {
      serviceCode
    } = getCurrentPages().pop().options;
    reportCmPV_YL({
      title: '预约挂号',
      query: {
        serviceCode
      }
    });
    this.getDepartmentList();
    my.setNavigationBar({
      title: '选择科室'
    });
  },

  methods: {
    // 获取科室列表
    async getDepartmentList(departmentId = '', keyWord) {
      this.setData({
        noData: false,
        showLoading: true,
        deparmentList: [],
        chidrenList: []
      });
      const params = {
        departmentId,
        keyWord
      };
      const [, res] = await getDepList(params);

      if (res && res.length) {
        const departmentList = this.addTitle(res);
        this.setData({
          showLoading: false,
          activeTab: 0,
          deparmentList: departmentList,
          noData: false
        }, () => {
          // 查询第一个科室的子科室
          departmentId = this.data.deparmentList[0].departmentId;
          const departmentName = this.data.deparmentList[0].name;
          const {
            hasChildren
          } = this.data.deparmentList[0];
          this.getChildrenList(departmentId, departmentName, hasChildren, keyWord);
        });
      } else {
        this.setData({
          showLoading: false,
          noData: true
        });
      }
    },

    // 增加title属性匹配组件展示
    addTitle(list) {
      return list.map(item => {
        // eslint-disable-next-line no-param-reassign
        item.title = item.name;
        return item;
      });
    },

    // 根据父级科室获取子科室列表
    async getChildrenList(parentId, parentName, hasChildren, keyWord) {
      if (!hasChildren) {
        this.setData({
          chidrenList: this.addTitle([{
            departmentId: parentId,
            name: parentName,
            hasChildren
          }])
        });
        return;
      }

      this.setData({
        chidrenList: []
      });
      const params = {
        parentId,
        parentName,
        keyWord
      };
      const [, res] = await getDepList(params);

      if (res && res.length) {
        const departmentList = this.addTitle(res);

        if (this.data.deparmentList && this.data.deparmentList.length) {
          this.setData({
            chidrenList: departmentList
          });
        }
      }
    },

    handleChange(index) {
      this.setData({
        activeTab: index
      });
      const {
        departmentId,
        name,
        hasChildren
      } = this.data.deparmentList[index]; // 查询对应的子科室

      this.getChildrenList(departmentId, name, hasChildren, this.data.keyWord);
    },

    clearkeyWord() {
      this.setData({
        keyWord: ''
      });
      this.getDepartmentList();
    },

    handleInput(keyWord) {
      this.setData({
        keyWord
      });
      this.getDepartmentList('', keyWord);
    },

    onItemClick(e) {
      const {
        departmentId,
        departmentName
      } = e.target.dataset;
      const bookInfo = {
        depId: departmentId,
        depName: departmentName
      };
      this.setData({
        bookInfo
      });
      history.openPage({
        pageType: 'choose-a-doctor',
        data: {
          bookInfo
        }
      });
    },

    handleRefresh() {
      this.getDepartmentList();
    }

  }
}));