<template>
  <zj-form-module title="活动网点" label-width="120px" :form-data="item" :form-items="formItems">
    <div style="text-align: right; padding-bottom: 10px; padding-right: 10px">
      <el-button
        v-if="item.promotionQuestionnaireQrcodes.length && item.id"
        size="mini"
        type="primary"
        @click="piliangxiazai"
        >批量下载二维码</el-button
      >
    </div>
  </zj-form-module>
</template>

<script>
import editTable from '@/components/template/editTable.js'
import { getToken } from '@/utils/auth'
import { zhapi } from '@/utils/request'
export default {
  props: {
    item: {
      type: Object,
      default: () => ({})
    },
    typeView: {
      type: [String, Number],
      default: 0
    }
  },
  mixins: [editTable],
  data() {
    return {}
  },
  computed: {
    formItems() {
      return [
        {
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            label: '',
            'label-width': '0px',
            prop: 'promotionQuestionnaireQrcodes',
            rules: []
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return this.convertTableJson(
              value,
              [
                {
                  columnAttributes: {
                    label: '网点名称',
                    prop: 'websitName'
                  },
                  editRender: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`promotionQuestionnaireQrcodes.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-input
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                          ></el-input>
                        </el-form-item>
                      </div>
                    )
                  },
                  viewRender: (h, { row, column, index }) => {
                    return <div style="padding-left:10px">{row[column.columnAttributes.prop]}</div>
                  }
                },
                {
                  columnAttributes: {
                    label: '活动二维码',
                    prop: 'qrcode',
                    width: 160
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        {row[column.columnAttributes.prop] ? (
                          <el-image
                            src={this.$showImgUrl(row[column.columnAttributes.prop])}
                            preview-src-list={[this.$showImgUrl(row[column.columnAttributes.prop])]}
                            fit="fit"
                            style="width:80px;height:80px;"
                          />
                        ) : null}
                      </div>
                    )
                  }
                }
              ],
              {
                isEdit: true,
                isAdd: this.qx(this.item),
                isUpdate: false,
                isDel: ({ row, column, index }) => {
                  return !row.id
                }
              },
              {
                add: () => {
                  this.item.promotionQuestionnaireQrcodes.push({
                    websitName: '',
                    qrcode: ''
                  })
                  this.isEditTableIndex = this.item.promotionQuestionnaireQrcodes.length - 1
                },
                delete: ({ row, column, index }, cb) => {
                  if (this.isEditTableIndex == index) {
                    this.isEditTableIndex = -1
                  } else if (this.isEditTableIndex > index) {
                    this.isEditTableIndex--
                  }
                  cb && cb()
                },
                verify: ({ row, column, index }, isEditTableIndex) => {
                  return new Promise(r => {
                    if (isEditTableIndex > -1) {
                      this.acquireVerify(this, 'dqangqianyemian')(
                        [`promotionQuestionnaireQrcodes.${isEditTableIndex}.websitName`],
                        v => {
                          if (v) {
                            r(true)
                          } else {
                            r(false)
                          }
                        }
                      )
                      r(true)
                    } else {
                      r(true)
                    }
                  })
                },
                btnSlot: ({ row, column, index }) => {
                  if (row.id && row.qrcode) {
                    return (
                      <el-button
                        type="text"
                        onClick={() => {
                          window.open(
                            zhapi(
                              process.env.VUE_APP_BASE_API,
                              `/promotion/questionnaire/download?id=${this.item.id}&id2=${row.id}&token=${getToken()}`
                            )
                          )
                        }}
                      >
                        下载二维码
                      </el-button>
                    )
                  }
                  return null
                }
              }
            )
          }
        }
      ]
    }
  },
  methods: {
    qx(item) {
      return (
        !!this.typeView &&
        (!item.id || !!~[0, 1].indexOf(item.status) || !!~['待生效', '生效中'].indexOf(item.statusText))
      )
    },
    acquireVerify(v, name) {
      if (v.$parent[name]) {
        return v.$parent[name]
      } else {
        return this.acquireVerify(v.$parent, name)
      }
    },
    piliangxiazai() {
      window.open(
        zhapi(process.env.VUE_APP_BASE_API, `/promotion/questionnaire/download?id=${this.item.id}&token=${getToken()}`)
      )
    }
  }
}
</script>

<style lang="scss"></style>
