/*
 *@description:导出
 *@author: hudw
 *@date: 2022-05-06 13:51:36
*/
<template>
  <p-button :disabled="disabled" :type="type" @click.stop="handleClick">
    <slot />
    <p-modal v-model="visible" okText="导出" :title="label" :maskClosable="false" :confirmLoading="loading" @ok="handleFormUpload" @cancel="handleCancel">
      <p-grid-form ref="form" style="padding-bottom: 20px" :span="24" :labelCol="{ span: 6 }">
        <p-grid-form-item name="url" :wrapperCol="{ span: 24, push: 3 }" :value="allUrl">
          <a-radio-group
            @change="change"
            :options="[
              { name: '1', label: '导出所有数据', value: allUrl },
              { name: '2', label: '导出当前页数据', value: currentUrl, params: 1212, disabled: !currentUrl },
              { name: '3', label: '导出选择行数据', value: selectUrl, disabled: ids.length === 0 || !selectUrl },
            ]"
          ></a-radio-group>
        </p-grid-form-item>
      </p-grid-form>
    </p-modal>
  </p-button>
</template>
<script>
export default {
  inheritAttrs: false,
  name: 'exportButton',
  props: {
    ids: [],
    pages: {},
    downloadFileName: '',
    radio: {
      type: Boolean,
      default: false,
    },
    label: {
      type: String,
      default: '导出文件',
    },
    allUrl: {
      type: String,
      default: '',
    },
    getSearchValue: {
      type: Function,
    },
    currentUrl: {
      type: String,
      default: '',
    },
    selectUrl: {
      type: String,
      default: '',
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    icon: {
      type: String,
      default: 'cloud-upload',
    },
    size: {
      type: String,
    },
    type: {
      type: String,
      default: 'primary',
    },
    fileKey: {
      type: String,
      default: 'file',
    },

    beforeUpload: {
      type: Function,
      default: null,
    },
  },

  data() {
    return {
      remoteUrl: '',
      fileName: '选择上传文件',
      loading: false,
      visible: false,
      sName: 1,
      params: {},
    }
  },

  methods: {
    change(event) {
      if (event.target.value === this.allUrl) {
        this.params = {}
        this.sName = 1
      } else if (event.target.value === this.currentUrl) {
        this.params = this.pages
        this.sName = 2
      } else if (event.target.value === this.selectUrl) {
        this.params = this.ids
        this.sName = 3
      }
    },
    handleClick(event) {
      this.visible = true
    },
    handleCancel() {
      this.$refs.form.form.resetFields()
    },

    handleFormUpload() {
      this.$refs.form.form.validateFields((err, vals) => {
        if (err) return
        this.remoteUrl = vals.url
        this.loading = true
        let params = this.params
        if (this.sName !== 3 && this.getSearchValue) {
          try {
            params = { ...this.params, ...this.getSearchValue() }
          } catch (err) {}
        }
        return this.handleHttp(params)
      })
    },

    handleHttp(data) {
      let params = {
        url: this.remoteUrl,
        method: 'post',
        responseType: 'blob',
        data,
        timeout: 36000000,
      }

      this.axios(params)
        .then((response) => {
          this.loading = false
          let downloadElement = document.createElement('a')
          let href = window.URL.createObjectURL(response) //创建下载链接
          let fileName = this.downloadFileName + '.xls'
          downloadElement.href = href
          downloadElement.download = decodeURIComponent(fileName) //解码
          document.body.appendChild(downloadElement)
          downloadElement.click()
          document.body.removeChild(downloadElement)
          window.URL.revokeObjectURL(href) //
        })
        .catch((error) => {
          this.loading = false
          this.$exception(error)
        })
    },
  },
}
</script>
