<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="试题库名称" prop="questionsLibraryName">
        <el-input
          v-model="queryParams.questionsLibraryName"
          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%" @selection-change="selectionChange">
      <!-- <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 prop="questionsLibraryName" label="试题库名称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span style="color: #409eff; cursor: pointer" @click="handleName(scope.row)">
            {{ scope.row.questionsLibraryName }}
          </span>
        </template>
      </el-table-column>
      <!-- 题目数量 -->
      <el-table-column prop="questionNum" label="题目数量" align="center" />
      <!-- 创建人 -->
      <el-table-column prop="createUserName" label="创建人" align="center" show-overflow-tooltip />
      <!-- 创建时间 -->
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <!-- 操作 -->
      <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="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="25%">
      <el-form :model="dialogForm" ref="dialogForm" size="small" label-width="80px" :rules="rules">
        <el-form-item label="题库名称" prop="questionsLibraryName">
          <el-input v-model="dialogForm.questionsLibraryName" placeholder="请输入题库名称" clearable v-no-whitespace />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleSave" :loading="isLoading" v-preventReClick="5000">确 定</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  testQuestLibraryList,
  addTestQuest,
  editTestQuest,
  delTestQuest,
} from '@/api/educationalTraining/resourceAdministration'

export default {
  data() {
    return {
      isLoading: false,
      // 是否显示搜索
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 题库名称
        questionsLibraryName: '',
        // 创建人
        createUserName: '',
        // 创建时间
        createTime: '',
        startDate: '',
        endDate: '',
      },
      // 列表数据
      tableData: [
        // {
        //   questionsLibraryName: '课件1',
        //   questionNum: '10',
        //   createUserName: '张三',
        //   createTime: '2021-01-01',
        // },
      ],
      // 弹框标题
      dialogTitle: '新建题库',
      // 弹框
      dialogVisible: false,
      // 弹框表单
      dialogForm: {
        questionsLibraryName: '',
      },
      // 弹框表单验证
      rules: {
        questionsLibraryName: [{ required: true, message: '请输入题库名称', trigger: 'blur' }],
      },
      isAdd: false, // 是否新增
      isEdit: false, // 是否编辑
      row: {}, // 当前行数据
    }
  },
  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()
    },
    // 获取列表
    async getList() {
      console.log('获取列表')
      const params = {
        questionsLibraryName: this.queryParams.questionsLibraryName,
        createUserName: this.queryParams.createUserName,
        startDate: this.queryParams.startDate,
        endDate: this.queryParams.endDate,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
      }
      const res = await testQuestLibraryList(params)
      console.log('🚀 ~ getList ~ data-列表:', res)
      this.tableData = res.rows
      this.total = res.total
    },
    // 重命名
    handleDialog(row) {
      console.log('重命名', row)
      this.dialogTitle = ''
      this.dialogForm.questionsLibraryName = ''
      if (row && row.questionsLibraryId) {
        this.row = row
        this.isAdd = false
        this.isEdit = true
        this.dialogTitle = '重命名'
        this.dialogForm.questionsLibraryName = row.questionsLibraryName
        console.log('🚀 ~ handleDialog ~ this.dialogForm.questionsLibraryName:', this.dialogForm.questionsLibraryName)
      } else {
        this.isAdd = true
        this.isEdit = false
        this.dialogTitle = '新建题库'
        this.dialogForm.questionsLibraryName = ''
      }
      this.dialogVisible = true
      setTimeout(() => {
        // 重置
        // this.$refs.dialogForm.resetFields()
      }, 200)
    },
    // 删除
    handleDelete(row) {
      console.log('删除', row)
      // 弹框提示
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 删除
          console.log('删除', row)
          const params = {
            questionsLibraryId: row.questionsLibraryId,
            type: 3,
          }
          delTestQuest(params).then(res => {
            // 刷新列表
            this.getList()
          })
        })
        .catch(() => {
          // 取消
        })
    },
    // 多选框选中数据
    selectionChange(sel) {
      console.log('多选框选中数据', sel)
    },
    // 保存修改
    handleSave() {
      console.log('保存修改')
      // 表单验证
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          this.isLoading = true
          // 保存修改
          console.log('保存修改', this.dialogForm)
          if (this.isAdd) {
            // 新增
            const params = {
              questionsLibraryName: this.dialogForm.questionsLibraryName,
              type: 1,
            }
            addTestQuest(params)
              .then(res => {
                console.log('🚀 ~ handleSave ~ res:', res)
                // 关闭弹框
                this.dialogVisible = false
                this.isLoading = false
                // 刷新列表
                this.getList()
              })
              .catch(() => {
                this.isLoading = false
              })
          } else if (this.isEdit) {
            // 编辑
            const params = {
              questionsLibraryId: this.row.questionsLibraryId,
              questionsLibraryName: this.dialogForm.questionsLibraryName,
              type: 2,
            }
            editTestQuest(params)
              .then(res => {
                console.log('🚀 ~ handleSave ~ res:', res)
                // 关闭弹框
                this.dialogVisible = false
                this.isLoading = false
                // 刷新列表
                this.getList()
              })
              .catch(() => {
                this.isLoading = false
              })
          }
        }
      })
    },
    // 点击名称
    handleName(row) {
      console.log('点击名称', row)
      this.$router.push({
        path: '/educationalTraining/resourceAdministration/testQuestionsStore/questionDetails',
        query: {
          questionsLibraryId: row.questionsLibraryId,
        },
      })
    },
  },
}
</script>

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