<template>
  <div>
    <el-dialog :visible.sync="dialogTableVisible" :width="width + 'px'">
      <!--<vue-qr
        ref="qrCode"
        :text="qrUrl"
        :size="width"
        :correct-level="3"
        :margin="5"
        :callback="qrCodeCallback"
      />-->
      <div class="qrCode">
        <el-image :src="itemObj.qrCodeImage" style="width: 200px;" />
        <div class="btn">
          <el-button type="primary" @click="downLoad">下载</el-button>
          <el-button @click="dialogTableVisible = false">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import VueQr from 'vue-qr'
import serviceMixin from '../../mixin/index'

export default {
  components: {
    // VueQr
  },
  mixins: [serviceMixin],
  props: {
    showQrCode: {
      type: Boolean,
      default: () => false
    },
    width: {
      type: Number,
      default: () => 300
    },
    itemObj: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      dataUrl: ''
    }
  },
  watch: {
    showQrCode(val) {
      if (val) {
        this.dialogTableVisible = true
      }
    },
    dialogTableVisible(val) {
      if (!val) {
        this.$emit('showQrCode')
      }
    }
  },
  methods: {
    // qrCodeCallback(val) {
    //  console.log(val)
    //  this.dataUrl = val
    // },
    downLoad() {
      this.$confirm('是否下载二维码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.isLoading = true
        // 使用a链接下载
        const downUrl = `${process.env.VUE_APP_BASE_API}/providers/download/image?ids=${this.itemObj.id}&type=1`
        this.downloadWithUrl(downUrl, this.itemObj.name)
          .catch((err) => {
            console.log(err)
            this.$message.error(`下载${this.itemObj.name}二维码失败`)
          })
          .finally(() => {
            this.$message({
              message: '下载成功',
              type: 'success'
            })
            this.dialogTableVisible = false
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.qrCode {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .btn {
    width: 100%;
    display: flex;
    justify-content: space-around;
    margin-top: 30px;
  }
}
</style>
