<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName" v-if="!userStore.currentProId">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷名称" prop="examTitle">
        <el-input
          v-model="queryParams.examTitle"
          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" v-if="queryParams.craftType">
        <el-select v-model="queryParams.craftPost" placeholder="请选择工种岗位" clearable>
          <el-option
            v-for="dict in pro_craft_post"
            v-show="dict.elTagClass == queryParams.craftType"
            :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:busExamInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:busExamInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:busExamInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:busExamInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="busExamInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="试卷编号" align="center" prop="id" width="80"/>
      <el-table-column label="所属公司" align="center" prop="comName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="试卷名称" align="center" prop="examTitle" />
      <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="singleNum" width="68"/>
      <el-table-column label="单选题分数" align="center" prop="singleMark" width="68"/>
      <el-table-column label="多选题数量" align="center" prop="multipleNum" width="68"/>
      <el-table-column label="多选题分数" align="center" prop="multipleMark" width="68"/>
      <el-table-column label="判断题数量" align="center" prop="estimateNum" width="68"/>
      <el-table-column label="判断题分数" align="center" prop="estimateMark" width="68"/>
      <el-table-column label="通过分数" align="center" prop="passMark" width="100">
        <template #default="scope">
          <el-tag type="success" effect="dark">{{ scope.row.passMark }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="答题次数" align="center" prop="useNumber" />
      <el-table-column label="是否启用" align="center" prop="isDel">
        <template #default="scope">
          <el-tooltip :content="scope.row.isDel == 0 ? '启用' : '停用'" placement="top">
            <el-switch
              :active-value="parseInt(0)"
              :inactive-value="parseInt(1)"
              v-model="scope.row.isDel"
              @change="setStatus($event, scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150" 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:busExamInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:busExamInfo: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="680px" append-to-body>
      <el-form ref="busExamInfoRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" v-if="form.projectId">
          <el-tag effect="plain">{{ form.projectName }}</el-tag>
        </el-form-item>
        <el-form-item label="试卷名称" prop="examTitle">
          <el-input v-model="form.examTitle" placeholder="请输入试卷名称" maxlength="64" show-word-limit/>
        </el-form-item>
        <el-form-item label="工种类型" prop="craftType">
          <el-select v-model="form.craftType" placeholder="请选择工种类型" style="width:100%" @change="changeCraftType">
            <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="请选择工种岗位" style="width:100%">
            <el-option
              v-for="dict in pro_craft_post"
              v-show="dict.elTagClass == form.craftType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">试卷分数设置</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单选题数量" prop="singleNum">
              <el-input v-model.number="form.singleNum" placeholder="请输入单选题数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单选题分数" prop="singleMark">
              <el-input v-model.number="form.singleMark" placeholder="请输入单选题分数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="多选题数量" prop="multipleNum">
              <el-input v-model.number="form.multipleNum" placeholder="请输入多选题数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="多选题分数" prop="multipleMark">
              <el-input v-model.number="form.multipleMark" placeholder="请输入多选题分数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单选题数量" prop="estimateNum">
              <el-input v-model.number="form.estimateNum" placeholder="请输入单选题数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单选题分数" prop="estimateMark">
              <el-input v-model.number="form.estimateMark" placeholder="请输入单选题分数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="及格分数线" prop="passMark">
          <el-input v-model.number="form.passMark" 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="BusExamInfo">
import { listBusExamInfo, getBusExamInfo, delBusExamInfo, addBusExamInfo, updateBusExamInfo } from "@/api/manage/busExamInfo";
import useUserStore from '@/store/modules/user'

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

const userStore = useUserStore()
const busExamInfoList = ref([]);
const busExamInfoQuesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedBusExamInfoQues = 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,
    projectName: null,
    examTitle: null,
    craftType: null,
    craftPost: null,
  },
  rules: {
    examTitle: [{ required: true, message: "试卷名称不能为空", trigger: "blur" }],
    craftType: [{ required: true, message: "工种类型不能为空", trigger: "change" }],
    craftPost: [{ required: true, message: "工种岗位不能为空", trigger: "change" }],
    singleNum: [
      { required: true, message: "单选题数量不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    singleMark: [
      { required: true, message: "单选题分数不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    multipleNum: [
      { required: true, message: "多选题数量不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    multipleMark: [
      { required: true, message: "多选题分数不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    estimateNum: [
      { required: true, message: "判断题数量不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    estimateMark: [
      { required: true, message: "判断题分数不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ],
    passMark: [
      { required: true, message: "合格分数不能为空", trigger: "blur" },
      { type: "number", message: "请输入数字类型", trigger: "blur" },
    ]
  }
});

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

/** 查询基础试卷列表 */
function getList() {
  loading.value = true;
  listBusExamInfo(queryParams.value).then(response => {
    busExamInfoList.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,
    examTitle: null,
    examType: null,
    craftType: null,
    craftPost: null,
    singleNum: null,
    singleMark: null,
    multipleNum: null,
    multipleMark: null,
    estimateNum: null,
    estimateMark: null,
    fullMark: null,
    passMark: null,
    useNumber: null,
    isDel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  busExamInfoQuesList.value = [];
  proxy.resetForm("busExamInfoRef");
}

/** 搜索按钮操作 */
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() {
  if(!userStore.currentProId){
    proxy.$modal.msgWarning("请切换到项目数据！！！");
    return false;
  }
  reset();
  form.value.projectId = userStore.currentProId;
  form.value.projectName = userStore.currentProName;
  open.value = true;
  title.value = "添加基础试卷";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBusExamInfo(_id).then(response => {
    form.value = response.data;
    busExamInfoQuesList.value = response.data.busExamInfoQuesList;
    open.value = true;
    title.value = "修改基础试卷";
  });
}

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

/** 启用状态滑块控制 */
function setStatus(val, row) {
  proxy.$modal
    .confirm(`是否确认${val == 0 ? "启用" : "停用"}当前数据项？`)
    .then(function () {
      let param = {'id': row.id, 'isDel': val};
      return updateBusExamInfo(param);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("修改成功");
    })
    .catch(() => {
      // 取消时恢复原始开关状态
      if (val == 0) {
        row.isDel = 1;
      } else {
        row.isDel = 0;
      }
    });
}

/** 岗位类型改变 */
function changeCraftType() {
  form.value.craftPost = null;
}

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

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

/** 试卷问题添加按钮操作 */
function handleAddBusExamInfoQues() {
  let obj = {};
  obj.questionId = "";
  obj.marks = "";
  obj.sortBy = "";
  busExamInfoQuesList.value.push(obj);
}

/** 试卷问题删除按钮操作 */
function handleDeleteBusExamInfoQues() {
  if (checkedBusExamInfoQues.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的试卷问题数据");
  } else {
    const busExamInfoQuess = busExamInfoQuesList.value;
    const checkedBusExamInfoQuess = checkedBusExamInfoQues.value;
    busExamInfoQuesList.value = busExamInfoQuess.filter(function(item) {
      return checkedBusExamInfoQuess.indexOf(item.index) == -1
    });
  }
}

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

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

getList();
</script>
<style lang="scss" scope>
.gap-2{
  .el-tag--default{
    margin:3px 4px;
  }
}
.el-divider__text{
  color: #409eff;
  font-weight: 800;
}
</style>