<template>
  <div class="container">
    <!-- 头部 -->
    <div class="header">
      <el-form :inline="true">
        <el-form-item label="文件名称">
          <el-input
            v-model="selFileQurey.conmpanyFileName"
            placeholder="文件名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="query()"
            >查询</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            @click="reset()"
            >重置</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            icon="el-icon-circle-plus"
            @click="addcompanyFile()"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 中间区域 -->
    <div>
      <div class="center">
        <el-table :data="selFileList" style="width: 100%" border >
          <el-table-column label="序号" width="100px" align="center">
            <template slot-scope="scope">
              {{
                (selFileQurey.pageNum - 1) * selFileQurey.pageSize +
                scope.$index +
                1
              }}
            </template>
          </el-table-column>
          <el-table-column prop="conmpanyFileName" label="文件名称"  align="center">
          </el-table-column>
          <el-table-column prop="fileRemarks" label="文件备注" align="center">
          </el-table-column>
          <el-table-column prop="submitTime" label="上传时间" align="center">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="170px" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="download(scope.row)"
                >点击下载</el-button
              >
              <el-button type="danger" size="mini" @click="del(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 尾部 -->
      <div class="footer">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="selFileQurey.pageNum"
          :page-size="selFileQurey.pageSize"
          layout="total, prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
  
  <script>
import { queryBypageByuserId ,getFileUrl,delFile } from "@/api/companyFile";
export default {
  data() {
    return {
      selFileQurey: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        userId: null,
      },
      selFileList: [],
      total: 0,
    };
  },
  created() {
    this.selFileQurey.companyId = this.$store.getters.companyId;
    this.selFileQurey.userId = this.$store.getters.info.userId;
    this.getselFileList();
  },
  methods: {
    //分页查询(查询自己的)
    getselFileList() {
      queryBypageByuserId(this.selFileQurey)
        .then((res) => {
          if (res.success) {
            this.selFileList = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error("获取数据失败");
        });
    },
    //查询
    query() {
      this.selFileQurey.pageNum=1
      this.getselFileList();
    },
    //重置
    reset(){
      this.selFileQurey={
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        userId: null,
      }
      this.selFileQurey.companyId = this.$store.getters.companyId;
      this.selFileQurey.userId = this.$store.getters.info.userId;
      this.getselFileList();
    },
    //下载
    async download(row) {
       var a = document.createElement("a"); //创建一个<a></a>标签 
       a.href=(await getFileUrl(row.companyFile)).data  
      // //a.href = process.env.VUE_APP_BASE_API + row.companyFile; // 给a标签的href属性值加上地址，注意，这里是绝对路径，不用加 点.
      a.target = "_blank";
      a.style.display = "none"; // 障眼法藏起来a标签
      document.body.appendChild(a); // 将a标签追加到文档对象中
      a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
      a.remove(); // 一次性的，用完就删除a标签
    },
    //删除
    del(row){
      delFile(row).then(res=>{
        if(res.success){
          this.$message.success("删除成功")
          if(this.selFileList.length == 1 && this.selFileQurey.pageNum != 1){
              this.selFileQurey.pageNum--
            }
            this.getselFileList()
        }else{
          this.$message.error(res.msg)
        }
      }).catch(error=>{
        this.$message.error("删除失败")
      })
    },
    //换页
    handleCurrentChange(val) {
      this.selFileQurey.pageNum = val;
      this.getselFileList();
    },
    //添加公司文件
    addcompanyFile() {
      this.$router.push("/companyFileAdd");
    },
  },
};
</script>
  
  <style scoped>
.container {
  padding: 10px;
}
.footer {
  text-align: right;
}
</style>