<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="CURRICULUMID" label="课程ID" />
      <el-table-column prop="EXAMNAME" label="试卷名称" />
      <el-table-column prop="EXAMTIME" label="考试时间（开始时间）" />
      <el-table-column prop="ANSWERSHEETTIME" label="答卷时间（持续时间）" />
      <el-table-column prop="EXAMSCORE" label="试卷分数" />
      <el-table-column prop="CURRICULUMUSERS" label="适用人员" />
      <el-table-column prop="QUESTIONNUM" label="试题数量（10、20、50、100）" />
      <el-table-column prop="EXAMSTATE" label="试卷状态" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.COURSEPAPERS_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.COURSEPAPERS_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="课程ID" prop="CURRICULUMID">
          <el-input id="CURRICULUMID" ref="CURRICULUMID" v-model="form.CURRICULUMID" maxlength="255" placeholder="这里输入课程ID..." title="课程ID"/>
        </el-form-item>
        <el-form-item label="试卷名称" prop="EXAMNAME">
          <el-input id="EXAMNAME" ref="EXAMNAME" v-model="form.EXAMNAME" maxlength="255" placeholder="这里输入试卷名称..." title="试卷名称"/>
        </el-form-item>
        <el-form-item label="考试时间（开始时间）" prop="EXAMTIME">
          <el-input id="EXAMTIME" ref="EXAMTIME" v-model="form.EXAMTIME" maxlength="255" placeholder="这里输入考试时间（开始时间）..." title="考试时间（开始时间）"/>
        </el-form-item>
        <el-form-item label="答卷时间（持续时间）" prop="ANSWERSHEETTIME">
          <el-input id="ANSWERSHEETTIME" ref="ANSWERSHEETTIME" v-model="form.ANSWERSHEETTIME" maxlength="255" placeholder="这里输入答卷时间（持续时间）..." title="答卷时间（持续时间）"/>
        </el-form-item>
        <el-form-item label="试卷分数" prop="EXAMSCORE">
          <el-input id="EXAMSCORE" ref="EXAMSCORE" v-model="form.EXAMSCORE" maxlength="255" placeholder="这里输入试卷分数..." title="试卷分数"/>
        </el-form-item>
        <el-form-item label="适用人员" prop="CURRICULUMUSERS">
          <el-input id="CURRICULUMUSERS" ref="CURRICULUMUSERS" v-model="form.CURRICULUMUSERS" maxlength="255" placeholder="这里输入适用人员..." title="适用人员"/>
        </el-form-item>
        <el-form-item label="试题数量（10、20、50、100）" prop="QUESTIONNUM">
          <el-input id="QUESTIONNUM" ref="QUESTIONNUM" v-model="form.QUESTIONNUM" maxlength="255" placeholder="这里输入试题数量（10、20、50、100）..." title="试题数量（10、20、50、100）"/>
        </el-form-item>
        <el-form-item label="试卷状态" prop="EXAMSTATE">
          <el-input id="EXAMSTATE" ref="EXAMSTATE" v-model="form.EXAMSTATE" maxlength="255" placeholder="这里输入试卷状态..." title="试卷状态"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      config: config,
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        CURRICULUMID: '', // 课程ID
        EXAMNAME: '', // 试卷名称
        EXAMTIME: '', // 考试时间（开始时间）
        ANSWERSHEETTIME: '', // 答卷时间（持续时间）
        EXAMSCORE: '', // 试卷分数
        CURRICULUMUSERS: '', // 适用人员
        QUESTIONNUM: '', // 试题数量（10、20、50、100）
        EXAMSTATE: ''// 试卷状态
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        CURRICULUMID: [{ required: true, message: '课程ID不能为空', trigger: 'blur' }],
        EXAMNAME: [{ required: true, message: '试卷名称不能为空', trigger: 'blur' }],
        EXAMTIME: [{ required: true, message: '考试时间（开始时间）不能为空', trigger: 'blur' }],
        ANSWERSHEETTIME: [{ required: true, message: '答卷时间（持续时间）不能为空', trigger: 'blur' }],
        EXAMSCORE: [{ required: true, message: '试卷分数不能为空', trigger: 'blur' }],
        CURRICULUMUSERS: [{ required: true, message: '适用人员不能为空', trigger: 'blur' }],
        QUESTIONNUM: [{ required: true, message: '试题数量（10、20、50、100）不能为空', trigger: 'blur' }],
        EXAMSTATE: [{ required: true, message: '试卷状态不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.COURSEPAPERS_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/coursepapers/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      requestFN(
        '/coursepapers/goEdit',
        {
          COURSEPAPERS_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/coursepapers/' + this.dialogType,
            this.form
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/coursepapers/delete',
          {
            COURSEPAPERS_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.COURSEPAPERS_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/coursepapers/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'coursepapers:add,coursepapers:del,coursepapers:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.coursepapersfhadminadd		// 新增权限
        this.del = data.coursepapersfhadmindel		// 删除权限
        this.edit = data.coursepapersfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
    },
    resetForm() {
      this.form = {
        CURRICULUMID: '', // 课程ID
        EXAMNAME: '', // 试卷名称
        EXAMTIME: '', // 考试时间（开始时间）
        ANSWERSHEETTIME: '', // 答卷时间（持续时间）
        EXAMSCORE: '', // 试卷分数
        CURRICULUMUSERS: '', // 适用人员
        QUESTIONNUM: '', // 试题数量（10、20、50、100）
        EXAMSTATE: ''// 试卷状态
      }
    }
  }
}

</script>
