<template>
  <div>
    <Alert type="error" v-if="errMsg.show">{{errMsg.msg}}</Alert>
    <Row>
      <Col span="24">
        <div  id='CameraCtl' style="width:100%; height: 400px">
            <img :src="stockData" style="width:100%; height: 400px">
        </div>
      </Col>
      <Col span="24">
        <Button type="primary" class="camera_btn" @click="TakePhoto" :loading="loading">拍照</Button>
<!--        <Button type="primary" class="camera_btn" @click="Cam_Focus">对焦</Button>-->
<!--        <Button type="primary" class="camera_btn" @click="Cam_ZoomIn">放大</Button>-->
<!--        <Button type="primary" class="camera_btn" @click="Cam_ZoomOut">缩小</Button>-->
      </Col>
    </Row>
  </div>
</template>

<script>
import { createUpLoadFiles, getSign } from '@/api/common'
import Camera from '_c/stock-camera'
export default {
  name: 'height-camera',
  components: {},
  props: {
    isOpen: false,
    filePath: {
      type: String,
      default: () => {
        return ''
      }
    },
    fileName: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data () {
    return {
      rules: {},
      errMsg: {
        show: false,
        msg: ''
      },
      stockData: Object,
      FenBianLvList: [
        { label: '选择', value: '1' }
      ],
      fenbianlv: '',
      loading: false,
      Camera: Object
    }
  },
  created () {
    let _this = this
    this.Camera = Camera
    this.Camera.start(function (event) {
      // _this.acceptMsg(event)
    }, function (event) {
      _this.ShowInfo(event.data)
    })
    this.Camera.runmessage = this.acceptMsg
  },
  watch: {
    isOpen (e) {
      if (e) {
        try {
          this.Camera.sendMsg('bStartPlay')
          this.Camera.runmessage = this.acceptMsg
        } catch (e) {
          this.ShowInfo('没有发现设备')
        }
      } else {
        this.Camera.close()
      }
    }
  },
  methods: {
    acceptMsg (event) {
      this.stockData = 'data:image/jpeg;base64,' + event.data
    },
    formatDate (time) {
      let date = new Date(time)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hour = date.getHours()
      let min = date.getMinutes()
      let sec = date.getSeconds()
      let newTime = year +
        (month < 10 ? '0' + month : month) +
        (day < 10 ? '0' + day : day) +
        (hour < 10 ? '0' + hour : hour) +
        (min < 10 ? '0' + min : min) +
        (sec < 10 ? '0' + sec : sec)
      return newTime
    },
    ShowInfo (op) {
      this.errMsg = { show: true, msg: op }
      let that = this
      setTimeout(function () {
        that.errMsg = { show: false, msg: '' }
      }, 2000)
    },
    toOpenCamera () {
      this.ShowInfo('打开了设备！')
    },
    // 拍照
    TakePhoto () {
      this.Camera.cat()
      this.baseImg64 = this.Camera.endData.data
      // this.upimg(this.Camera.endData.data)
      if (this.Camera.endData.data) {
        this.upimg(this.Camera.endData.data)
      } else {
        this.ShowInfo('拍照失败！')
      }
    },
    // 对焦
    Cam_Focus () {},
    // 放大
    Cam_ZoomIn () {},
    // 缩小
    Cam_ZoomOut () {},
    // 选择分辨率
    SelectResolution (e) {},
    upimg (base64img) {
      this.backImgUrl = ''
      let that = this
      this.loading = true
      let params = {}
      params.file = base64img
      params.filePath = this.filePath
      console.log(this.filePath)
      params.fileName = this.fileName
      var time = new Date().getTime()
      this.timeStep = time.toString()
      let param = {
        filePath: params.filePath,
        fileName: params.fileName,
        timestamp: this.timeStep
      }
      getSign(param).then(res => {
        if (res.data.errcode === 0) {
          console.log(this.filePath)
          createUpLoadFiles(res.data.data.docking_url, { sign: res.data.data.sign, timestamp: this.timeStep, filePath: this.filePath, fileName: this.fileName, file: base64img }).then(res => {
            if (res.data.errcode === 0) {
              that.loading = false
              // console.log(res.data.data, 's')
              // let data = JSON.parse(res.data.data)
              that.backImgUrl = res.data.data
              that.$emit('ret-fun', { url: res.data.data })
              // that.$emit('resultAction', res.data, base64img)
            } else {
              that.loading = false
              that.$Notice.error({ title: '上传失败', desc: '请重新拍照' })
            }
          })
        } else {
          this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
        }
      })
    }
  }
}
</script>
<style>
  .camera_btn{
    margin-right: 5px;
    margin-top: 5px;
  }
</style>
