var tmpl=`
<el-upload ref="upload"
  class="avatar-uploader"
  :action="action"
  :data="d"
  :multiple="multiple"
  :on-exceed="onExceed"
  :accept="accept"
  :before-upload="beforeUpload"
  :on-success="success"
  :on-remove="remove"
  :on-error="error"
  :on-progress="progress"
  :show-file-list="showlist"
  :limit="limitnum"
  :on-preview="preview">
  <slot><el-button @click="custumClick">点击上传</el-button></slot>
</el-upload>
`
module.exports=
`{
   template:'${tmpl}',
   model:{
    prop: 'value',
    event: 'uploadsuccess'
   },
   props:["listtype","filetype","ifshowlist","value","flag","limitnum"],
   data:function(){
     return {
       d:{},
       action:'',
       results:this.value?this.value:[],
       multiple:true,
       accept:"",
       showlist:true,
       drag:false,
       listtype:"text",
       flag:this.flag,
       limitnum:this.limitnum
     };
   },
   watch:{
     value:function(v){
        this.results=v;
     }
   },
   mounted:function(){
    if(this.filetype!=undefined && this.filetype!=null){
      this.accept=this.filetype;
    }
    if(this.ifshowlist!=undefined && this.ifshowlist!=null){
      this.showlist=this.ifshowlist;
    }
    var that=this;
    this.$root.postReq("/web/uploadCtl/getOssConfig","").then(function(d){
        that.d["key"]="key";
        that.d["Bucket"]=d.Bucket;
        that.d["OSSAccessKeyId"]=d.OSSAccessKeyId;
        that.d["Signature"]=d.Signature;
        that.d["policy"]=d.policy;
        that.d["success_action_status"]=d.success_action_status;
        that.action=d.url;
    });
   },
   created:function(){

   },
   methods:{
     custumClick(){
        this.results=[];
        this.$refs.upload.clearFiles();
     },
     getResult(){
        return this.results;
     },
     onExceed(files, fileList){
       this.$message.warning('当前限制选择 '+this.limitnum+' 个文件，本次选择了 '+files.length+' 个文件');
     },
     beforeUpload(file){
       this.$emit('beforeUpload',file);
       if(this.accept!="" && (this.accept.indexOf(file.type) == -1 || file.type=="" || file.type==null)){
          this.$message.warning('文件格式错误，请上传'+this.accept+'格式文件');
          return false;
       }
       var myDate = new Date();
       var key = "zc_"+Math.round(Math.random()*500)+myDate.getMonth()+myDate.getTime()+myDate.getFullYear()+myDate.getDate()+myDate.getHours()+myDate.getMinutes()+myDate.getSeconds()+myDate.getMilliseconds()+file.name.replace(" ","");
       this.d.key=key;
       this.$emit('setKey',file.name,this.flag);
     },
     remove(file, fileList){
       this.results.splice(this.results.findIndex(v => v.name ===file.name), 1);

       fileList=[];
     },
     error(err, file, fileList){
       this.$emit('error',err);
     },
     progress(event, file, fileList){
       console.log("progress");
     },
     preview(file){
       console.log("preview");
     },
     success(response, file, fileList){
       var xml= this.loadXML(response);
       var result=xml.documentElement.getElementsByTagName("Location")[0].textContent;
       this.results.push({name:file.name,url:result});
       if(this.results.length==fileList.length){
          this.$emit('uploadsuccess',this.results,fileList);
       }
     },
     loadXML(xmlString){
       var xmlDoc=null;
       if(!window.DOMParser && window.ActiveXObject){
           var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
           for(var i=0;i<xmlDomVersions.length;i++){
               try{
                   xmlDoc = new ActiveXObject(xmlDomVersions[i]);
                   xmlDoc.async = false;
                   xmlDoc.loadXML(xmlString);
                   break;
               }catch(e){
               }
           }
       }
       else if(window.DOMParser && document.implementation && document.implementation.createDocument){
           try{
               domParser = new  DOMParser();
               xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
           }catch(e){
           }
       }
       else{
           return null;
       }
       return xmlDoc;
     },
   },
   vname:"gsb-upload3"
}`
