<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="练习题库名称" prop="examTemplateName">
        <el-input
          v-model="queryParams.examTemplateName"
          placeholder="请输入练习题库名称"
          clearable v-no-whitespace
          @keyup.enter.native="handleSearch"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUserName">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="请输入创建人"
          clearable v-no-whitespace
          @keyup.enter.native="handleSearch"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
        />
      </el-form-item>
      <!-- 查询, 搜索 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleDialog">新建题库</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table :data="tableData" fit highlight-current-row style="width: 100%">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="item in tableHeader"
        :key="item.prop"
        :prop="item.prop"
        :label="item.label"
        align="center"
        show-overflow-tooltip
      >
        <!-- 题目数量 v-slot -->
        <template v-slot="scope" v-if="item.prop == 'examTemplateName'">
          <span
            @click="handleEdit(scope.row)"
            style="cursor: pointer; color: #409eff"
          >
            {{ scope.row.examTemplateName }}
          </span>
        </template>
        <template v-slot="scope" v-else-if="item.prop === 'questionNum'">{{ scope.row.questionNum || 0 }}道</template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDialog(scope.row)" icon="el-icon-edit">重命名</el-button>
          <el-button type="text" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit-outline">编辑</el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            style="color: #e47470"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <el-form :model="dialogForm" ref="dialogForm" size="small" label-width="110px" :rules="rules">
        <el-form-item label="练习题库名称" prop="examTemplateName">
          <el-input v-model="dialogForm.examTemplateName" placeholder="请输入题库名称" clearable maxlength="50" v-no-whitespace />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleSave" v-preventReClick="5000">确 定</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <TestQuestionsDialog :isShow="false" ref="testQuestionsDialog" @getList="getList" />
  </div>
</template>

<script>
import TestQuestionsDialog from '../../components/TestQuestionsDialog'
import { practiceTemplateList, addExam, renameExam, delExam } from '@/api/educationalTraining/resourceAdministration'

export default {
  components: { TestQuestionsDialog },
  data() {
    return {
      // 是否显示搜索
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateType: 2,
        // 练习题库名称
        examTemplateName: '',
        // 创建人
        createUserName: '',
        // 创建时间
        createTime: [],
        startDate: '', // 开始时间
        endDate: '', // 结束时间
      },
      // 表格数据 - 表头
      tableHeader: [
        { prop: 'examTemplateName', label: '练习题库名称' },
        { prop: 'questionNum', label: '题目数量' },
        { prop: 'createUserName', label: '创建人' },
        { prop: 'createTime', label: '创建时间' },
      ],
      // 列表数据
      tableData: [],
      // 弹框标题
      dialogTitle: '',
      // 弹框
      dialogVisible: false,
      // 弹框表单
      dialogForm: {
        examTemplateName: '',
      },
      // 弹框表单验证
      rules: {
        examTemplateName: [{ required: true, message: '请输入试卷名称', trigger: 'blur' }],
      },
      isEdit: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 搜索
    handleSearch() {
      console.log('搜索', this.queryParams)
      if (this.queryParams.createTime) {
        this.queryParams.startDate = this.queryParams.createTime[0]
        this.queryParams.endDate = this.queryParams.createTime[1]
      } else {
        this.queryParams.startDate = ''
        this.queryParams.endDate = ''
      }
      this.getList()
    },
    // 重置
    handleReset() {
      console.log('重置')
      // 重置查询参数 resetFields
      this.$refs.queryForm.resetFields()
      this.queryParams.startDate = ''
      this.queryParams.endDate = ''
      this.getList()
    },
    // 获取列表
    getList() {
      console.log('获取列表')
      const params = {
        templateType: this.queryParams.templateType,
        examTemplateName: this.queryParams.examTemplateName,
        createUserName: this.queryParams.createUserName,
        startDate: this.queryParams.startDate,
        endDate: this.queryParams.endDate,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
      }
      practiceTemplateList(params).then(res => {
        console.log('🚀 ~ practiceTemplateList ~ res-列表:', res)
        this.tableData = res.rows
        this.total = res.total
      })
    },
    // 重命名
    async handleDialog(row) {
      console.log('重命名', row)
      // 打开弹框 修改课件名
      await (this.dialogVisible = true)
      // 重置
      this.$refs.dialogForm.resetFields()
      if (row.examTemplateId) {
        this.isEdit = true
        this.dialogTitle = '重命名'
        this.dialogForm.examTemplateName = row.examTemplateName
        this.dialogForm.examTemplateId = row.examTemplateId
        console.log('🚀 ~ handleDialog ~ this.dialogForm.examTemplateName:', this.dialogForm.examTemplateName)
      } else {
        this.isEdit = false
        this.dialogTitle = '新建题库'
        this.dialogForm.examTemplateName = ''
      }
    },
    // 编辑
    handleEdit(row) {
      console.log('编辑', row)
      this.$refs.testQuestionsDialog.show(row)
    },
    // 删除
    handleDelete(row) {
      console.log('删除', row)
      // 弹框提示
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 删除
          console.log('删除', row)
          if (row.examTemplateId) {
            const params = {
              examTemplateId: row.examTemplateId,
              type: 3,
            }
            delExam(params).then(res => {
              console.log('🚀 ~ delExam ~ res:', res)
              // 刷新列表
              this.getList()
              this.$message.success('操作成功')
            })
          }
        })
        .catch(() => {
          // 取消
        })
    },
    // 保存修改
    handleSave() {
      console.log('保存修改')
      // 表单验证
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          // 保存修改
          console.log('保存修改', this.dialogForm)
          if (this.isEdit) {
            // 重命名
            const params = {
              examTemplateId: this.dialogForm.examTemplateId,
              examTemplateName: this.dialogForm.examTemplateName,
              templateType: 2,
              type: 2,
              isSelfBuild: 0,
            }
            renameExam(params).then(res => {
              console.log('🚀 ~ renameExam ~ res:', res)
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.getList()
                // 关闭弹框
                this.dialogVisible = false
              }
            })
          } else {
            // 新建题库
            const params = {
              examTemplateName: this.dialogForm.examTemplateName,
              templateType: 2,
              type: 1,
              isSelfBuild: 0,
            }
            addExam(params).then(res => {
              console.log('🚀 ~ addExam ~ res:', res)
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.getList()
                // 关闭弹框
                this.dialogVisible = false
              }
            })
          }
          // // 关闭弹框
          // this.dialogVisible = false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
