<template>
  <basic-container class="list">
    <div class="flexBetween">
      <el-button
        type="primary"
        size="mini"
        class="el-icon-plus"
        @click="pushRouter(0)"
      >新增</el-button>
      <el-select
        v-model="value"
        filterable
        style="width:300px;"
        placeholder="请选择"
        clearable
        @change="projectChange"
      >
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.projectName"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </div>

    <el-table
      class="martop20 tableList"
      :data="tableData"
      header-color='red'
      style="width: 100%"
    >
      <el-table-column
        type="index"
        label="序号"
        width="180"
      >
      </el-table-column>
      <el-table-column
        prop="certificateUserName"
        label="证书所有者"
      >
      </el-table-column>
      <el-table-column
        prop="certificateId"
        label="证书ID/企业统一社会信用代码"
      >
      </el-table-column>
      <el-table-column
        prop="certificateType"
        label="证书类型"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.certificateType==1">个人证书</span>
          <span v-else>企业证书</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="isRegister"
        label="注册"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="scope.row.isRegister==0"
            @click="register(scope.row.id)"
          >注册</el-button>
          <el-button
            type="text"
            v-else
            disabled
          >已注册</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="pushRouter(scope.row.id)"
          >编辑</el-button>
          <el-button
            type="text"
            @click="deleteList(scope.row.id)"
            style="color:rgba(240, 99, 10, 1);"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      style="float:right"
      background
      class="martop20 marbottom20"
      layout="prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pageindex"
      :page-size="pagesize"
    >
    </el-pagination>
  </basic-container>
</template>

<script>
import { queryProjectList, listpage, goRegister, remove } from "@/api/certificate/list";

export default {
  data () {
    return {
      value: '',
      options: [],
      tableData: [],
      total: 0,
      pageindex: 1,
      pagesize: 20,
    }
  },
  methods: {
    //#region 页码
    handleSizeChange (val) {
      this.pagesize = val
      this.listpage()
    },
    handleCurrentChange (val) {
      this.pageindex = val
      this.listpage()
    },
    //#endregion

    //#region 头接口
    pushRouter (id) {
      this.$router.push({
        path: '/certificate/list/addList',
        query: {
          id
        }
      })
    },
    //#endregion

    //#region 列表接口
    projectChange () {//下拉框change事件
      this.listpage()
    },
    register (id) {//注册按钮
      this.goRegister(id)
    },
    deleteList (ids) {//删除按钮
      let _that = this
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _that.remove({ ids })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async remove (ids) {
      const { data: res } = await remove(ids)
      console.log(res);
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: "删除证书成功!"
        })
        this.listpage()
      }
    },
    async queryProjectList () {//获取所有项目
      const { data: res } = await queryProjectList()
      console.log(res);
      if (res.code == 200) {
        this.options = res.data
      }
    },
    async listpage () {//分页获取证书列表数据
      const { data: res } = await listpage({
        current: this.pageindex,
        size: this.pagesize,
        projectId: this.value,
      })
      console.log(res);
      if (res.code == 200) {
        this.tableData = res.data.records
        this.total = res.data.total
      }
    },
    async goRegister (id) {//注册
      const { data: res } = await goRegister({ "key" : id })
      console.log(res);
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: "证书注册成功!"
        })
        this.listpage()
      }
    },
    //#endregion
  },
  created () {
    this.queryProjectList()
    this.listpage()
  },
}
</script>


<style lang="scss" scoped>
.tableList {
  /deep/ th.el-table__cell {
    font-size: 16px !important;
  }
}
</style>
