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"
  :on-preview="preview">
  <slot>
  <img v-if="result" :src="result" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </slot>
</el-upload>
`
module.exports=
`{
   template:'${tmpl}',
   model:{
    prop: 'value',
    event: 'uploadsuccess'
   },
   props:["listtype","filetype","ifshowlist","value","flag"],
   data:function(){
     return {
       d:{},
       action:'',
       result:this.value,
       multiple:false,
       accept:"",
       showlist:false,
       drag:false,
       listtype:"text",
       flag:this.flag,
     };
   },
   watch:{
     value:function(v){
        this.result=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:{
     getResult(){
        return this.result;
     },
     onExceed(files, fileList){
       this.$emit('onExceed',files, fileList);
     },
     beforeUpload(file){
       this.$emit('beforeUpload',file,{"flag":this.flag,"key":this.d.key});

       if(this.accept!="" && (this.accept.indexOf(file.type) == -1 || file.type=="" || file.type==null)){
          this.$message.warning('文件格式错误，请上传'+this.accept+'格式文件');
          return false;
       }
       this.result="";
       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.$emit('remove',file, 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);
       this.result=xml.documentElement.getElementsByTagName("Location")[0].textContent;
       this.$emit('uploadsuccess',this.result,{"flag":this.flag,"key":this.d.key});
     },
     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-upload2"
}`
