<!--题库管理-->
<template>
  <div class="app-container repo">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="form-box">
      <el-col :span="1.5" style="margin-right:20px;">
        <p class="add-button-one-120"
           @click="handleAdd"
           v-hasPermi="['exam:repo:add']"
        ><i class="el-icon-plus"></i>新增题库</p>
      </el-col>
      <el-form-item label="题库名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题库名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="学科类别" prop="code">
        <el-select v-model="queryParams.code" placeholder="请选择学科类别" clearable size="small">
          <el-option
            v-for="dict in subClassOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="inTime" style="margin-left:10px;">
        <el-date-picker clearable size="small"
                        v-model="queryParams.inTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="outTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.outTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="repoList" @selection-change="handleSelectionChange">
      <el-table-column label="题库名称" align="left" prop="title" />
      <el-table-column label="学科类别" align="left" prop="code" >
        <template slot-scope="scope">
          <span v-for="(item,index) in subClassOptions" :key="index" v-if="scope.row.code==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="单选题数量" align="left" prop="radioCount" />
      <el-table-column label="多选题数量" align="left" prop="multiCount" />
      <el-table-column label="判断题数量" align="left" prop="judgeCount" />
      <el-table-column label="创建时间" align="left" prop="createTime" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               style="margin-right:10px;"
               @click="handleUpdate(scope.row)"
               v-hasPermi="['exam:repo:edit']"
            ><i class="el-icon-edit"></i>编辑</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['exam:repo:remove']"
            ><i class="el-icon-delete"></i>删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题库名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入题库名称" />
        </el-form-item>
        <el-form-item label="学科类别" prop="code">
          <el-select v-model="form.code" placeholder="请选择">
            <el-option
              v-for="dict in subClassOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepo, getRepo, delRepo, addRepo, updateRepo } from "@/api/exam/repo";

export default {
  name: "Repo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题库表格数据
      repoList: [],
      // 学科类别字典
      subClassOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        title: null,
        radioCount: null,
        multiCount: null,
        judgeCount: null,
        userId: null,
        deptId: null,
        deptName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "题库类别不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "题库名称不能为空", trigger: "blur" },
          { required: true, message: "题库名称不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        radioCount: [
          { required: true, message: "单选数量不能为空", trigger: "blur" }
        ],
        multiCount: [
          { required: true, message: "多选数量不能为空", trigger: "blur" }
        ],
        judgeCount: [
          { required: true, message: "判断数量不能为空", trigger: "blur" }
        ],
        // remark: [
        //   { required: true, message: "题库备注不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList(),
    this.getDicts("subject_class").then(response => {
      this.subClassOptions = response.data;
    });
  },
  methods: {
    /** 查询题库列表 */
    getList() {
      this.loading = true;
      listRepo(this.queryParams).then(response => {
        this.repoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        title: null,
        radioCount: null,
        multiCount: null,
        judgeCount: null,
        remark: null,
        createTime: null,
        updateTime: null,
        userId: null,
        createBy: null,
        updateBy: null,
        deptId: null,
        deptName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepo(id).then(response => {
        this.form = response.data;
        this.form.code = this.form.code + ""
        this.open = true;
        this.title = "修改题库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除题库?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRepo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/repo/export', {
        ...this.queryParams
      }, `exam_repo.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .repo{
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .button-box{
      width:190px;
      display: flex;
    }
    .form-box{
    }
  }
</style>
