<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-row>
        <a-col :span="24">
          <a-form-model-item label="时间选择" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataTime">
            <a-range-picker v-model="model.dataTime" @change="onChange" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>

import { getExportJx } from '@/api/api'
import { getFileAccessHttpUrl } from '@/api/manage'

export default{
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    data() {
        return {
            model:{},
            title:'绩效导出',
            width:800,
            visible: false,
            disableSubmit: false,
            labelCol: {
            xs: { span: 24 },
            sm: { span: 5 },
            },
            wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
            },
            validatorRules: {
                dataTime: [
                    { required: true, message: '请选择时间!'},
                ],
            },
            // 传输参数
            exportModel: {},
        }
    },
    methods: {
      next () {
        this.visible=true
      },
      // 监听时间选择的值
      onChange(date, dateString) {
        console.log(date, dateString);
        this.exportModel.beginDate = dateString[0]
        this.exportModel.endDate = dateString[1]
        console.log(this.exportModel)
      },
      handleOk () {
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            getExportJx(this.exportModel).then((res) =>{
              // 导出绩效
              let url = window.URL.createObjectURL(new Blob([res],{type: 'application/vnd.ms-excel'}))
              let link = document.createElement('a')
              link.style.display = 'none'
              link.href = url
              link.setAttribute('download', '绩效导出表.xls')
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link); //下载完成移除元素
              window.URL.revokeObjectURL(url); //释放掉blob对象
              this.handleCancel()
              // dundun写的
              // const blob = new Blob([res])
              // const url = window.URL.createObjectURL(blob)
              // let a = document.createElement('a')
              // a.setAttribute('href', url)
              // a.setAttribute('download', `绩效单.xls`)
              // a.click()
            })
          }
        })

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleCancel () {
        this.close()
        this.model= {}
        this.$refs.form.resetFields()
      }

    }
}
</script>
