<template>
  <div>
    <!-- <van-button @click="eee" class="mt12" type="primary">人脸识别</van-button> -->
    <!-- <van-uploader name="fileListIdFront" ref="uploader" capture="camera" :preview-image="false" :after-read="afterRead" class="mt10 ml20">
      <slot></slot>
    </van-uploader> -->
    <input type="file" ref="file" @change="addImg" accept="image/*" capture="camera" style="display:none" />
  </div>
</template>
<script>
import { Uploader, button } from 'vant'
import { uploadImg, recognition } from '@/api/ebiz/sale/sale'
export default {
  name: 'UploadImageFile',
  props: {
    // 是否展示
    realName: {
      type: String,
      default: ''
    },
    // start_ocr: {
    //   type: String,
    //   default: ''
    // },
    idno: {
      type: String,
      default: ''
    },
    typeface: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      file: '',
      imgName: '',
      fileListIdFront: [],
      isSuccess: false
    }
  },
  components: {
    [button.name]: button,
    [Uploader.name]: Uploader
  },
  watch: {
    // typeface(v) {
    //   console.log(v, 'v')
    //   if (v == '1') {
    //     this.$refs.file.dispatchEvent(new MouseEvent('click'))
    //     this.typeface = '0'
    //   }
    // }
    typeface: {
      deep: true,
      handler: function() {
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
        //     this.typeface = '0'
      }
    }
  },
  methods: {
    eee() {
      console.log(this.$refs.uploader)
      this.$refs.file.dispatchEvent(new MouseEvent('click'))
    },
    addImg(e) {
      this.$toast.loading({
        // 持续展示 toast
        duration: 0,
        // 禁用背景点击s
        forbidClick: true,
        loadingType: 'spinner',
        message: '加载中……'
      })
      let that = this
      this.file = this.$refs.file.files[0]
      var reader = new FileReader()
      reader.onload = (function(file) {
        return function(e) {
          let result = this.result
          console.info(this.result)

          // 上传图片
          uploadImg(formdata).then(res => {
            let num = 0
            that.recognition(res, num)
          })
        }
      })(e.target.files[0])
      reader.readAsDataURL(e.target.files[0])

      let formdata = new FormData()
      formdata.append('imgPath', that.file)
    },
    recognition(val, num) {
      let data = {}
      num++
      console.log(num)
      if (window.localStorage.failedNumber == '0') {
        // 第一次人脸失败传180
        data = { imgData: val.path, realName: this.realName, idno: this.idno, isFly: '270', num: num }
      } else {
        data = { imgData: val.path, realName: this.realName, idno: this.idno, isFly: '90', num: num }
      }
      recognition(data).then(res => {
        if (res.result == '0' && res.content.result == '1') {
          window.localStorage.failedNumber = ''
          this.isSuccess = true
        } else if (res.result == '0' && res.content.result == '2') {
          window.localStorage.failedNumber = '0'
          this.recognition(val, num)
        } else {
          this.$toast(res.resultMessage)
        }
        this.$emit('sendimage', this.isSuccess)
      })
    }
  }
}
</script>
