<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位主键" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入单位主键"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目主键" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目主键"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工种类型" prop="craftType">
        <el-select v-model="queryParams.craftType" placeholder="请选择工种类型" clearable>
          <el-option
            v-for="dict in pro_craft_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工种岗位" prop="craftPost">
        <el-select v-model="queryParams.craftPost" placeholder="请选择工种岗位" clearable>
          <el-option
            v-for="dict in pro_craft_post"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题难度" prop="questionDiff">
        <el-select v-model="queryParams.questionDiff" placeholder="请选择问题难度" clearable>
          <el-option
            v-for="dict in edu_question_diff"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" prop="questionType">
        <el-select v-model="queryParams.questionType" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in edu_question_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题描述" prop="questionDesc">
        <el-input
          v-model="queryParams.questionDesc"
          placeholder="请输入问题描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题答案" prop="questionAnswer">
        <el-input
          v-model="queryParams.questionAnswer"
          placeholder="请输入问题答案"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDel">
        <el-select v-model="queryParams.isDel" placeholder="请选择是否删除" clearable>
          <el-option
            v-for="dict in sys_is_del"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['manage:busExamQuestion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:busExamQuestion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:busExamQuestion:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:busExamQuestion:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="busExamQuestionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="单位主键" align="center" prop="deptId" />
      <el-table-column label="项目主键" align="center" prop="projectId" />
      <el-table-column label="工种类型" align="center" prop="craftType">
        <template #default="scope">
          <dict-tag :options="pro_craft_type" :value="scope.row.craftType"/>
        </template>
      </el-table-column>
      <el-table-column label="工种岗位" align="center" prop="craftPost">
        <template #default="scope">
          <dict-tag :options="pro_craft_post" :value="scope.row.craftPost"/>
        </template>
      </el-table-column>
      <el-table-column label="问题难度" align="center" prop="questionDiff">
        <template #default="scope">
          <dict-tag :options="edu_question_diff" :value="scope.row.questionDiff"/>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="questionType">
        <template #default="scope">
          <dict-tag :options="edu_question_type" :value="scope.row.questionType"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="questionDesc" />
      <el-table-column label="问题答案" align="center" prop="questionAnswer" />
      <el-table-column label="是否删除" align="center" prop="isDel">
        <template #default="scope">
          <dict-tag :options="sys_is_del" :value="scope.row.isDel"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:busExamQuestion:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:busExamQuestion: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="busExamQuestionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位主键" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入单位主键" />
        </el-form-item>
        <el-form-item label="项目主键" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目主键" />
        </el-form-item>
        <el-form-item label="工种类型" prop="craftType">
          <el-select v-model="form.craftType" placeholder="请选择工种类型">
            <el-option
              v-for="dict in pro_craft_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工种岗位" prop="craftPost">
          <el-select v-model="form.craftPost" placeholder="请选择工种岗位">
            <el-option
              v-for="dict in pro_craft_post"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题难度" prop="questionDiff">
          <el-select v-model="form.questionDiff" placeholder="请选择问题难度">
            <el-option
              v-for="dict in edu_question_diff"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题类型" prop="questionType">
          <el-select v-model="form.questionType" placeholder="请选择问题类型">
            <el-option
              v-for="dict in edu_question_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="questionDesc">
          <el-input v-model="form.questionDesc" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="问题答案" prop="questionAnswer">
          <el-input v-model="form.questionAnswer" placeholder="请输入问题答案" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <el-select v-model="form.isDel" placeholder="请选择是否删除">
            <el-option
              v-for="dict in sys_is_del"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">题库问题信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddBusExamQuestionResult">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteBusExamQuestionResult">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="busExamQuestionResultList" :row-class-name="rowBusExamQuestionResultIndex" @selection-change="handleBusExamQuestionResultSelectionChange" ref="busExamQuestionResult">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="选项" prop="opt" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.opt" placeholder="请输入选项" />
            </template>
          </el-table-column>
          <el-table-column label="问题选项" prop="result" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.result" placeholder="请输入问题选项" />
            </template>
          </el-table-column>
          <el-table-column label="是否删除" prop="isDel" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.isDel" placeholder="请选择是否删除">
                <el-option
                  v-for="dict in sys_is_del"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </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="BusExamQuestion">
import { listBusExamQuestion, getBusExamQuestion, delBusExamQuestion, addBusExamQuestion, updateBusExamQuestion } from "@/api/manage/busExamQuestion";

const { proxy } = getCurrentInstance();
const { pro_craft_type, edu_question_type, sys_is_del, pro_craft_post, edu_question_diff } = proxy.useDict('pro_craft_type', 'edu_question_type', 'sys_is_del', 'pro_craft_post', 'edu_question_diff');

const busExamQuestionList = ref([]);
const busExamQuestionResultList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedBusExamQuestionResult = 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,
    deptId: null,
    projectId: null,
    craftType: null,
    craftPost: null,
    questionDiff: null,
    questionType: null,
    questionDesc: null,
    questionAnswer: null,
    isDel: null,
  },
  rules: {
  }
});

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

/** 查询基础题库列表 */
function getList() {
  loading.value = true;
  listBusExamQuestion(queryParams.value).then(response => {
    busExamQuestionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    projectId: null,
    craftType: null,
    craftPost: null,
    questionDiff: null,
    questionType: null,
    questionDesc: null,
    questionAnswer: null,
    isDel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  busExamQuestionResultList.value = [];
  proxy.resetForm("busExamQuestionRef");
}

/** 搜索按钮操作 */
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
  getBusExamQuestion(_id).then(response => {
    form.value = response.data;
    busExamQuestionResultList.value = response.data.busExamQuestionResultList;
    open.value = true;
    title.value = "修改基础题库";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["busExamQuestionRef"].validate(valid => {
    if (valid) {
      form.value.busExamQuestionResultList = busExamQuestionResultList.value;
      if (form.value.id != null) {
        updateBusExamQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBusExamQuestion(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 delBusExamQuestion(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 题库问题序号 */
function rowBusExamQuestionResultIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 题库问题添加按钮操作 */
function handleAddBusExamQuestionResult() {
  let obj = {};
  obj.opt = "";
  obj.result = "";
  obj.isDel = "";
  busExamQuestionResultList.value.push(obj);
}

/** 题库问题删除按钮操作 */
function handleDeleteBusExamQuestionResult() {
  if (checkedBusExamQuestionResult.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的题库问题数据");
  } else {
    const busExamQuestionResults = busExamQuestionResultList.value;
    const checkedBusExamQuestionResults = checkedBusExamQuestionResult.value;
    busExamQuestionResultList.value = busExamQuestionResults.filter(function(item) {
      return checkedBusExamQuestionResults.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleBusExamQuestionResultSelectionChange(selection) {
  checkedBusExamQuestionResult.value = selection.map(item => item.index)
}

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

getList();
</script>
