import { connect } from 'herculex';
import { Ajax, valueToBase64 } from '../utils';
const cardInfoApi = 'api/v1/proxy/isvRequest.healthCard.details';
const openCardApi = 'api/v1/proxy/isvRequest.healthCard.open';
const refreshCardApi = 'api/v1/proxy/isvRequest.healthCard.refresh';
const {
  windowHeight: wH,
  titleBarHeight: tH,
  statusBarHeight: sH
} = my.getSystemInfoSync();
const pageHeight = wH - tH - sH;
Component(connect({})({
  data: {
    step: -1,
    cardInfo: {},
    loading: true,
    viewHeight: `${pageHeight}px`
  },
  props: {
    componentData: {}
  },

  async didMount() {
    const {
      storeConfig
    } = this;
    const state = storeConfig.state;
    const {
      userInfo
    } = state.$global;
    const {
      isLogin = false
    } = userInfo || {};
    /*
    * 判断是否已经登录
    * */

    if (isLogin) {
      await this.getCardInfo();
      return;
    }

    this.setData({
      loading: false
    });
  },

  methods: {
    /*
    * 获取当前页面实例
    * */
    getCurPage() {
      const pages = getCurrentPages();
      return pages[pages.length - 1];
    },

    /*
    * 打开loading
    * */
    loadingShow(msg = '') {
      my.showLoading({
        content: msg
      }).then(() => null);
    },

    /*
    * 关闭loading
    * */
    loadingClose() {
      my.hideLoading({
        page: this.getCurPage()
      }).then(() => null);
    },

    /*
    * 退出页面
    * */
    onExitPage() {
      my.navigateBack();
    },

    /*
    * 获取卡的详情
    * */
    async getCardInfo() {
      let info = null;
      let success = true;

      try {
        // my.showLoading();
        info = await Ajax(cardInfoApi); // my.hideLoading();
      } catch (e) {
        success = false;
        this.getCardInfoFail();
      }

      const _data = {
        loading: false,
        step: 0
      };

      if (success && info) {
        _data.step = 1;
        _data.cardInfo = await this.getInfo(info);
      }

      this.setData(_data);
    },

    /*
    * 获取卡详情失败
    * */
    getCardInfoFail() {
      my.confirm({
        title: '获取卡信息失败!',
        cancelButtonText: '取消',
        confirmButtonText: '重新获取',
        success: ({
          confirm
        }) => !confirm ? this.onExitPage() : this.getCardInfo()
      });
    },

    /*
    * 开卡
    * */
    async onCardOpen() {
      let info = null;
      let success = true;
      this.loadingShow('领取中..');

      try {
        info = await Ajax(openCardApi);
      } catch (e) {
        success = false;
        this.onCardOpenFail();
      }

      const _data = {
        loading: false
      };

      if (success && info) {
        _data.step = 1;
        _data.cardInfo = await this.getInfo(info);
      }

      this.setData(_data);
      this.loadingClose();
    },

    /*
    * 开卡失败
    * */
    onCardOpenFail() {
      my.confirm({
        title: '领取卡片失败!',
        cancelButtonText: '取消',
        confirmButtonText: '重新领取',
        success: async ({
          confirm
        }) => {
          if (confirm) await this.onCardOpen();
        }
      });
    },

    /*
    * 刷新健康卡
    * */
    async onRefreshCard(healthCardId) {
      let info = null;
      let success = true;
      const para = {
        healthCardId
      };
      this.setData({
        loading: true
      });

      try {
        my.showLoading();
        info = await Ajax(refreshCardApi, para);
        my.hideLoading();
      } catch (e) {
        success = false;
        this.onRefreshCardFail();
      }

      const _data = {
        loading: false
      };

      if (success && info) {
        _data.cardInfo = await this.getInfo(info);
      }

      this.setData(_data);
    },

    /*
    * 刷新卡信息失败
    * */
    onRefreshCardFail() {
      my.confirm({
        title: '刷新失败',
        cancelButtonText: '取消',
        confirmButtonText: '重试',
        success: async ({
          confirm
        }) => {
          if (confirm) await this.onRefreshCard();
        }
      });
    },

    /*
    * 组装二维码url数据
    * */
    async getInfo(_info_) {
      const {
        qrCode,
        qrCodeUrl
      } = _info_;

      if (!qrCodeUrl) {
        _info_.qrCodeUrl = await valueToBase64(qrCode);
      }

      return _info_;
    },

    /*
    * 点击认证按钮
    * */
    async onAuthTap() {
      /*
      * 调用授权接口登录
      * */
      const type = '$global:onPageLogin';
      const isLoginSuccess = await this.dispatch(type);

      if (isLoginSuccess) {
        await this.onCardOpen();
      }
    },

    toNextPage(page) {
      this.setData({
        step: page
      });
    },

    onBackCard() {
      this.setData({
        step: 1
      });
    }

  }
}));