<template>
  <el-page-header content="实验项目申请" style="margin-bottom: 30px" @back="this.$router.push('/Home')"/>
  <el-card>
    <form @submit.prevent="submit">
      <el-form-item label="项目名称">
        <el-input v-model="form.name" type="text" id="name" required/>
      </el-form-item>
      <el-form-item label="上课班级">
        <el-input v-model="form.className" type="text" id="name" required/>
      </el-form-item>
      <el-form-item class="btn-Ex">
        <el-upload :on-change="fileChange"
                   :show-file-list="false"
                   :auto-upload="false"
                   v-if="form.annex === null">
          <el-button type="primary">上传附件</el-button>
        </el-upload>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">确认</el-button>
      </el-form-item>
    </form>
  </el-card>
  <el-card style="margin-top: 10px">
    <el-table :data="itemList" style="width: 100%">
      <el-table-column fixed prop="name" label="项目名称"/>
      <el-table-column prop="className" label="上课班级"/>
      <el-table-column prop="status" label="审批状态">
        <template #default="scope">
          <el-tag v-show="scope.row.status === 0" type="warning">待审核</el-tag>
          <el-tag v-show="scope.row.status === 2" type="danger">拒绝</el-tag>
          <el-tag v-show="scope.row.status === 1" type="success">通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="getList"
        @current-change="getList"
        v-model:current-page="form.pageNum"

        v-model:page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="form.total"/>
  </el-card>
</template>

<script>
import {computed, ref} from 'vue'
import {ElMessage} from "element-plus";

let formData = new FormData;
const size = ref('')
computed(() => {
  const marginMap = {
    large: '32px',
    default: '28px',
    small: '24px',
  }
  return {
    marginTop: marginMap[size.value] || marginMap.default,
  }
});
export default {
  name: 'ApplyExperiment',
  data() {
    return {
      itemList: [],
      form: {
        name: '',
        className: '',
        pageNum: 1,
        pageSize: 10,
        total: 0,
        annex: null
      },
      size: '',
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    submit() {
      this.$http({
        url: "/experiment",
        method: "post",
        data: this.form
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: '信息获取失败',
            type: 'error',
          })
        } else {
          ElMessage({
            message: data.msg,
            type: 'success',
          })
          this.getList()
        }
      })
    },
    getList() {
      this.$http({
        url: "/experiments/" + this.form.pageNum + "/" + this.form.pageSize,
        method: 'post',
        data: {}
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: '信息获取失败',
            type: 'error',
          })
        } else {
          this.itemList = data.list;
        }
      })
    },
    remove(id) {
      this.$http({
        url: '/experiment/' + id,
        method: 'delete',
      }).then(({data}) => {
        if (data.code !== 200) {
          ElMessage({
            message: data.msg,
            type: 'error',
          })
        } else {
          ElMessage({
            message: "删除成功",
            type: 'success',
          })
          this.getList()
        }
      })
    },
    fileChange(files, fileList) {
      formData.append('file', files.raw)
      files = null;
      this.loading = true;
      this.$http({
        method: 'post',
        url: '/upload',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: '上传成功',
            message: res.data.msg,
            type: 'success'
          });
          this.form.annex = res.data.fileName;
        } else {
          this.$notify({
            title: '文件解析失败',
            message: res.data.msg,
            type: 'error'
          });
        }
        formData = null;
        formData = new FormData();
        this.loading = false;
      })
    },
  }
}
</script>
<style>
.btn-Ex {
  display: inline-flex;
  justify-content: center;
  text-align: center;
  width: 100%;
}
</style>

