/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-15 18:12:52
 * @LastEditors: zhangzhuobin 1093998108@qq.com
 * @LastEditTime: 2022-06-09 14:57:42
 */
import { fhGetNurse, fhGetHospitalMerchant } from "../../../api/hospital/index";
import { nurseStatusEnum, nurseLevelEnum, options } from "../../utils/index";
import { computedAge } from "../../../utils/util";
const { Loading, hLoading, watch } = getApp();
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    query: {
      hospitalId: 1,
      pageNumber: 1,
      pageSize: 10,
      sex: null,
      nurseLevel: null,
      merchantIdList: null,
    },
    // 护工列表数据
    nurse: [],
    // 加载中
    loading: true,
    options,
    unsafeHeight: 0,
    loadingText: "加载中...",
    refresherTriggered: false,
    isLoadingEnd: false,
    // 已选择的信息
    tempPatientInfo: {},
    // 是否给选择框下方数据添加add-zindex类名
    addClass: false,
    // 用户当前选择的下拉菜单类型名称
    menuText: ''
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
  },
  // 页面初始化
  init(e) {
    this.fhGetHospitalMerchant();
    const { from } = e;
    this.tempFrom = from;
    const tempPatientInfo = wx.getStorageSync("tempPatientInfo") || {};
    this.setData(
      {
        tempPatientInfo,
      },
      () => this.getNurse()
    );
    watch("safeArea", this.initDomHeight.bind(this));
  },
  // 页面显示时
  onShow() {
    const { isFirstOnShow } = this.data;

    if (isFirstOnShow) {
      // 首次执行时
      this.setData({
        isFirstOnShow: false,
      });
      return;
    }
  },
  // 到达页面底部时
  reachBottom() {
    let { pageNumber } = this.data.query;
    pageNumber += 1;
    if (pageNumber <= this.pageCount) {
      Loading();
      this.setData(
        {
          "query.pageNumber": pageNumber,
        },
        () => {
          this.getNurse();
        }
      );
    }
  },
  // 获取所有护工
  async getNurse() {
    this.setData({
      isLoadingEnd: false,
    });
    wx.showLoading({ title: "正在加载中", mask: true })
    try {
      const { query, tempPatientInfo } = this.data;
      const { nurseStartTime, nurseEndTime } = tempPatientInfo;
      const send = {
        ...query,
        startTime: nurseStartTime + ":00",
        endTime: nurseEndTime + ":00",
      };
      let {
        results: { results, pageCount, curPage },
      } = await fhGetNurse(send);
      results = results.map((i) => {
        const { status, nurseLevel, birthday } = i;
        i.statusText = nurseStatusEnum.get(+status);
        i.nurseLevelText = nurseLevelEnum.get(+nurseLevel);
        if (birthday) i.age = computedAge(birthday);
        return i;
      });

      this.pageCount = pageCount;
      this.setData(
        {
          nurse: this.data.nurse.concat(results),
          loading: false,
          loadingText: pageCount === curPage ? "没有更多数据" : "加载中...",
          refresherTriggered: false,
        },
        () => {
          hLoading();
          this.setData({
            isLoadingEnd: true,
          });
        }
      );
    } catch (e) {
      console.log("debug axios", e);
    } finally {
      wx.hideLoading()
    }
  },
  // 选中一个护工
  chooseNurse({ currentTarget: { dataset } }) {
    const { info } = dataset;
    wx.$routerTo({
      url: "/nurse/pages/appointment/index",
      type: "reLaunch",
      query: info,
    });
  },
  // 用户打开顶部下拉菜单触发
  openDownList({ currentTarget: { dataset } }) {
    // 选择的下拉菜单类型
    const { key } = dataset;
    console.log('当前选择的下拉菜单类型名称', key)
    // 保存当前点击的下拉菜单类型名称
    this.setData({
      menuText: key
    })
    this.setData({
      addClass: false
    })
  },
  // 用户关闭顶部下拉菜单触发
  closeDownList() {
    this.setData({
      addClass: true
    })
  },
  // 切换查询条件
  queryChange({ currentTarget: { dataset }, detail }) {
    const { key } = dataset;
    const query = {
      ...this.data.query,
      hospitalId: 1,
      pageNumber: 1,
      pageSize: 10,
      [key]: detail,
    };
    this.setData(
      {
        query,
        nurse: [],
      },
      () => {
        this.getNurse();
      }
    );
  },
  // 安全区域
  initDomHeight(value) {
    const { unsafeHeight } = value;
    this.setData({
      unsafeHeight,
    });
  },
  // 下拉刷新
  refresherrefresh() {
    Loading();
    this.setData(
      {
        refresherTriggered: true,
        nurse: [],
        "query.pageNumber": 1,
      },
      () => {
        this.getNurse();
      }
    );
  },
  // 获取所有护工商家
  async fhGetHospitalMerchant() {
    try {
      const { results } = await fhGetHospitalMerchant({
        merchantManagerType: "nurse",
      });
      this.setData({
        "options.merchantIdList": [
          {
            text: "服务公司",
            value: null,
          },
        ].concat(
          results.map((i) => {
            return {
              text: i.merchantName,
              value: i.sellerId,
            };
          })
        ),
      });
    } catch (e) {
      console.log("debug axios", e);
    }
  },
};

Page(pageOptions);
