<template>
  <div class="app-container">
    <el-page-header v-show="isTitle" @back="isTitle = false" content="题目">
    </el-page-header>
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="create('add')"
      class="margin-bottom"
      >添加题{{ isTitle ? "目" : "库" }}</el-button
    >
    <!--搜索区-->
    <div class="float">
      <template v-if="!isTitle">
        <el-input
          v-model="page.data.name"
          placeholder="请输入题库"
          class="margin-left input m-b-10"
        ></el-input>
        <el-date-picker
          v-model="page.data.startTime"
          type="datetime"
          placeholder="选择起始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="margin-left m-b-10"
        >
        </el-date-picker>
        <el-date-picker
          v-model="page.data.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="margin-left m-b-10"
        >
        </el-date-picker>
      </template>
      <template v-if="isTitle">
        <el-input
          v-model="page1.data.description"
          placeholder="请输入题目描述"
          class="margin-left m-b-10 input"
        ></el-input>
        <el-select
          filterable
          clearable
          v-model="page1.data.type"
          placeholder="请选择题目类型"
          class="margin-left input"
        >
          <el-option
            v-for="item in type_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-date-picker
          v-model="page1.data.startTime"
          type="datetime"
          placeholder="选择起始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="margin-left m-b-10"
        >
        </el-date-picker>
        <el-date-picker
          v-model="page1.data.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="margin-left m-b-10"
        >
        </el-date-picker>
      </template>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
        class="margin-left m-b-10"
        >搜索</el-button
      >
    </div>
    <!-- 题库页面 -->
    <div v-show="!isTitle">
      <el-table
        class="table"
        ref="table_form"
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" align="center" width="80px">
          <template slot-scope="scope">
            {{ indexMethod(scope.$index) }}
          </template>
        </el-table-column>
        <el-table-column label="题库名" align="center">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="题目总数" align="center">
          <template slot-scope="scope">
            {{ scope.row.topicNum }}
          </template>
        </el-table-column>
        <el-table-column label="单选题数" align="center">
          <template slot-scope="scope">
            {{ scope.row.singleTopicNum }}
          </template>
        </el-table-column>
        <el-table-column label="多选题数" align="center">
          <template slot-scope="scope">
            {{ scope.row.multipleTopicNum }}
          </template>
        </el-table-column>
        <el-table-column label="判断题数" align="center">
          <template slot-scope="scope">
            {{ scope.row.judgeTopicNum }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="操作区" width="250">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="small"
              @click="deleteFun(scope.row.stTopicBankId)"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="create('update', scope.row)"
              >编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="goTitle(scope.row.stTopicBankId)"
              >题目</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 题目页面 -->
    <div v-show="isTitle">
      <el-table
        class="table"
        ref="table_form"
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" align="center" width="80px">
          <template slot-scope="scope">
            {{ indexMethod(scope.$index) }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="题库名" align="center">
          <template slot-scope="scope">
            {{ scope.row.poolName }}
          </template>
        </el-table-column> -->
        <el-table-column label="题目" align="center">
          <template slot-scope="scope">
            {{ scope.row.description }}
          </template>
        </el-table-column>
        <el-table-column label="答案选项" align="left">
          <template slot-scope="scope">
            <el-tag
              effect="plain"
              v-for="(item, index) in scope.row.judgmentList"
              :key="index"
              class="m-r-5"
            >
              {{ item }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="正确答案" align="left">
          <template slot-scope="scope">
            <el-tag
              effect="plain"
              v-for="(item, index) in scope.row.answerList"
              :key="index"
              class="m-r-5"
            >
              {{ item }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="题目类型" align="center">
          <template slot-scope="scope">
            <el-tag effect="plain" v-if="scope.row.type === 2"
              >多选</el-tag
            >
            <el-tag
              effect="plain"
              v-if="scope.row.type === 1"
              type="success"
              >单选</el-tag
            >
            <el-tag
              effect="plain"
              v-if="scope.row.type === 3"
              type="info"
              >判断</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="操作区" width="200">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="small"
              @click="deleteFun(scope.row.stTopicId)"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="create('update', scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      :title="dialogTitle[dialogType]"
      :visible.sync="dialogVisible"
      width="700px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        label-position="left"
      >
        <!-- 题目 -->
        <div v-if="isTitle">
          <el-form-item label="题目" prop="description">
            <el-input
              placeholder="请填写题目"
              v-model.trim="form.description"
              clearable
            />
          </el-form-item>
          <el-form-item label="题目类型" prop="content">
            <el-radio v-model="form.type" :label="1">单选</el-radio>
            <el-radio v-model="form.type" :label="2">多选</el-radio>
            <el-radio v-model="form.type" :label="3">判断</el-radio>
          </el-form-item>
          <div>
            <el-form-item label="答案选项" prop="judgmentList">
              <el-tag
                class="m-r-10"
                :key="tag"
                v-for="tag in form.judgmentList"
                v-model="form.judgmentList"
                closable
                :disable-transitions="false"
                @close="handleClose(tag)"
              >
                {{ tag }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="inputVisible"
                v-model.trim="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter="handleInputConfirm"
                @blur="handleInputConfirm"
              >
              </el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput"
                >+选项</el-button
              >
            </el-form-item>
            <el-form-item label="正确答案" prop="answerList">
              <el-select
                style="width: 100%"
                size="medium"
                v-model="form.answerList"
                multiple
                :multiple-limit="form.type === 2 ? 0 : 1"
                placeholder="请选择正确答案"
              >
                <el-option
                  v-for="item in form.judgmentList"
                  :key="item"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
        <!-- 题库 -->
        <el-form-item label="题库名称" prop="name" v-if="!isTitle">
          <el-input
            placeholder="请输入题库名称"
            v-model.trim="form.name"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  topicBankEdit,
  topicBankGetPageList,
  topicBankDelete,
  topicEdit,
  topicgetPageList,
  topicDelete,
} from "@/api/examination";
export default {
  components: { Pagination },
  filters: {},
  computed: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [{}],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          name: null,
          startTime: null,
          endTime: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      page1: {
        data: {
          description: null,
          stTopicBankId: null,
          type: null,
          startTime: null,
          endTime: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      type_list: [
        { id: 1, name: "单选" },
        { id: 2, name: "多选" },
        { id: 3, name: "判断" },
      ],
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: "添加",
        update: "编辑",
      },
      dialogType: "add",
      form_copy: null,
      // 添加角色参数
      form: {
        name: null,
        description: null,
        type: 1,
        answerList: [],
        judgmentList: [],
        content: "",
      },
      // 验证规则
      rules: {
        name: [{ required: true, message: "请输入", trigger: "change" }],
        description: [
          { required: true, message: "请输入", trigger: "change" },
        ],
        judgmentList: [
          { required: true, message: "请输入", trigger: "change" },
        ],
        answerList: [{ required: true, message: "请输入", trigger: "change" }],
      },
      isTitle: false, //true：题目 false：题库
      inputVisible: false,
      inputValue: "",
    };
  },
  created() {
    this.fetchData();
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  watch: {
    isTitle(n) {
      this.fetchData();
    },
  },
  methods: {
    // 查询权限
    // checkTypeList,
    // 获取列表数据
    async fetchData() {
      this.listLoading = true;
      let res;
      if (this.isTitle) {
        // 题目
        res = await topicgetPageList(this.page1);
      } else {
        res = await topicBankGetPageList(this.page);
      }
      this.list = res.data.records;
      this.total = res.data.total;
      this.listLoading = false;
    },
    // 打开题目
    goTitle(id) {
      this.isTitle = true;
      this.page1.data.stTopicBankId = id;
    },
    // 打开添加角色对话框
    create(type, row) {
      if (type === "add") {
        this.form = JSON.parse(JSON.stringify(this.form_copy));
      } else {
        this.form = Object.assign(this.form, row);
      }
      this.dialogType = type;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let res;
          if (this.isTitle) {
            // 题目
            this.form.stTopicBankId = this.page1.data.stTopicBankId;
            res = await topicEdit(this.form);
          } else {
            res = await topicBankEdit(this.form);
          }
          if (res.code == 200) {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          }
        }
      });
    },
    // 删除角色
    deleteFun(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(async () => {
        let res;
        if (this.isTitle) {
          // 题目
          res = await topicDelete(id);
        } else {
          res = await topicBankDelete(id);
        }
        if (res.code == 200) {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        }
      });
    },
    handleClose(tag) {
      this.form.judgmentList.splice(this.form.judgmentList.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.judgmentList.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>
<style scoped lang="scss">
.float {
  float: right;
  // margin-bottom: 20px;
}
h3 {
  font-weight: 400;
  margin-bottom: 0;
}
.margin-left {
  margin-left: 10px;
}
.margin-bottom {
  margin-bottom: 20px;
}
.m-r-10 {
  margin-right: 10px;
}
.expand-css {
  color: #97a8be !important;
  margin-left: 20px;
  font-weight: 400;
  font-size: 15px;
}
.flex1 {
  display: flex;
  justify-content: space-between;
  font-weight: 600;
  margin-bottom: 12px;
}
.line-height {
  line-height: 30px;
}
.el-page-header {
  margin-bottom: 15px;
  color: #409eff;
  ::v-deep .el-page-header__content {
    color: #409eff;
  }
}
.el-tag + .el-tag {
  margin-right: 10px;
}

.button-new-tag {
  margin-right: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-right: 10px;
  vertical-align: bottom;
}
.m-r-5{
  margin-right: 5px;
  margin-bottom: 5px;
}
</style>
