<template>
  <p-card>

    <div slot="title" style="display: flex; align-items: center; cursor: pointer" @click="backPage('back')">
      <a-icon type="arrow-left" style="color: #1990ff" class="mr-10" />
      {{ title }}
    </div>
    <a-spin :spinning="spinning">

    <p-table
      :row-select-type="1"
      dragging
      :rowSelectType="false"
      ref="cardTable"
      :bordered="true"
      rowKey="id"
      auto-width
      :remoteUrl="remoteUrl"
      :remoteData="params"
      :editor-row-table="false"
      :data-source="list"
      :columns="columns"
    >
      <span slot="action" slot-scope="text, record">
        <div style="display: flex">
          <div>
            <a @click="upLoadChange(text, record)" style="margin-right: 10px">上传</a>
          </div>
          <div>
            <a @click="grid2Main03Table_checkClick(text, record)" style="margin-right: 10px">查看</a>
          </div>
            <div>
            <a @click="grid2Main03Table_downLoadClick(text, record)">下载</a>
          </div>
        </div>
      </span>
    </p-table>
    </a-spin>
    <imgsUpload v-model="isUpload" :id="uploadId" :isView="lookats"></imgsUpload>
  </p-card>
</template>
<script>
import imgsUpload from '@/blocks/ImgsUpload/index'

export default {
  components: { imgsUpload },
  props: {
    pUrl: {
      type: String,
      default: '/api/oms-std-maindata/pubCertificate/pubCertificateQuery/v1.0'
    },
    params: {
      type: Object,
      default() {
        return {}
      }
    },
    columns: {
      type: Array,
      default() {
        return []
      }
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      remoteUrl: {
        url: this.pUrl,
        // params: this.params,
        headers: {
          'Content-Type': 'application/JSON'
        },
        method: 'post',
        timeout: 30000
      },
      isUpload: false,
      lookats: false,
      uploadId: '',
      list: [],
      spinning: false
    }
  },
  created() {
    // console.log(this.remoteUrl,'2233')
  },
  watch: {
    // params: {
    //   handler(newVal, oldVal) {
    //   },
    //   deep: true,
    //   immediate: true
    // },
  },
  computed: {},
  methods: {
    upLoadChange(text, record) {
      this.uploadId = record.id
      this.isUpload = true
      this.lookats = false
    },
    grid2Main03Table_checkClick(text, record) {
      this.uploadId = record.id
      this.isUpload = true
      this.lookats = true
    },
     grid2Main03Table_downLoadClick(text, record) {
      //   console.log(record, 111)
      let _this = this
      this.spinning = true
      let request = {
        bizId: record.id,
        bizType: 0,
        enterpriseid: _this.$store.state.user.info.enterpriseId
      }
      this.axios({
        method: 'post',
        url: '/api/oms-std-maindata/sysFile/querySysPicture',
        headers: { 'Content-Type': 'application/json' },
        data: JSON.stringify(request),
        // responseType: 'blob',
      }).then((_res) => {
        if (_res.data.length) {
          _res.data.forEach((item, index2) => {
            let obj = item.fileUrl
            var index = obj.lastIndexOf('.')
            obj = '.' + obj.substring(index + 1, obj.length)
            let num = ''
            if (_res.data.length == 1) {
              num = ''
            } else {
              num = '_' + (index2 + 1)
            }
            let str = ''
            str = record.parentname + '_' + record.certificateName + num + obj

            _this
              .axios({
                url: '/api/oms-std-business/biz-file/download/url' + '?url=' + item.fileUrl,
                method: 'get',
                responseType: 'arraybuffer',
                // headers: {
                //   'content-type': 'application/x-www-form-urlencoded',
                // },
                // data: {
                //   url: item.fileUrl,
                // },
              })
              .then((res) => {
                let blob = new Blob([res])
                let downloadElement = document.createElement('a')
                let href = window.URL.createObjectURL(blob) //创建下载的链接
                downloadElement.href = href
                downloadElement.download = str // '货主.xls' //下载后文件名
                document.body.appendChild(downloadElement)
                downloadElement.click() //点击下载
                document.body.removeChild(downloadElement) //下载完成移除元素
                window.URL.revokeObjectURL(href) //释放掉blob对象
              })
              .catch((error) => {
                _this.$message.error('服务忙，请稍后！')
              })
          })
        }

        this.spinning = false
      })
    },
    backPage() {
      this.$emit('back')
    }
  }
}
</script>
<style lang="less">
.ant-upload-list-text {
  display: none;
}
</style>
