<template>
  <div class="app-container">
    <h3 class="toolbar">
      <div class="tool">
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleDownloadBatch">批量下载</el-button>
      </div>
    </h3>
    <el-table v-loading="loading" :data="list" border @selection-change="handleSelectionChange" style="width: 100%">
      <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="'PROJECT'"></upload-enclosure>
    <div class="container" style="margin-top: 20px;">
      <div v-show="loading" class="well loading">正在加载中，请耐心等待...</div>
      <div v-show="!loading" class="well" ref="output"></div>
    </div>
  </div>
</template>

<script>
  import {getEnclosureApi,deleteEnclosureApi,getProjectInfoApi,getProjectInfoCommonApi} from '@/api/enterprise/project/project'
  import UploadEnclosure from './UploadEnclosure'
  import { getExtend, readBuffer, render } from '@/utils/util';
  import { parse } from 'qs';
  import axios from 'axios'
  export default {
    components: {
      UploadEnclosure
    },
    props: {
      projectOwner: {
        type: String
      },
      finOwner:{
        type: String
      }
    },
    data() {
      return {
        total: 0,
        enclosureVisible: false,
        list: [],
        id: this.$route.query.id,
        dataId: undefined,
        attachType: 'PROJECT',
        loading: false,
        selectionList: [],
        ids: '',
        info: {},
        queryParams: {
          pageNum: 1,
          pageSize: 10
        }
      };
    },
    created() {
      this.id = this.$route.params.id || undefined;
      this.getData()
    },
    methods: {
      getData() {
        getProjectInfoCommonApi(this.id).then(res => {
          if(this.projectOwner == '1') {
            this.info = res.data.entProject || {}
            this.dataId = this.info.id
          }else if(this.projectOwner == '2') {
            this.info = res.data.techProject || {}
            this.dataId = this.info.id
          }else if(this.projectOwner == '3') {
            this.info = res.data.finProject || {}
            this.dataId = this.info.id
          }
          this.getList()
        })
      },
      getList() {
        if(this.finOwner == 'fin') {
          this.attachType = 'FINACIAL'
        }
        getEnclosureApi(this.attachType,this.dataId).then(res => {
          if(res.code == 200) {
            this.list = res.data.records
            this.total = res.data.total
          }
        })
      },
      handleDownload(row) {
        window.open("http://124.232.146.72:7015/api/common/attach/"+row.id)
      },
      handleDownloadBatch() {
        if(this.selectionList.length <=0) {
          this.$message.error('请选择要下载的数据');
          return
        }
        window.open("http://124.232.146.72:7015/api/common/attach/"+this.ids)
        this.getList();
      },
      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("删除成功");
            }
        	})
        })
      },
      // 从url加载
      handleView(row) {
        let routeUrl = this.$router.resolve({
          path: '/enclosurePreview',
          query: {url: row.url,fileName: row.fileName,id: row.id}
        });
        window.open(routeUrl.href, "_blank");
      },
      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>
