import { connect } from 'herculex';
import dayjs from 'dayjs';
import { storageAPI } from "../../../../core/utils/jsapi";
import { prefixPagePath, dynamicPageName } from "../../../../core/utils";
import { getQrCode } from '../utils';
const app = getApp();
Component(connect({
  mapStateToProps: {
    userInfo: state => state.$global.userInfo,
    passport: state => state.$global.passport,
    titleBarHeight: state => state.$global.titleBarHeight
  }
})({
  props: {
    componentData: {}
  },
  data: {
    imgSrcPrefix: app.globalData.imgSrcPrefix,
    expiresTime: 300,
    qrExpiresTime: 60,
    defaultTips: '',
    timer: null,
    menuList: [{
      name: '重选来访原因',
      type: 'onReselectReason',
      icon: 'https://gw.alipayobjects.com/mdn/rms_41760b/afts/img/A*FrMRR7uHtDIAAAAAAAAAAAAAARQnAQ'
    }]
  },

  async didMount() {
    const {
      componentExtInfo
    } = this.props.componentData;
    const {
      passport
    } = this.data;
    const expiresTime = parseInt(componentExtInfo.expiresTime || this.data.expiresTime);
    const qrExpiresTime = parseInt(componentExtInfo.qrExpiresTime || this.data.qrExpiresTime);
    this.accessTime = dayjs();
    this.setData({
      time: this.accessTime.format('MM月DD日 HH:mm:ss')
    });
    this.timer = setInterval(() => {
      const nowtime = dayjs(); //

      if (passport.qrCodeUrl) {
        if (nowtime.diff(this.accessTime, 'second') >= qrExpiresTime) {
          this.accessTime = dayjs();
          this.refreshQRcode();
        }
      } else if (nowtime.diff(this.accessTime, 'second') >= expiresTime) {
        clearInterval(this.timer);
        my.reLaunch({
          url: `${prefixPagePath}/${dynamicPageName}/index?pageCode=certificateExpiredPage`
        });
        return;
      }

      this.setData({
        time: nowtime.format('MM月DD日 HH:mm:ss')
      });
    }, 1000);
  },

  didUnmount() {
    clearInterval(this.timer);
    this.timer = null;
  },

  methods: {
    async handleClick() {
      const [, {
        data = {}
      }] = await storageAPI.getStorage({
        key: 'passport'
      });
      const {
        communityCode,
        communityName,
        sceneCode
      } = data;
      const passport = {
        communityCode,
        communityName,
        sceneCode
      };
      await storageAPI.setStorage({
        key: 'passport',
        data: passport
      });
      this.commit('$global:updateState', {
        passport
      });
      my.navigateBack();
    },

    async refreshQRcode() {
      const {
        passport
      } = this.data;
      const params = {
        communityCode: passport.communityCode,
        communityName: passport.communityName,
        sceneCode: passport.sceneCode
      };
      const [err, res = {}] = await getQrCode(params);
      if (err || res.result !== 'success') return;
      const newPassport = { ...passport,
        qrCodeUrl: res.qrCodeUrl || ''
      }; // 更新storage

      this.commit('$global:updateState', {
        passport: newPassport
      });
      await storageAPI.setStorage({
        key: 'passport',
        data: newPassport
      });
    }

  }
}));