import { prefixPagePath } from "../../../../core/utils";
import { createSubscribe } from 'applet-page-component';
import { reportCmPV_YL } from '../../utils/cloudMonitorHelper';
const app = getApp();

const getRect = id => new Promise(resolve => {
  my.createSelectorQuery().select(`#${id}`).boundingClientRect().exec(resolve);
});

const getExtInfo = item => {
  const {
    componentExtInfo
  } = item;
  return componentExtInfo;
};

Component(createSubscribe({
  // eslint-disable-next-line no-unused-vars
  async onPageScroll(_, {
    scrollTop
  }) {
    const {
      id,
      isScrollOver
    } = this.data;
    let contentHeight = 0;

    if (this.contentHeight) {
      // eslint-disable-next-line prefer-destructuring
      contentHeight = this.contentHeight;
    } else {
      const res = await getRect(id);
      const [rect] = res;
      const {
        height = 0
      } = rect || {};
      contentHeight = height;
    }
    /* 这里动态设置标题栏 */


    if (scrollTop > contentHeight) {
      if (!isScrollOver) {
        const {
          componentData
        } = this.props;
        const {
          title
        } = getExtInfo(componentData);
        my.setNavigationBar({
          title
        });
        this.setData({
          isScrollOver: true
        });
      }
    } else if (isScrollOver) {
      my.setNavigationBar({
        title: ' '
      });
      this.setData({
        isScrollOver: false
      });
    }
  }

})({
  props: {
    componentData: {}
  },
  data: {
    navHeight: 72,

    /* 消息通知是否显示 */
    showMsg: true,
    isScrollOver: false,
    id: `index_heart_${Date.now()}`,
    imgSrcPrefix: app.globalData.imgSrcPrefix
  },

  didMount() {},

  methods: {
    /**
     * 关闭消息通知
     */
    closeMsgBtn() {
      this.setData({
        showMsg: false
      });
    },

    /**
     * 跳转到医院特色
     */
    goToDetail() {
      const pageCode = 'hospital-characteristics';
      my.navigateTo({
        url: `${prefixPagePath}/one/index?pageCode=${pageCode}`
      });
      /* 服务预警，医院介绍 */

      reportCmPV_YL({
        title: '医院介绍'
      });
    },

    /**
     * 获取头部初始头部初始padding-top
     */
    onCallback({
      height
    }) {
      this.setData({
        navHeight: height
      });
    }

  }
}));