<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    @close="handleClose">
    <el-form
      inline
      :model="form"
      ref="form"
      class="form-wrap"
      label-width="120px">
      <div class="dialog-info">
        <el-form-item
          style="width:100%"
          label="名称"
          prop="channel_name">
          <el-input
            v-model="form.channel_name"
            disabled></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="自动开发票"
          prop="auto_invoice">
          <el-switch
            v-model="form.auto_invoice"
            :inactive-value="0"
            :active-value="1"
            active-text="启用"
            inactive-text="禁用"
          >
          </el-switch>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="自动开发票时间"
          prop="auto_invoice_time">
          <el-time-picker
            :editable="false"
            :clearable="false"
            v-model="form.auto_invoice_time"
            value-format="HH:mm:ss"
            placeholder="选择时间"
          >
          </el-time-picker>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="接口地址"
          prop="url">
          <el-input
            v-model="form.url"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="允许开发票天数"
          prop="allow_days">
          <el-input-number
            v-model="form.allow_days"
            :precision="0"
            :min="0"></el-input-number>
        </el-form-item>
      </div>

      <el-form-item
        v-if="form.channel_name==='百旺云票平台'"
        style="width:100%"
        label="人脸识别二维码"
      >
        <div
          id="printMyQrCode"
          class="img-wrap img-wrap1"
          style="{display:block}">
          <div
            class="img"
            :style="{backgroundImage:`url(${qrImg})`,height:'100%'}">
          </div>
        </div>
        <div
          style="text-align: center;">
          <el-button
            :disabled="disabled"
            @click="getQrCode">
            刷新二维码
          </el-button>
        </div>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">
        取 消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateInvoiceChannel, invoiceBaiWangGetFaceQrcode } from '@/api/ticketType'
import QRCode from 'qrcode'

const defaultForm = {
  auto_invoice: 1,
  auto_invoice_time: null,
  url: '',
  allow_days: 0
}

export default {
  data () {
    return {
      qrImg: '',
      disabled: false,
      title: '',
      visible: false,
      loading: false,
      form: {
        ...defaultForm
      }
    }
  },
  methods: {
    async getQrCode () { // 百旺云票平台 专用 定时获取字符串 并渲染成 二维码
      this.disabled = true
      const { extraInfo } = await invoiceBaiWangGetFaceQrcode({})
      this.disabled = false

      this.$nextTick(async () => {
        var opts = {
          errorCorrectionLevel: 'H',
          type: 'image/png',
          quality: 1,
          color: {
            dark: '#000000ff', // Blue dots
            light: '#0000' // Transparent background
          }
        }
        this.qrImg = await QRCode.toDataURL(extraInfo, opts)
      })
    },
    show (data, type) {
      this.type = type
      this.title = (type === 'edit' ? '编辑渠道' : '新增渠道')
      this.visible = true

      this.$nextTick(() => {
        this.reset()

        if (type === 'edit' && data) {
          this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        } else {
          this.form = JSON.parse(JSON.stringify(defaultForm))
        }

        if (this.form.channel_name !== '百旺云票平台') return
        this.getQrCode()
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))

          this.loading = true
          if (this.type === 'edit') {
            updateInvoiceChannel(params).then(res => {
              this.$message.success('更新成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          } else {
            updateInvoiceChannel(params).then(res => {
              this.$message.success('添加成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    }
  }
}
</script>

    <style lang="scss" scoped>
    .periodItem {
      display: flex;  margin: 0 10px 10px 0; align-items: center;
    }
    .form-wrap {
      .priceInput {
         margin: 0 10px;
        .el-input-number{
            width: 160px;
        }
        ::v-deep .el-input {
          width: 100%;
        }
      }
    }

    .img-wrap{
  height:300px;
}
.img{
  height: 100%; background: no-repeat center center /contain;
}
    </style>
