<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="科目ID" prop="subjectId">
        <el-input v-model="queryParams.subjectId" placeholder="请输入科目ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="试卷名称" prop="paperName">
        <el-input v-model="queryParams.paperName" placeholder="请输入试卷名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="试卷年份" prop="year">
        <el-input v-model="queryParams.year" placeholder="请输入试卷年份" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="试卷题数" prop="number">
        <el-input v-model="queryParams.number" placeholder="请输入试卷题数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="试卷总分" prop="fenshu">
        <el-input v-model="queryParams.fenshu" placeholder="请输入试卷总分" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否精品" prop="paperSource">
        <el-input v-model="queryParams.paperSource" placeholder="请输入0 默认 1 精品试卷" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否有在线卷" prop="online">
        <el-input v-model="queryParams.online" placeholder="请输入是否有在线卷" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="唯一标识" prop="tiid">
        <el-input v-model="queryParams.tiid" placeholder="请输入唯一标识" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="适用地区" prop="locations">
        <el-input v-model="queryParams.locations" placeholder="请输入适用地区" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="YYYY-MM-DD" class="w-[170px]!"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['learn:paper_real:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['learn:paper_real:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['learn:paper_real:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['learn:paper_real:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paper_realList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="科目ID" align="center" prop="subjectId" fixed="left" />
      <el-table-column label="试卷名称" align="center" prop="paperName" />
      <el-table-column label="试卷年份" align="center" prop="year" />
      <el-table-column label="试卷分类" align="center" prop="paperType">
        <template #default="scope">
          <span>{{ paperTypeOptions.find(item => item.value === scope.row.paperType)?.label }}</span>
        </template>
      </el-table-column>
      <el-table-column label="试卷题数" align="center" prop="number" />
      <el-table-column label="试卷总分" align="center" prop="fenshu" />
      <el-table-column label="是否精品" align="center" prop="paperSource" />
      <el-table-column label="是否有在线卷" align="center" prop="online" min-width="120" />
      <el-table-column label="唯一标识" align="center" prop="tiid" />
      <el-table-column label="下载路径" align="center" prop="osspath" />
      <el-table-column label="原始文件名" align="center" prop="filename" min-width="100" />
      <el-table-column label="适用地区" align="center" prop="locations" />
      <el-table-column label="适用考生" align="center" prop="examineeType" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['learn:paper_real:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['learn:paper_real:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改真题卷对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="paper_realRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="科目ID" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入科目ID" />
        </el-form-item>
        <el-form-item label="试卷名称" prop="paperName">
          <el-input v-model="form.paperName" placeholder="请输入试卷名称" />
        </el-form-item>
        <el-form-item label="试卷年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入试卷年份" />
        </el-form-item>
        <el-form-item label="试卷题数" prop="number">
          <el-input v-model="form.number" placeholder="请输入试卷题数" />
        </el-form-item>
        <el-form-item label="试卷总分" prop="fenshu">
          <el-input v-model="form.fenshu" placeholder="请输入试卷总分" />
        </el-form-item>
        <el-form-item label="0 默认 1 精品试卷" prop="paperSource">
          <el-input v-model="form.paperSource" placeholder="请输入0 默认 1 精品试卷" />
        </el-form-item>
        <el-form-item label="是否有在线卷" prop="online">
          <el-input v-model="form.online" placeholder="请输入是否有在线卷" />
        </el-form-item>
        <el-form-item label="唯一标识" prop="tiid">
          <el-input v-model="form.tiid" placeholder="请输入唯一标识" />
        </el-form-item>
        <el-form-item label="下载路径" prop="osspath">
          <el-input v-model="form.osspath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="原始文件名" prop="filename">
          <el-input v-model="form.filename" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="适用地区" prop="locations">
          <el-input v-model="form.locations" placeholder="请输入适用地区" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Paper_real">
import { listPaper_real, getPaper_real, delPaper_real, addPaper_real, updatePaper_real } from "@/api/learn/paper_real"

const { proxy } = getCurrentInstance()
const paperTypeOptions = ref([
  { label: "历年真题", value: 1 },
  { label: "模拟卷", value: 2 },
  { label: "必刷题", value: 3 },
])
const paper_realList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    subjectId: null,
    paperName: null,
    year: null,
    paperType: null,
    number: null,
    fenshu: null,
    paperSource: null,
    online: null,
    tiid: null,
    osspath: null,
    filename: null,
    locations: null,
    examineeType: null,
    createTime: null
  },
  rules: {
    subjectId: [
      { required: true, message: "科目ID不能为空", trigger: "blur" }
    ],
    paperName: [
      { required: true, message: "试卷名称不能为空", trigger: "blur" }
    ],
    year: [
      { required: true, message: "试卷年份不能为空", trigger: "blur" }
    ],
    fenshu: [
      { required: true, message: "试卷总分不能为空", trigger: "blur" }
    ],
    paperSource: [
      { required: true, message: "0 默认 1 精品试卷不能为空", trigger: "blur" }
    ],
    online: [
      { required: true, message: "是否有在线卷不能为空", trigger: "blur" }
    ],
    locations: [
      { required: true, message: "适用地区不能为空", trigger: "blur" }
    ],
    examineeType: [
      { required: true, message: "适用考生不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询真题卷列表 */
function getList() {
  loading.value = true
  listPaper_real(queryParams.value).then(response => {
    paper_realList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    subjectId: null,
    paperName: null,
    year: null,
    paperType: null,
    number: null,
    fenshu: null,
    paperSource: null,
    online: null,
    tiid: null,
    osspath: null,
    filename: null,
    locations: null,
    examineeType: null,
    createTime: null
  }
  proxy.resetForm("paper_realRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加真题卷"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPaper_real(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改真题卷"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["paper_realRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePaper_real(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPaper_real(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除真题卷编号为"' + _ids + '"的数据项？').then(function () {
    return delPaper_real(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('learn/paper_real/export', {
    ...queryParams.value
  }, `paper_real_${new Date().getTime()}.xlsx`)
}

getList()
</script>
