<template>
  <div style="margin-left: 160px">
    <div class="buttonBox">
      <el-upload
        action=""
        accept=".xlsx, .xls"
        :auto-upload="false"
        :show-file-list="false"
        :on-change="handle"
        >
        <el-button type="primary" slot="trigger">选取EXCEL文件</el-button>
        <el-button type="danger" @click="postDatatoServer">上传</el-button>
      </el-upload>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="username" label="姓名" width="160" align="center"></el-table-column>
      <el-table-column prop="parentclass" label="大类" width="200" align="center"></el-table-column>
      <el-table-column prop="childclass" label="小类" width="160" align="center"></el-table-column>
      <el-table-column prop="subpoint" label="小点" width="140" align="center"></el-table-column>
      <el-table-column prop="scoringelement" label="依据" align="center"></el-table-column>
      <el-table-column prop="awardtime" label="时间" width="160" align="center"></el-table-column>
      <el-table-column prop="score" label="得分" width="140" align="center"></el-table-column>

    </el-table>
  </div>
</template>


<script>
import * as xlsx from 'xlsx'
import { readFile } from '../../../utils/xlsx'
let postData = null
export default {
  name: "IntakeData",
  data() {
    return {
      tableData: [],
    }
  },
  mounted() {
  },
  methods: {
    async handle(ev) {
      let file = ev.raw;
      if (!file) return;
      let data = await readFile(file);
      let workbook = xlsx.read(data, {type:"binary"}),
        worksheet = workbook.Sheets[workbook.SheetNames[0]];
      data = xlsx.utils.sheet_to_json(worksheet);
      /**
       * 修改 Json 中 key 值
       */
      data = JSON.parse(JSON.stringify(data).replace(/姓名/g, 'username'));
      data = JSON.parse(JSON.stringify(data).replace(/小类合计/g, 'childscore'));
      data = JSON.parse(JSON.stringify(data).replace(/大类合计/g, 'parentscore'));
      data = JSON.parse(JSON.stringify(data).replace(/大类/g, 'parentclass'));
      data = JSON.parse(JSON.stringify(data).replace(/小类/g, 'childclass'));
      data = JSON.parse(JSON.stringify(data).replace(/小点/g, 'subpoint'));
      data = JSON.parse(JSON.stringify(data).replace(/依据/g, 'scoringelement'));
      data = JSON.parse(JSON.stringify(data).replace(/时间/g, 'awardtime'));
      data = JSON.parse(JSON.stringify(data).replace(/得分/g, 'score'));

      /**
       * 展示 Excel 内容
       */
      this.tableData = data;

      /**
       * 修改 Json 中部分 value 值以适配服务端
       */
      /*
      data = JSON.parse(JSON.stringify(data).replace(/课程考核成绩/g, '1'));
      data = JSON.parse(JSON.stringify(data).replace(/平时综合表现/g, '2'));
      data = JSON.parse(JSON.stringify(data).replace(/毕业联考成绩/g, '3'));
      data = JSON.parse(JSON.stringify(data).replace(/思想政治/g, '21'));
      data = JSON.parse(JSON.stringify(data).replace(/军人气质/g, '22'));
      data = JSON.parse(JSON.stringify(data).replace(/领导管理起评分/g, '230'));
      data = JSON.parse(JSON.stringify(data).replace(/担任骨干/g, '231'));
      data = JSON.parse(JSON.stringify(data).replace(/组织活动/g, '232'));
      data = JSON.parse(JSON.stringify(data).replace(/扣分项/g, '233'));
      data = JSON.parse(JSON.stringify(data).replace(/领导管理/g, '23'));
      data = JSON.parse(JSON.stringify(data).replace(/团队意识起评分/g, '240'));
      data = JSON.parse(JSON.stringify(data).replace(/参加集体活动/g, '241'));
      data = JSON.parse(JSON.stringify(data).replace(/团队意识/g, '24'));
      data = JSON.parse(JSON.stringify(data).replace(/学科竞赛/g, '251'));
      data = JSON.parse(JSON.stringify(data).replace(/发表文章/g, '252'));
      data = JSON.parse(JSON.stringify(data).replace(/科技成果/g, '253'));
      data = JSON.parse(JSON.stringify(data).replace(/文体特长/g, '254'));
      data = JSON.parse(JSON.stringify(data).replace(/实践创新/g, '25'));
      data = JSON.parse(JSON.stringify(data).replace(/表彰奖励/g, '26'));
      data = JSON.parse(JSON.stringify(data).replace(/作风纪律/g, '27'));
       */
      postData = JSON.stringify(data)
      console.log(postData)
    },
    postDatatoServer() {
      this.$axios
        .post('permission', {
          postData
        })
        .then(response => {
          if (response !== null){
            console.log("success")
          }
        })
    }
  },
}
</script>

<style scoped>


</style>
