import { connect } from 'herculex';
import { setPagesQuery } from "../../../../core/utils";
import getQueryString from '../../utils/getQueryParams';
import { queryCertDetail, queryServiceByCode } from '../../services';
const app = getApp();
Component(connect({
  mapStateToProps: {
    linkQuery: ({
      $global,
      activeIndex
    }) => {
      const tabBar = $global.tabbar || [];
      const {
        linkQuery
      } = tabBar[activeIndex] || {};
      return linkQuery;
    }
  }
})({
  data: {
    rows: [],
    cardIcon: '',
    certPhotoUrl: '',
    serviceInfo: {},
    details: {},
    loading: true,
    imgSrcPrefix: app.globalData.imgSrcPrefix
  },
  props: {
    componentData: {}
  },

  async didMount() {
    const {
      linkQuery
    } = this.data;
    /*
    * 获取服务里面的参数
    * */

    const _certType = getQueryString(linkQuery, 'certType');

    const _serviceCode = getQueryString(linkQuery, 'serviceCode');
    /*
    * url地址里面的参数
    * */


    const {
      certType = _certType,
      serviceCode = _serviceCode
    } = this.$page.options;
    setPagesQuery({
      certType,
      serviceCode
    });
    await this.fetchServiceInfo(serviceCode);
    await this.fetchCertDetail(certType);
    await this.updateData({
      loading: false
    });
  },

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

    getValue(value) {
      return value || '--';
    },

    /*
    * 获取服务详情
    * */
    async fetchServiceInfo(serviceCode) {
      const [err, serviceInfo] = await queryServiceByCode(serviceCode);
      if (!err) await this.updateData({
        serviceInfo
      });
    },

    /*
    * 获取证件详情
    * */
    async fetchCertDetail(certType) {
      const [err, details] = await queryCertDetail(certType);
      if (err) return;
      const {
        grantOrg,
        userNameDe,
        userCertNoDe,
        identityType
      } = details;
      const rows = [{
        name: '姓名：',
        value: this.getValue(userNameDe)
      }, {
        name: '证件号：',
        value: this.getValue(userCertNoDe)
      }, {
        name: '类型：',
        value: this.getValue(identityType)
      }, {
        name: '颁发单位：',
        value: this.getValue(grantOrg)
      }];
      await this.updateData({
        rows,
        details
      });
    }

  }
}));