<template>
  <div :closable="true">
    <div :model="file">
      <Button id="upload-btn" type="ghost" style="margin-left:10px;">{{$t("module.userManage.selectFile")}}</Button>
      <Input  style="width: 240px" type="text" v-model="file.excelFile"  /> 
    </div>
    <br>
    <div slot="footer">
      <Button type="ghost" @click.native="onOk">{{$t("module.userManage.confirmImport")}}</Button>
      <Button type="ghost" @click.native="onCancel">{{$t("module.userManage.cancelImport")}}</Button>
    </div>     
  </div>
</template>
<script>
export default {
  props: {
      noticeId: {
          type: String,
          default() {
              return "";
          }
      }
  },
  data(){
    return{
      file:{
        excelFile:""
      },
      uploader: undefined,
    }
  },
  created(){
    this.$emit('SetTitle', "附件上传");
    this.$emit('SetPageSize', 400);   
  },
  mounted(){
    this.uploader = new plupload.Uploader({
      //实例化一个plupload上传对象
      browse_button: "upload-btn",
      url: `${location.origin}/${process.env.API.baseURL}/sys/notice/attach/upload/${this.noticeId}`,
      multi_selection: false,
      headers: {
        crctoken: sessionStorage.crctoken
      },
      multipart_params: {
        name: "file"
      },
      filters: {
      }
    });
    this.uploader.bind("UploadComplete", (uploader, files) => {
      this.file.excelFile = undefined;
      this.$emit('Close');    
      });
    this.uploader.bind("FilesAdded",(uploader,files)=>{
      this.file.excelFile = files[0].name;      
      
    })
    this.uploader.bind("FileUploaded", (uploader, file, responseObject) => {
      let response= eval('(' + responseObject.response + ')');
      if(response.statusCode == "0"){
        this.$Message.success({
          content: response.msg,
          duration: 3
        })
      }else{
        this.$Message.warning({
          content: response.msg,
          duration: 3
        })
      }
    })
    this.uploader.init();
  },

  methods:{
    onOk() {
      //判断文件格式
      let _fileName = this.file.excelFile;
      let fileType = "";
      let reverseName = _fileName.split("").reverse().join("");
      if(reverseName.indexOf(".") > 0){
         fileType = reverseName.split(".")[0];
      }else{
          this.$Message.warning({
          content: this.$t("module.systemNotice.selectRightFile"),
          duration: 3
        });
        return ;
      }

      if(!this.file.excelFile){
        this.$Message.warning({
          content: this.$t("module.systemNotice.pleaseSelectFile"),
          duration: 3
        })
        return ;
      }
      this.uploader.start();          
    },
    onCancel() {
      this.$emit('Close');
    },

  },
}
</script>
