/*上传报告*/
<template>

  <el-dialog
   ref="userForm"
      class="user-form"
      label-width="120px"
      hide-required-asterisk
    title="上传报告"
  :visible.sync="fileuploadisShow"
    width="500px"
  >
     <el-form class="upfile">
        <el-upload
          ref="upload"
          class="image"
          accept=".doc, .docx, .xml, .xlsx"
          action="string"
          :multiple="false"
          :limit="1"
          :before-upload="onBeforeUploadImage"
          :http-request="UploadImage"
          :on-success="handleSuccess"
          :on-remove="fileRemove"
          :show-file-list="false"
        >
        <p>上传文件名称：{{upname}}
         <el-button size="small"  class="dataSendBtn">选择文件</el-button> 
  
          </p>
          
        </el-upload>
               <el-button
          type="danger"
          size="small"
          class="next-btn"
          @click.native.prevent="Save"
          >上传</el-button >
          <!-- <el-upload
      class="upload-demo sendData"
        :action="url"
         accept=".doc, .docx, .xml, .xlsx"
      :on-preview="handlePreview"
      :on-change="fileChange"
      :before-remove="beforeRemove"
      :limit="1"
      :http-request="uploadFile"
      :auto-upload="true"
      :on-exceed="handleExceed"
      :file-list="fileList"
    >
      <el-button size="small" type="primary" class="dataSendBtn">上传数据</el-button>
     </el-upload>  -->
        </el-form>
   
    <span slot="footer" class="dialog-footer">
   
      <el-button size="small" type="primary" @click="cancel">取消</el-button>
    </span>
  <!-- 下载 -->
<div class="xiazai"   @click="xiazai()">
  <p>{{fileuploadConfig.reportFileName}}</p>
  <el-button
            type="success"
            size="mini"
            icon="el-icon-download"
            plain
            circle
          
          ></el-button>
</div>
  </el-dialog>
</template>

<script>
import { request, uploadFile, getImage } from '@/utils/request'
import store from "@/store";
import { ElMessage } from '@/utils/message.js'
let Message = new ElMessage()
// import { parseTime } from '@/utils/index'
export default {
  name: 'ChainInfo',
  props: {
    fileuploadConfig: {
      isShow: {
        type: Boolean,
        default: () => false,
      },
      projectId: {
        type: String,
        default: () => '',
      },
        reportFileName: {
        type: String,
        default: () => '',
      },
      fileList: {
        type: Array,
        default: () => [],
      },
      projectId:String
    },
  },
  data(){
    const validateFile = (rule, value, callback) => {
      if (value) {
        callback()
      } else {
        callback(new Error('请上传头像'))
      }
    }
   return {
     hrefneme:'',
    upname:'',
     xaizainame:'',
      userForm: {
        name: '',
        personWords: '',
        imageUrl: '',
      },
       fileuploadisShow:false,
      file: '',
      starLevel: 0,
    }
  },
  created() {},
  mounted(){
  },
  methods: {
      cancel() {
                this.fileuploadisShow = false;
                 this.file = ''
            },
      onBeforeUploadImage(file) {
     
    },
    UploadImage(params) {
      console.log(params.file);
      this.upname=params.file.name
      this.userForm.imageUrl = URL.createObjectURL(params.file)
      this.file = params.file
      this.$refs.upload.clearFiles() // 清除文件对象
    },
    fileRemove() {
      this.userForm.imageUrl = ''
      this.file = ''
    },
    // 保存
    Save() {
          const params = new FormData()
          params.append('transCode', 'D00024')
          params.append('projectId', this.fileuploadConfig.projectId)
          params.append('uid', this.$store.getters.userId)
          params.append('reportfile', this.file)
          uploadFile(params, 'service', (progress) => {
            console.log('progress: ' + progress + '%')
          })
            .then((res) => {
              this.$message.success('上传成功');
            //  this.$emit('getData');
              
          this.$parent.getData();

            })
            .catch((err) => {})
    },
     
     handleSuccess (response, file) {
   
      let blob = new Blob([JSON.stringify(response)], { type: 'application/json' });
       console.log('blob', blob)
      var url = window.URL.createObjectURL(blob);
      console.log(url);
      var a = document.createElement('a');
      a.href = url;
      a.download = 'change_' + file.name;
      a.click();
    },
    getImage(id) {
      return getImage(id)
    },
    //下载
    xiazai(){
     let k=getImage(this.fileuploadConfig.reportFile);
     this.hrefneme=k;
     window.location.href = k; 

    }
  },
}
</script>

<style lang="scss" scoped>
.xiazai{
  height: 50px;
  line-height: 50px;
  padding: 0px;
}
.xiazai p{
  float: left;

}
.el-button--mini.is-circle{
    float: right;
}
</style>
