<template>
  <div class="app-container">
    <div class="tips">
      加计扣除备查资料清单：1.自主、委托、合作研究开发项目计划书和企业有权部门关于自主、委托、合作研究开发项目立项的决议文件；2.自主、委托、合作研究开发专门机构或项目组的编制情况和研发人员名单；3.经科技行政主管部门登记的委托、合作研究开发项目的合同；4.从事研发活动的人员（包括外聘人员）和用于研发活动的仪器、设备、无形资产的费用分配说明（包括工作使用情况记录及费用分配计算证据材料）；5.集中研发项目研发费决算表、集中研发项目费用分摊明细情况表和实际分享收益比例等资料；6.“研发支出”辅助账及汇总表；7.企业如果已取得地市级（含）以上科技行政主管部门出具的鉴定意见，应作为资料留存备查；8.《研发费用加计扣除优惠明细表》（a107012，选择预缴享受的企业留存备查）。
    </div>
    <h3 class="toolbar">
      <div class="tool">
        <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleUpload" v-if="userType == 'ENT_ADMIN'">一键上传</el-button>
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleDownloadBatch">批量下载</el-button>
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleBatchDelte" v-if="userType == 'ENT_ADMIN'">批量删除</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)" v-if="userType == 'ENT_ADMIN'">删除</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} 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
    },
    data() {
      return {
        total: 0,
        enclosureVisible: false,
        list: [],
        id: this.$route.query.id,
        dataId: undefined,
        attachType: 'PROJECT',
        loading: false,
        selectionList: [],
        ids: '',
        queryParams: {
          pageNum: 1,
          pageSize: 10
        }
      };
    },
    computed: {
      userType() {
        return this.$store.state.user.userType
      }
    },
    created() {
      this.id = this.$route.query.id || undefined;
      this.getData()
    },
    methods: {
      getData() {
        getProjectInfoApi(this.id).then(res => {
          let obj = res.data.entProject || {}
          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(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>
