<template>
  <div class="expiredCompanyCleanup">
    <!-- <div class="expiredCompanyCleanup-header">
      头部
    </div> -->
    <div class="expiredCompanyCleanup-content">
      <el-table :data="tableData" @selection-change="handleSelectionChange" border height="80vh" v-loading="btnDeleteLoading" element-loading-text="删除中..." element-loading-background="rgba(255, 255, 255, 0.8)" style="width: 100%;">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="企业ID" width="100" align="center" />
        <el-table-column prop="companyName" label="企业名称" min-width="240" align="center" />
        <el-table-column prop="setMeal" label="是否签约" min-width="80" align="center">
          <template slot-scope="scope">{{ scope.row.setMeal ? "已签约" : "未签约" }}</template>
        </el-table-column>
        <el-table-column prop="expirationDate" label="有效期" min-width="160" align="center" />
        <el-table-column prop="staffCountMax" label="人数上限" min-width="80" align="center" />
      </el-table>
    </div>
    <!-- 底部 -->
    <div class="pagination-container">
      <el-button type="primary" :loading="btnDeleteLoading" :disabled="!multipleSelection.length"
        @click="deleteEnterprise()" size="small">批量删除</el-button>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
        :page-sizes="[100, 200, 300, 400]" :page-size="100" layout="total, prev, pager, next, sizes"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "expiredCompanyCleanup",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      btnDeleteLoading: false,
      total: 0,
      page: 1,
      size: 100,
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    handleCurrentChange(val) {
      this.page = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page = 1
      this.size = val
      this.getTableData()
    },
    deleteEnterprise() {
      this.$confirm('此操作将永久删除选中的企业, 是否删除?', '删除企业', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.btnDeleteLoading = true
        const companyIds = this.multipleSelection.map(item => item.id).join(',')
        this.postData(`/company/delNeedDelList`, { companyIds }).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.page = 1
          this.getTableData()
        }).finally(() => {
          this.btnDeleteLoading = false
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getTableData() {
      this.postData('/company/getNeedDelList', {
        pageIndex: this.page,
        pageSize: this.size
      }).then(res => {
        this.tableData = res.data.record || []
        this.total = res.data.total
      })
    },
    // 单独封装请求
    async postData(urls, param = {}) {
      return new Promise((resolve, reject) => {
        this.http.post(urls, { ...param },
          res => {
            if (res.code == 'ok') {
              resolve(res)
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
              reject(res)
            }
            resolve(res)
          },
          error => {
            this.$message({
              message: error,
              type: "error"
            });
            reject(error)
          }
        )
      });
    },
  }
}
</script>

<style lang="scss">
.expiredCompanyCleanup {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 10px 20px;

  .expiredCompanyCleanup-content {
    flex: 1;
  }

  .pagination-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 20px 0;
  }
}
</style>