<!-- 评定列表 -->
<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-page-header content="评定列表" @back="goBack"></el-page-header>
    </div>
    <div ref="contentPanel" class="bbsl-table-content">
      <div ref="btnPanel" class="bbsl-btn-content">
        <el-button
          v-if="reportStatus === '0'"
          icon="el-icon-plus"
          type="success"
          @click="handleAdd"
          >新增课目
        </el-button>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="loading"
        :cell-style="{'text-align': 'center'}"
        :data="tableData"
        :header-cell-style="{'text-align': 'center'}"
        :max-height="tableMaxHeight"
        :style="{width: '100%'}"
        size="mini"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" type="index" width="55"></el-table-column>
        <el-table-column label="课目名称" min-width="150" prop="subjectName">
        </el-table-column>
        <el-table-column label="考生" min-width="100" prop="userName">
        </el-table-column>
        <el-table-column label="年份" min-width="120" prop="year">
        </el-table-column>
        <el-table-column label="是否评级" min-width="100">
          <template #default="scope">
            {{ scope.row.status == 0 ? '未评定' : '已评定' }}
          </template>
        </el-table-column>

        <el-table-column
          v-if="reportStatus === '0'"
          fixed="right"
          label="操作"
          width="200"
        >
          <template #default="scope">
            <!-- <el-button @click="handleClick(scope.row)" size="small">设置成绩</el-button> -->
            <el-dropdown
              split-button
              @click="handleClick(scope.row)"
              @command="
                command => {
                  deleteSubject(scope.row)
                }
              "
            >
              设置成绩
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="0"> 删除课目</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 设置成绩 -->
    <resultsDialog ref="results" @loadData="loadData"></resultsDialog>
    <!-- 新增 -->
    <subjectDialog ref="formModal" @loadData="loadData"></subjectDialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import ndbbMenu from '@/api/examination/ndbbMenu.js'
import resultsDialog from './resultsDialog.vue'
import subjectDialog from './subjectDialog.vue'

export default {
  name: 'ypMenu',
  components: {
    resultsDialog,
    subjectDialog
  },
  mixins: [TableMixin],
  data() {
    return {
      // 模拟数据
      tableData: [],
      showMore: false,
      ksztList: [],
      subjectsList: [],
      khlxList: [], //考核类型
      id: '',
      subjectId: '',
      subjectDialog: false,
      reportStatus: '' //年份状态
    }
  },
  created() {
    this.ksztList = dictUtils.getDictItemsFromCache('kszt')
    this.khlxList = dictUtils.getDictItemsFromCache('khlx')
  },
  mounted() {},
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    // 获取list
    loadData() {
      console.log(this.$route.params)
      if (
        this.$route.params.id !== {} &&
        typeof this.$route.params.id === 'string'
      ) {
        this.id = this.$route.params.id
        sessionStorage.setItem('id', this.id) //初次收到id，存起来
      } else {
        if (sessionStorage.getItem('id')) {
          this.id = sessionStorage.getItem('id') //不是第一次了，取出来
        } else {
          //如果实在是没有了，就直接去store里面取
          this.id = store.state.user.userInfo.id
        }
      }
      this.loading = true
      this.tableData = []
      ndbbMenu.evaluationDetail(this.id, res => {
        this.tableData = res.value.list
        this.reportStatus = res.value.reportStatus
        this.$set(this.tableData)
        this.loading = false
      })
    },

    //打开成绩
    handleClick(val) {
      this.$refs.results.add(this.id, val.subjectId)
    },
    //新增
    handleAdd() {
      if (this.tableData.length > 0) {
        let a = []
        this.tableData.forEach(t => {
          a.push(t.subjectId)
        })
        this.subjectId = a.toString()
      } else {
        this.subjectId = ''
      }
      this.$refs.formModal.add(this.id, this.subjectId)
    },
    deleteSubject(val) {
      val.id = this.id
      this.$confirm('此操作将永久删除该课目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ndbbMenu.delSubject(val, res => {
            if (res.state) {
              this.$message.success('删除成功')
              this.loadData(1)
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
.type-tag {
  margin: 0 2px;
}

::v-deep .el-col-1 {
  margin-left: -15px;
}
</style>
