
<template>
  <div class="questionBank app-container">
    <div class="search">
      <div>

      <el-date-picker
        v-model="date"
        size="small"
        class="item-width-350"
        value-format="yyyy-MM-dd hh:mm:ss"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>
       <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>

      <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="试卷名称"
        prop="paperName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="questionsNum"
        align="center"
        label="试题数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="创建时间"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="createTime"
        align="center"
        label="问卷状态"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span :class="[scope.row.status == 1 ? 'red' : 'green']">{{
            scope.row.status == 1 ? "未发布" : "已发布"
          }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleState(scope.row.id, scope.row.status == 1 ? 2 : 1)"
            >{{ scope.row.status == 1 ? "立即发布" : "取消发布" }}</el-button
          >
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="editQuestions(scope.row.paperName,scope.row.id)">编辑题库</el-button>
          <el-button
            type="text"
            class="red"
            size="small"
            @click="handleDel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" width="550px" :visible.sync="dialogFormVisible">
        <el-form label-width="130px" :model="setQuery"
        ref="setQuery"
        :rules="rules">
            <el-form-item label="试卷名称:"  prop="paperName">
                <el-input
                v-model="setQuery.paperName"
                class="item-width-300"
                ></el-input>
            </el-form-item>
            <el-form-item label="题目数量:"  prop="questionsNum">
                <el-input
                type="number"
                v-model.number="setQuery.questionsNum"
                class="item-width-300"
                ></el-input>
            </el-form-item>
        </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="save">确 定</el-button>
    </div>
    </el-dialog>


  </div>
</template>
  
<script>
import { debounce } from "@/utils/index";
import { paperList, publish, delPaper ,addOrUpdatePaper} from "@/api/study";
export default {
  name: "questionBank",
  data() {
    return {
      date: "", //时间
      tableData: [],
      loading: false,
      btnLoading: false,
      title:'',
      dialogFormVisible: false,
      query: {
        currentPage: 1,
        pageSize: 10,
        startDateTime: "",
        endDateTime: "",
      },
      total: 0,
      setQuery:{
        "id": 0,
        "paperName": "",
        "paperState": 0,
        "paperType": 0,
        "questionsNum": 0
      },
      rules:{
         paperName: [{ required: true, message: "请输入试卷名称", trigger: "blur" }],
         questionsNum: [
            { required: true, message: "请输入题目数量", trigger: "blur" },
            { type:'number', min:1, message: "题目数量最小为1", trigger: "blur" }
         ],
      },
    };
  },
  watch: {
    // query: {
    //   handler: debounce(function (val) {
    //     this.paperList();
    //   }),
    //   deep: true,
    // },
    date(val) {
      if (val) {
        this.query.startDateTime = val[0];
        this.query.endDateTime = val[1];
      } else {
        this.query.startDateTime = "";
        this.query.endDateTime = "";
      }
    },
  },

  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.paperList();

    },
    handleAdd() {
        this.title = '新增题库'
        this.dialogFormVisible = true
        this.setQuery = {
            "paperName": "",
            "paperState": 1,
            "paperType": 1,
            "questionsNum": 0
        }
    },
    handleEdit(row) {
        this.title = '编辑题库'
        this.dialogFormVisible = true
        this.setQuery = {
            "id":row.id,
            "paperName": row.paperName,
            "paperState": row.paperState,
            "paperType": row.paperType,
            "questionsNum": row.questionsNum
        }
    },
    editQuestions(title,id){
        sessionStorage.setItem('questionsTitle',title)
        this.$router.push({
            path:'/studyManage/questionBankManage/editQuestions',
            query:{
                id
            }
        })
    },
    handleState(id, status) {
      let msg;
      if (status == 2) {
        msg = "发布";
      } else {
        msg = "取消发布";
      }
      this.$confirm(`此操作将${msg}该数据, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        publish({ id, status }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.paperList();
          }
        });
      });
    },
    // 点击删除
    handleDel(id) {
      this.$confirm(`此操作将删除该数据, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delPaper({ id }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.search();
          }
        });
      });
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.paperList();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.paperList();
    },

    search() {
      this.query.currentPage = 1;
      this.paperList();
    },
    paperList() {
      this.loading = true;
      paperList(this.query).then((res) => {
        this.loading = false;
        if (res.state == "Success") {
          this.tableData = res.content.records;
          this.total = res.content.total;
        }
      });
    },
    save(){
        this.$refs.setQuery.validate((v) => {
        if (v) {
          this.btnLoading = true
          addOrUpdatePaper(this.setQuery).then(res=>{
            this.btnLoading = false
            if(res.state == 'Success'){
                this.dialogFormVisible = false
              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
                this.query.currentPage = 1
                this.paperList();
            }
          })
        }
      });
    }
  },
  created() {
    this.paperList();
  },
};
</script>
  
<style lang="scss" scoped>
.questionBank {
  .search {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
  