<template>
  <div class="app-container">
    <div class="iframeMsg">{{ iframeMsg }}</div>
    <div>
      <iframe name="iframeMap" scrolling="no" width="100%" height="450" frameBorder="no" src="static/eye/eye.html" style="z-index: -1;"/>
    </div>
  </div>
</template>

<script>
import { saveIris } from '@/api/person'
export default {
  name: 'AddIris',
  data() {
    return {
      irisForm: {
        personId: '',
        idCardNo: '',
        image_zy0: '',
        image_yy0: '',
        image_zy1: '',
        image_yy1: ''
      },
      iframeMsg: ''
    }
  },
  mounted() {
    // 接收iframe的传值
    window['vueDefined'] = (receiveParams) => {
      this.receiveParamsFromHtml(receiveParams)
    }
    this.resetForm()
    if (this.$route.query.personId) {
      this.irisForm.personId = this.$route.query.personId
    }
    if (this.$route.query.idCardNo) {
      this.irisForm.idCardNo = this.$route.query.idCardNo
    }
    // console.log(this.irisForm)
  },
  activated() {
    // 接收iframe的传值
    window['vueDefined'] = (receiveParams) => {
      this.receiveParamsFromHtml(receiveParams)
    }
    this.resetForm()
    if (this.$route.query.personId) {
      this.irisForm.personId = this.$route.query.personId
    }
    if (this.$route.query.idCardNo) {
      this.irisForm.idCardNo = this.$route.query.idCardNo
    }
    // console.log(this.irisForm)
  },
  methods: {
    beforeRouteLeave(to, from, next) {
      window.frames['iframeMap'].closeDev()
      next()
    },
    resetForm() {
      this.irisForm = {
        personId: '',
        idCardNo: '',
        image_zy0: '',
        image_yy0: '',
        image_zy1: '',
        image_yy1: ''
      }
    },
    // 接收iframe传来的消息
    receiveParamsFromHtml(res) {
      if (res === 'saveIrisData') {
        this.saveIrisData()
      } else {
        this.iframeMsg = res
      }
    },
    // 保存虹膜数据
    saveIrisData() {
      if (!this.irisForm.personId || !this.irisForm.idCardNo) {
        this.$message.error('人员信息为空，请退回重试')
        return
      }
      this.irisForm.image_zy0 = window.frames['iframeMap'].image0
      this.irisForm.image_zy1 = window.frames['iframeMap'].image1
      this.irisForm.image_yy0 = window.frames['iframeMap'].image2
      this.irisForm.image_yy1 = window.frames['iframeMap'].image3
      saveIris(this.irisForm).then(response => {
        if (response.code === 200) {
          this.$message.success('保存虹膜成功')
          this.resetForm()
          this.$router.push({ path: '/staff' })
        }
      })
    }
  }
}
</script>

<style scoped>
  .iframeMsg{
    height: 50px;
    text-align: center;
    margin-top: 5px;
  }
</style>
