<style>
  .add-Enclosure {
    text-align: right;
    padding-bottom: 20px;
  }
</style>
<template>
  <div class="Enclosure">
    <div class="add-Enclosure" v-if="upload !== 2">
      <el-upload class="upload-demo" :file-list="fileList" :limit="1" :http-request="uploadImg2" :multiple="true"
        :show-file-list="false" action="">
        <el-button icon="el-icon-plus" class="crm-btn query-btn crm-btn-mini margin-right0">上传附件</el-button>
      </el-upload>
    </div>
    <el-table v-loading="loading" stripe ref="multipleTable" :data="fileList" tooltip-effect="dark" style="width: 100%"
      row-class-name="font-size-12">
      <el-table-column prop="FileName" label="附件名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="CreateSrt" label="上传人" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="CreateTime" label="上传时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="cfont-color-info">{{scope.row.CreateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="IsFirstContact" label="操作" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="font-color-link cp" style="padding-right: 10px;"
            @click="previewFile(scope.row.FileURL)">预览</span>
          <span class="font-color-warning cp underline" v-if="scope.row.FileUploadType && IsRead === 0"
            @click="deleteItem(scope.row.ID)">删除</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  export default {
    props: ['ID', 'type', 'IsRead', 'upload', 'isGuest'],
    data() {
      return {
        loading: false,
        fileList: [],
        msg: {
          pageIndex: 1,
          pageSize: 10,
          FileSoureId: 0,
          FileSoureType: 1
        },
        fileMsg: {
          FileName: '',
          FileURL: '',
          FileSoureId: '',
          FileSoureType: ''
        }
      };
    },
    watch: {

    },
    mounted() {
      this.msg.FileSoureId = this.ID
      this.msg.FileSoureType = this.type
      this.fileMsg.FileSoureId = this.ID
      this.fileMsg.FileSoureType = this.type
      if (this.isGuest) {
        this.msg.CustomerType = 1
        this.fileMsg.CustomerType = 1
      }
      this.getList()
    },
    methods: {
      getList() {
        this.loading = true
        this.apipost('/api/Customer/GetCustomerFilePage', this.msg, res => {
          if (res.data.resultCode == 1) {
            this.fileList = res.data.data.pageData
            this.loading = false
          }
        })
      },
      AddCustomerFile() {
        this.apipost('/api/Customer/AddCustomerFile', this.fileMsg, res => {
          if (res.data.resultCode == 1) {
            this.msg.pageIndex = 1
            this.getList()
          }
        })
      },
      // 删除
      deleteItem(id) {
        this.$confirm('确定删除此附件，删除后不可恢复', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.apipost('/api/Customer/DelCustomerFile', {
            IDs: id
          }, res => {
            if (res.data.resultCode == 1) {
              this.msg.pageIndex = 1
              this.getList()
              this.$message.success('删除成功')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      previewFile(file) {
        if (file.IsPicture()) {
          this.MsgBus.$emit('showImg', file)
          return
        }
        window.open(file, "_blank");
      },
      // 上传附件
      uploadImg2(file) {
        let newArr = [];
        newArr.push(file.file);
        let path = "/Upload/Temporary/";
        this.UploadSelfFileT(path, newArr, x => {
          this.fileMsg.FileURL = this.domainManager().ViittoFileUrl + x.data.FilePath
          this.fileMsg.FileName = file.file.name
          this.AddCustomerFile()
          this.$message.success('上传成功')
        });
      },
    }
  }
</script>