<template>
  <div class="app-container">
    <h3 class="toolbar">
      <div class="tool">
        <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleUpload">一键上传</el-button>
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleDownload">批量下载</el-button>
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleBatchDelte">批量删除</el-button>
      </div>
    </h3>
    <el-table v-loading="loading" :data="list" border @selection-change="handleSelectionChange" style="width: 100%" @select-all="selectAll">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column label="文件名称" prop="fileName" align="center"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleDownload(scope.row)">下载</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
    <upload-enclosure ref="uploadEnclosure" v-if="enclosureVisible" @refreshData="getList()" :dataId="dataId" :attachType="'FINACIAL'"></upload-enclosure>
  </div>
</template>

<script>
  import {getEnclosureApi,deleteEnclosureApi,getProjectInfoApi} from '@/api/enterprise/project/project'
  import UploadEnclosure from './UploadEnclosure'
  export default {
    components: {
      UploadEnclosure
    },
    data() {
      return {
        total: 0,
        enclosureVisible: false,
        list: [],
        dataId: undefined,
        id: this.$route.query.id,
        attachType: 'FINACIAL',
        loading: false,
        selectionList: [],
        ids: '',
        queryParams: {
          pageNum: 1,
          pageSize: 10
        }
      };
    },
    created() {
      this.id = this.$route.query.id || undefined;
      this.getData()
    },
    methods: {
      getData() {
        getProjectInfoApi(this.id).then(res => {
          let obj = res.data.finProject || {}
          this.dataId = obj.id //财务资料id
          this.getList()
        })
      },
      getList() {
        getEnclosureApi(this.attachType,this.dataId).then(res => {
          if(res.code == 200) {
            this.list = res.data.records
            this.total = res.data.total
          }
        })
      },
      handleUpload() {
        this.enclosureVisible = true
        this.$nextTick(() => {
          this.$refs.uploadEnclosure.init()
        })
      },
      handleDownload() {

      },
      handleBatchDelte() {
        if(this.selectionList.length <=0) {
          this.$message.error('请选择要删除的数据');
          return
        }
        this.$confirm('请确认是否删除所选择的数据！', '提示').then(() => {
        	deleteEnclosureApi(this.ids).then(res => {
            if(res.code == 200) {
              this.queryParams.pageNum = 1;
              this.getList();
              this.$modal.msgSuccess("删除成功");
            }
        	})
        })
      },
      handleView() {

      },
      handleDelete(row) {
        const id = row.id;
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteEnclosureApi(id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
      },
      // 取消全选
      selectAll(selection) {
        if (selection.length == 0) {
          let list = this.selectedList.filter(item => !this.list.some(ele => ele.id === item.id));
          this.selectionList = list;
        }
      },
      handleSelectionChange(val) {
        this.selectionList = val;
        if(this.selectionList.length > 0) {
          let selectionIds = []
          for (let i = 0, len = this.selectionList.length; i < len; i++) {
          	selectionIds.push(this.selectionList[i].id)
          }
          this.ids = selectionIds.join(',')
        }else {
          this.ids = ''
        }
      }
    }
  };
  </script>

</script>

<style>
</style>
