<template>
  <div class="page">
    <pageTitle title="企业实名认证" />
    <steps :data="dict.enterpriseRealnameSteps" :index="1" />
    <page-module name="企业基本信息" icon="info">
      <BaseInfo ref="baseInfo" :data="baseData"/>
    </page-module>
    <page-module name="证件信息" icon="info">
      <CertInfo ref="certInfo" :data="certData" />
    </page-module>
    <div class="next-step">
      <el-button size="small" plain @click="onPrevStep">上一步</el-button>
      <el-button size="small" type="primary" @click="onNextStep">下一步</el-button>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import BaseInfo from './components/base-info'
import CertInfo from './components/cert-info'
import Common from '@/components/Common'
export default {
  name: 'EnterpriseRealnameStep2',
  components: {
    BaseInfo,
    CertInfo
  },
  mixins: [Common],
  data() {
    return {
      pageData: {}
    }
  },
  computed: {
    ...mapGetters(['dict']),
    baseData() {
      return this.pageData.baseData || {}
    },
    certData() {
      return this.pageData.certData || {}
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getCacheData()
      if (this.$route.params.from === 'prev') {
        this.restoreCacheData()
      } else {
        this.resetStepData()
      }
    },

    getCacheData() {
      this.cacheData = this.cacheGetStepData(this.cacheKey)
    },

    restoreCacheData() {
      if (this.isValidObject(this.cacheData)) {
        this.pageData = this.cloneObject(this.cacheData)
      } else {
        this.resetStepData()
      }
    },

    resetStepData() {
      this.pageData = {
        baseData: {},
        certData: {}
      }
    },

    /**
    * 上一步
    * @time 2022-04-23 16:33:24
    */
    onPrevStep() {
      this.$router.push({ name: window.OS.isTablet ? 'EnterpriseRealnameStepPad1' : 'EnterpriseRealnameStep1', params: { from: 'prev' }})
    },

    /**
    * 下一步
    * @time 2022-04-23 15:50:05
    */
    async onNextStep() {
      try {
        const valid = [
          this.$refs.baseInfo.validate(),
          this.$refs.certInfo.validate()
        ]
        await Promise.all(valid)

        const baseData = this.$refs.baseInfo.getPendingCacheData()
        const certData = this.$refs.certInfo.getPendingCacheData()
        this.cacheSetStepData(this.cacheKey, { baseData, certData })

        this.$router.push({ name: 'EnterpriseRealnameStep3' })
      } catch (e) {
        e.anchor()
      }
    }
  }
}
</script>
