<template>
  <el-upload
    class="upload-demo"
    action="string"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :before-upload="beforeUpload"
    multiple
    :file-list="fileList"
    :modal-append-to-body="false"
    ref="n_u_l_c"
    >
    <template v-if="btn=='btn'">
      <el-button size="small" type="success">点击上传</el-button>
    </template>
    <template v-else>
      <el-button size="small" type="text">点击上传</el-button>
    </template>
  </el-upload>
</template>
<script>
import {strToFileList} from "@/utils/tv.js"
 export default {
     props:{
        itemId:{required: true,type: String},
        type: {required: true,type: String},
        idx: {required: false,type: String},
        btn: {required: false,type: String},
        fList :{required: false,type: String},
     },
    data() {
      return {
          fileList:[],
          params:{}
      }
    },
    methods: {
      handleRemove(file, fileList) {
        let type = this.type
        let k = 1
        let body = file;
        let files = fileList
        this.$emit("handleResult",{k,type,body,files});
      },
      handlePreview(file) {
      },
      beforeRemove(file, fileList) {
        return true;//this.$confirm(`确定移除 ${ file.name }？`);
      },
      beforeUpload(file){
        //market/tvImport/upload
        let query = new FormData();
        query.append("file", file);
        query.append("itemId", this.itemId);
        query.append("idx", this.idx);
        query.append("type", this.type);
        this.$http({
            url: "/market/tvImport/upload",
            method: "post",
            headers: {
                "Content-Type": "application/json",
            },
            data: query,
        }).then((res) => {
            if(res.data.result===0){
              this.handleResult(1,this.type,res.data.body)
            }else{
              this.$message({ message: res.data.desc,type: 'error'});
            }
        });
      },
      handleResult(k,type,body){
        let file = {"name":body.realName,"url":body.path + "/" + body.fileName,"fileId":body.fileId,"itemId":body.itemId,"idx":body.idx}
        this.fileList.push(file)
        let files = this.fileList
        //console.log(this.fileList)
        this.$emit("handleResult",{k,type,body,files});
      },
      resetFile(fileList){
        this.fileList = fileList
      },
      getFileList(){
        return this.fileList
      }
    },
    watch:{
      itemId(){
        this.fileList = strToFileList(this.fList)
      },
      idx(){
        this.fileList = strToFileList(this.fList)
      }
    },
    created(){
      this.fileList = strToFileList(this.fList)
    }
 }
</script>
<style>

</style>
