<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="userName">
        <el-input
          v-model="queryParams.userName"
          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 label="考试结果" prop="resStatus">
        <el-select v-model="queryParams.resStatus" placeholder="请选择考试结果" clearable>
          <el-option
            v-for="dict in edu_res_status"
            :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:busExamUser:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:busExamUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:busExamUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:busExamUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="busExamUserList" @selection-change="handleSelectionChange">
      <el-table-column label="所属公司" align="center" prop="comName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="试卷名称" align="center" prop="examTitle" />
      <el-table-column label="试卷类型" align="center" prop="examType" />
      <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="userMark" />
      <el-table-column label="答题时间" align="center" prop="createTime" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结果状态" align="center" prop="resStatus">
        <template #default="scope">
          <dict-tag :options="edu_res_status" :value="scope.row.resStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="答题耗时" align="center" prop="useTimes" />
      <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:busExamUser:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:busExamUser: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="busExamUserRef" :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="userId">
          <el-input v-model="form.userId" placeholder="请输入用户主键" />
        </el-form-item>
        <el-form-item label="试卷名称" prop="examTitle">
          <el-input v-model="form.examTitle" 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="singleNum">
          <el-input v-model="form.singleNum" placeholder="请输入单选题数量" />
        </el-form-item>
        <el-form-item label="多选题数量" prop="multipleNum">
          <el-input v-model="form.multipleNum" placeholder="请输入多选题数量" />
        </el-form-item>
        <el-form-item label="判断题数量" prop="estimateNum">
          <el-input v-model="form.estimateNum" placeholder="请输入判断题数量" />
        </el-form-item>
        <el-form-item label="用户分数" prop="userMark">
          <el-input v-model="form.userMark" placeholder="请输入用户分数" />
        </el-form-item>
        <el-form-item label="结果状态" prop="resStatus">
          <el-radio-group v-model="form.resStatus">
            <el-radio
              v-for="dict in edu_res_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="答题耗时" prop="useTimes">
          <el-input v-model="form.useTimes" 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-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="BusExamUser">
import { listBusExamUser, getBusExamUser, delBusExamUser, addBusExamUser, updateBusExamUser } from "@/api/manage/busExamUser";
import useUserStore from '@/store/modules/user'

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

const userStore = useUserStore()
const busExamUserList = 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,
    comId: null,
    projectId: null,
    userName: null,
    examTitle: null,
    examType: null,
    craftType: null,
    craftPost: null,
  },
  rules: {
  }
});

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

/** 查询用户试卷列表 */
function getList() {
  loading.value = true;
  listBusExamUser(queryParams.value).then(response => {
    busExamUserList.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,
    userId: null,
    examTitle: null,
    examType: null,
    craftType: null,
    craftPost: null,
    singleNum: null,
    multipleNum: null,
    estimateNum: null,
    userMark: null,
    resStatus: null,
    useTimes: null,
    isDel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("busExamUserRef");
}

/** 搜索按钮操作 */
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
  getBusExamUser(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户试卷";
  });
}

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

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

getList();
</script>
