<template>
  <div class="table-wrap">
    <el-table :data="tableData" ref="tableRef" highlight-current-row size="mini" stripe border >

      <el-table-column type="index" label="序号"  width="60" show-overflow-tooltip>
      </el-table-column>

      <el-table-column v-for="(item, index) in colunms" :key="index"
            :prop="item.prop"
            :label="item.label"
            :fixed="item.fixed"
            :formatter="formatter"
            :class-name="item.prop =='status'?'c357F':'' "
            :show-overflow-tooltip="item.tooltip"
            :min-width="item.width"
            >
      </el-table-column>

      <el-table-column  label="操作" min-width="200" fixed="right" >
        <template slot-scope="scope" >
          <el-button  v-if=" projectStatus ==='draft'|| projectStatus ==='audited'|| projectStatus ==='filed'"
            @click="handleEdit(scope.row)" type="text">编辑</el-button>
          <el-button v-if=" projectStatus ==='draft'" @click="handleDelete(scope.row)"  type="text">删除</el-button>
          <el-button @click="handleView(scope.row)" type="text">预览</el-button>
          <el-button @click="handleDownload(scope.row)" type="text">下载</el-button>
        </template>
      </el-table-column>
    </el-table>

    <dialogFile  ref="digFileRef" :project-status="projectStatus" ></dialogFile>
  </div>
</template>

<script>

  import dialogFile from '../dialog/dialog-file.vue'
  export default {
    name: 'document-table',
    components:{
      dialogFile,
    },
    created() {
       this.doQuery();
    },
    mounted() {

    },
    methods: {
      // 查询数据
      doQuery () {
        let param = {
          busId: this.projectid,
          modelId: 3101,
        };
        this.$ajax.post({
          domain: 'crmUrl',
          url: 'project.listProjectFile',
          data: param,
          requestType: 'body',
          showLoad: true,
          success: function (res) {
            if (res.errorCode === 200) {
                 this.tableData = res.data || [];
            }
          }.bind(this),
          error: function (res) {
            this.$message({
              type: 'error',
              message: res.body.msg || res.msg
            })
          }.bind(this)
        })
      },
      //上传
      handleUpdate(row) {
        this.$refs.digFileRef.visible = true;
        this.$refs.digFileRef.openTitle = '新增文件';
        this.$refs.digFileRef.openType = 'add';
      },
      //编辑
      handleEdit(row){
        this.$refs.digFileRef.visible = true;
        this.$refs.digFileRef.openTitle = '编辑文件';
        this.$refs.digFileRef.openType = 'edit';
        let fileObj = {
          size: row.fileSize,
          name: row.fileName,
          response: {
            data: {
              fileId: row.fileId,
            }
          }
        }
        this.$refs.digFileRef.fileList.push(fileObj);
        this.$refs.digFileRef.paramForm = JSON.parse(JSON.stringify(row));
      },
      //删除
      handleDelete (row){

        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$ajax.post({
              domain: 'crmUrl',
              url: 'attachFiles.deleteAttachFile',
              data: {id: row.id},
              requestType: 'body',
              showLoad: true,
              success: function (res) {
                if (res.errorCode === 200) {
                   this.$message({
                     type: 'success',
                     message: '删除成功'
                  })
                  this.doQuery();
                }
              }.bind(this),
              error: function (res) {
                this.$message({
                  type: 'error',
                  message: res.body.msg || res.msg
                })
              }.bind(this)
            })
        }).catch(() => { });

      },
      //预览
      async handleView(row){
        this.$refs.digFileRef.visible = true;
        this.$refs.digFileRef.openTitle = '预览文件';
        this.$refs.digFileRef.openType = 'view';
        let fileObj = {
          size: row.fileSize,
          name: row.fileName,
          response: {
            data: {
              fileId: row.fileId,
            }
          }
        }
        this.$refs.digFileRef.fileList.push(fileObj);
        this.$refs.digFileRef.paramForm = JSON.parse(JSON.stringify(row));
      },
      //下载
      handleDownload(row){
        this.$common.downloadByUrl(this.$common.getDownFileUrl(row.fileId), row.fileName);
          // let param = {
          //   fileId: row.fileId,
          //   busSystem: 'cloud-crm-project',
          //   busModel: 'cloud-crm-project',
          //   uid: row.uuId,
          // };
          // this.$ajax.post({
          //   domain: 'attachUrl',
          //   url: 'downLoadFile',
          //   data: param,
          //   requestType: 'form',
          //   showLoad: true,
          //   success: function (res) {
          //     if (res.errorCode === 200) {
          //         this.$common.downloadByUrl(res.data, row.fileName);
          //     }
          //   }.bind(this),
          //   error: function (res) {
          //     this.$message({
          //       type: 'error',
          //       message: res.body.msg || res.msg
          //     })
          //   }.bind(this)
          // })
      },
      formatter(row, column, cellValue, index){
           if(column.property == 'status'){
               if(row.status == 1 ){
                 return '有效';
               }  else {
                 return '无效';
               }
           } else if(column.property == 'fileSize'){
             return (row.fileSize / 1024).toFixed(2)  + "KB";
           }  else if(column.property == 'fileIndate'){
             if(row.fileIndate){
               if(this.$common.compareDate(row.fileIndate)){
                 return '已失效';
               } else {
                 return '生效中';
               }
             } else if(row.isFlag){
               if(row.isFlag === 1){
                 return '生效中';
               }
             }
           }
           else {
             return cellValue;
           }
       },
    },
    props: ['projectid', 'projectStatus'],
    data() {
      return {

        tableData: [],
        //表格列
        colunms: [
           { prop: 'fileName', label: '文件名称',   width: 120, fixed: false,  tooltip: true },
           { prop: 'fileVersion', label: '版本',   width: 100, fixed: false,  tooltip: true },
           { prop: 'fileTypeKey', label: '文件类型',   width: 100, fixed: false,  tooltip: true },
           { prop: 'signMainPartKey', label: '签订主体',   width: 100, fixed: false,  tooltip: true },
           { prop: 'fileIndate', label: '文件有效期',   width: 100, fixed: false,  tooltip: true },
           { prop: 'fileSuffix', label: '附件格式',   width: 100, fixed: false,  tooltip: true },
           { prop: 'fileSize', label: '文件大小',   width: 100, fixed: false,  tooltip: true },
           { prop: 'createTime', label: '上传时间',   width: 100, fixed: false,  tooltip: true },
           { prop: 'updater', label: '上传人员',   width: 100, fixed: false,  tooltip: true },
           { prop: 'status', label: '文件状态',   width: 100, fixed: false,  tooltip: true },
           { prop: 'remark', label: '备注说明',   width: 100, fixed: false,  tooltip: true },
        ]

      }
    },

  }
</script>

<style>
</style>
