<template>
  <el-table :data="admitList" stripe border v-loading="loading" :highlight-current-row="true">
    <el-table-column align="center" prop="name" label="项目名称"/>
    <el-table-column align="center" prop="className" label="教学班"/>
    <el-table-column align="center" prop="applyUser" label="申请人"/>
    <el-table-column align="center" prop="applyTimeStr" label="申请时间"/>
    <el-table-column align="center" label="操作">
      <template #default="scope">
        <el-button type="success" @click="access(scope.row.id)" v-show="userInfo.role === 'admin'">允许</el-button>
        <el-button type="danger" @click="deny(scope.row.id)" v-show="userInfo.role === 'admin'">拒绝</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: "Admit",
  data() {
    return {
      loading: true,
      userInfo: {
        userId: '',
        role: '',
        username: '',
        userDepart: '',
      },
      admitList: [],
    }
  },
  mounted() {
    this.userInfo.role = window.sessionStorage.getItem("role");
    this.userInfo.userId = window.sessionStorage.getItem("userId");
    this.getAllAdmit()
  },
  methods: {
    getAllAdmit() {
      this.loading = true;
      this.$http({
        url: '/experiments/1/30',
        method: 'post',
        data: {
          status: 0
        }
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: '信息获取失败',
            type: 'error',
          })
        } else {
          this.admitList = data.list;
        }

        this.loading = false;
      })
    },
    access(id) {
      this.loading = true;
      this.$http({
        url: '/isAdmitted/' + id + '/1',
        method: 'put',
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: data.msg,
            type: 'error',
          })
        } else {
          this.getAllAdmit();
        }
        this.loading = false;
      })
    },
    deny(id) {
      this.loading = true;
      this.$http({
        url: '/isAdmitted/' + id + '/2',
        method: 'put',
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: data.msg,
            type: 'error',
          })
        } else {
          this.getAllAdmit();
        }
        this.loading = false;
      })
    },
  }
}
</script>

<style scoped>

</style>
