import history from "./../../utils/history";
import { getContentHeight } from "../../utils";
import { getDepartmentList } from "./service";
import { queryServiceByCode } from "../../service/common";
import { reportCmPV_YL } from "../../utils/cloudMonitorHelper";
import { envContext } from "../../../../core/utils/constants";
Component({
  props: {
    componentData: {},
  },
  data: {
    index: 0,
    show: false,
    menus: [],
    cache: {},
    lastCache: {},
    loading: true,
    serviceItem: null,
    contentHeight: getContentHeight(),
  },

  async didMount() {
    /* 服务预警, 预约挂号 */
    reportCmPV_YL({
      title: "预约挂号",
    });
    const { hospitalDistrictId } = this.getQuery();

    try {
      await this.getServiceDetail(); // eslint-disable-next-line no-empty
    } catch (e) {}
    await this.fetchMenus(hospitalDistrictId);
  },

  methods: {
    updateData(data) {
      return new Promise((resolve) => this.setData(data, resolve));
    },

    async getServiceDetail() {
      const code = "hospital_service_code";
      const { link, uuid, title, linkType, accessMode, serviceDesc } =
        await queryServiceByCode(code);
      if (uuid) {
        const serviceItem = {
          url: link,
          accessMode,
          name: title,
          serviceDesc,
          urlType: linkType,
          serviceUUID: uuid,
        };
        await this.updateData({
          serviceItem,
        });
      }
    },

    getQuery() {
      const { $routeConfig } = this.$page;
      return $routeConfig.query;
    },

    async fetchProxy(func, ...arg) {
      my.showLoading();
      await func(...arg).finally(my.hideLoading);
    },

    /* 检测缓存 */
    hasCache() {
      const { index, cache } = this.data;
      return (cache[index] || []).length > 0;
    },

    /* 设置缓存 */
    setCache(list) {
      const { index: i, cache: _cache } = this.data;
      const cache = { ..._cache };
      cache[i] = list;
      return this.updateData({
        cache,
      });
    },

    /* 获取一级列表数据 */
    async fetchMenus(hospitalDistrictId) {
      const params = {
        hospitalDistrictId: envContext === 'th' ? "" : hospitalDistrictId,
      };
      const list = await getDepartmentList(params);
      await this.updateData({
        menus: list,
        loading: false,
      });
      /* 获取一级列表下面的第一项的第一个 */
      const departmentId = this.getDepartmentId(0);
      if (list) {
        await this.fetchDepartments(departmentId, list[0]);
      }
    },

    /* 获取二级列表 */
    async fetchDepartments(departmentId, item) {
      if (this.hasCache()) return;
      const list = !departmentId
        ? [item]
        : await getDepartmentList({
            parentId: departmentId,
          });
      this.setCache(list);
    },

    /* 获取科室id */
    getDepartmentId(index) {
      const { menus = [] } = this.data;
      const { hasChildren, departmentId } = menus[index] || {};
      if (hasChildren) return departmentId;
    },

    /* 切换科室 */
    async onTapMenu(i) {
      const item = this.data.menus[i];
      await this.updateData({
        index: i,
      });
      const departmentId = this.getDepartmentId(i);
      await this.fetchDepartments(departmentId, item);
    },

    /* 获取三级列表数据 */
    async onTapOpenLast(item) {
      const { departmentId } = item;
      const { lastCache } = this.data;
      const cacheList = lastCache[departmentId] || [];
      if (cacheList.length > 0) return cacheList;
      my.showLoading();
      const list = await getDepartmentList({
        parentId: departmentId,
      }).finally(my.hideLoading);
      lastCache[departmentId] = list;
      await this.updateData({
        lastCache: { ...lastCache },
      });
      return list;
    },

    onTapItem(item) {
      const query = this.getQuery();
      const { name: areaName } = query;
      const { componentData } = this.props;
      const {
        departmentId,
        name: dpName,
        departmentCode: depCode,
        hospitalId,
      } = item;
      const { subHospitalTitle, subHospitalId } = query;
      history.push({
        query: {
          departmentId,
          depCode,
          hospitalId,
          subHospitalTitle,
          subHospitalId,
        },
        title: `${dpName}(${areaName})`,
        pageType: "hospital-num-source",
        componentData: { ...componentData, department: item },
      });
    },
  },
});