<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.currentPrjId">
        <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="请选择工种类型" @change="changeCraftType" 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 v-if="false" :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="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="userName">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.userName
          }}</el-button>
        </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="userMark" />
      <el-table-column label="答题耗时" align="center" prop="useTimes" />
      <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" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="false" link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:busExamUser:edit']">修改</el-button>
          <el-button v-if="false" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:busExamUser:remove']">删除</el-button>
          <el-button link type="primary" icon="Management" @click="handleDetail(scope.row)" v-hasPermi="['manage:busExamUser:query']">试卷详情</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="880px" append-to-body modal-class="busExamUserDlg">
        <el-row class="task_panel">
          <el-col :span="6">
            <el-statistic class="statistic_div">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  <el-icon style="margin-right: 4px; color: #409eff" :size="12">
                    <InfoFilled />
                  </el-icon>
                  <strong>考试结果</strong>
                </div>
              </template>
            </el-statistic>
            <div v-if="form.resStatus == '0'" class="countdown-footer">
              <strong style="font-size: 16px; color: #909399; font-weight: 800;">未提交</strong>
            </div>
            <div v-if="form.resStatus == '1'" class="countdown-footer">
              <strong style="font-size: 16px; color: #67c23a; font-weight: 800;">已通过</strong>
            </div>
            <div v-if="form.resStatus == '2'" class="countdown-footer">
              <strong style="font-size: 16px; color: #f56c6c; font-weight: 800;">不通过</strong>
            </div>
          </el-col>
          <el-col :span="6">
            <el-statistic class="statistic_div">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  <el-icon style="margin-right: 4px; color: #67c23a" :size="12">
                    <TrendCharts />
                  </el-icon>
                  <strong>考试分数</strong>
                </div>
              </template>
            </el-statistic>
            <div class="countdown-footer">
              <strong style="font-size: 16px; color: #909399">{{form.userMark!=null?form.userMark:'-'}}</strong>
            </div>
          </el-col>
          <el-col :span="6">
            <el-statistic class="statistic_div">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  <el-icon style="margin-right: 4px; color: #409eff" :size="12">
                    <InfoFilled />
                  </el-icon>
                  <strong>答题耗时</strong>
                </div>
              </template>
            </el-statistic>
            <div class="countdown-footer">
              <strong style="font-size: 16px; color: #909399" v-if="form.useTimes">{{formatDuraTime(form.useTimes)}}</strong>
              <strong style="font-size: 16px; color: #909399" v-if="!form.useTimes"> - </strong>
            </div>
          </el-col>
          <el-col :span="6">
            <el-statistic class="statistic_div">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  <el-icon style="margin-right: 4px" :size="12">
                    <Calendar />
                  </el-icon>
                  答题时间
                </div>
              </template>
            </el-statistic>
            <div class="countdown-footer">
              <strong>{{ parseTime(form.createTime, "{y}-{m}-{d} {h}:{i}") }}</strong>
            </div>
          </el-col>
        </el-row>

        <div class="startExamination">
          <ul>
            <li v-for="(item,idx) in useQuestionList" :key="item.id">
              <div class="topicTilte" style="font-weight: bold" ref="topicTilte">
                {{ idx + 1 }}、<span class="score">[{{ item.questionType==1?'单选题':(item.questionType==2?'多选题':'判断题')}}]</span>{{ item.questionTitle }}
              </div>
              <template v-if="item.questionType === 1">
                <el-radio-group v-model="item.userAnswer" class="ml-4 Selected_item_radio" style="width: 100%">
                  <div v-for="(option,ox) in item.questionOption" class="Selected_item">
                    <el-radio :label="option.opt" disabled> {{ option.result }} </el-radio>
                  </div>
                </el-radio-group>
                <div :class="item.answer == item.userAnswer?'answer':'error'">
                  <div class="correctAnswer">正确答案：{{ item.answer }}</div>
                  <div class="testAnswers">试卷答案：{{ item.userAnswer }}</div>
                </div>
              </template>
              <template v-if="item.questionType === 2">
                <el-checkbox-group v-model="item.userAnswerValues" class="ml-4 Selected_item_radio" style="width: 100%">
                  <div v-for="option in item.questionOption" class="Selected_item">
                    <el-checkbox :label="option.opt" disabled> {{ option.result }} </el-checkbox>
                  </div>
                </el-checkbox-group>
                <div :class="item.answer == item.userAnswer?'answer':'error'">
                  <div class="correctAnswer">正确答案：{{ item.answer }}</div>
                  <div class="testAnswers">试卷答案：{{ item.userAnswer }}</div>
                </div>
              </template>
              <template v-if="item.questionType === 3">
                <el-checkbox-group v-model="item.userAnswerValues" class="ml-4 Selected_item_radio" style="width: 100%">
                  <div v-for="option in item.questionOption" class="Selected_item">
                    <el-checkbox :label="option.opt" disabled> {{ option.result }} </el-checkbox>
                  </div>
                </el-checkbox-group>
                <div :class="item.answer == item.userAnswer?'answer':'error'">
                  <div class="correctAnswer">正确答案：{{ item.answer }}</div>
                  <div class="testAnswers">试卷答案：{{ item.userAnswer }}</div>
                </div>
              </template>
            </li>
          </ul>
        </div>
      <template #footer>
        <div class="dialog-footer">
          <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 useQuestionList = 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 handleDetail(row) {
  reset();
  getBusExamUser(row.id).then(response => {
    let list = response.data.busExamUserResultList;
    list.forEach(item => {
      if(item.questionType===2){
        if(item.userAnswer==null){
          item.userAnswer = "";
        }
        item.userAnswerValues = item.userAnswer.split(',');
      }
      item.questionOption = JSON.parse(item.questionOption);
    });
    useQuestionList.value = list;
    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`)
}

/** 改变工种类型 */
function changeCraftType() {
  queryParams.value.craftPost = null;
}

getList();
</script>
<style lang="scss" scope>
.el-divider__text{
  color: #409eff;
  font-weight: 800;
}
.busExamUserDlg{
  .el-dialog__body{
    margin-top: -25px;
    overflow: auto;
    max-height: 680px;
  }
}
.task_panel {
  text-align: center;
  margin-bottom: 20px;
}
.statistic_div {
  .el-statistic__content {
    display: none;
  }
}
.startExamination {
  ul {
    margin: 30px 0;
    li {
      list-style: none;
      margin-top: 10px;
      .Selected_item_radio {
        display: block;
        flex: none;
      }
      .topicTilte {
        .score {
          display: inline-block;
          margin-right: 10px;
        }
      }
      .Selected_item_fill {
        height: 45px;
        display: flex;
        align-items: center;
      }
      .answer {
        height: 30px;
        display: flex;
        align-items: center;
        border: 1px solid #e1f3d8;
        color: #67c23a;
        padding: 0 15px;
        //margin: 10px 20px 10px 0;
        background-color: #f0f9eb;
        .correctAnswer {
          margin-right: 40px;
        }
      }
      .error {
        height: 30px;
        display: flex;
        align-items: center;
        padding: 0 15px;
        //margin: 10px 20px 10px 0;
        background: #fdf6ec;
        border: 1px solid #faecd8;
        color: #f56c6c;
        .correctAnswer {
          margin-right: 40px;
        }
      }
      .CompletionAnswer {
        height: 80px;
        display: flex;
        align-items: center;
        border: 1px solid #e1f3d8;
        color: #67c23a;
        padding: 0 15px;
        margin: 10px 20px 10px 0;
        background-color: #f0f9eb;
        .CompletionAnswerIndex {
          width: 50px;
          height: 100%;
          display: flex;
          align-items: center;
          border-right: 1px solid #e1f3d8;
          margin-right: 20px;
        }
        .correctAnswer {
          margin-right: 40px;
        }
      }
      .CompletionError {
        height: 80px;
        display: flex;
        align-items: center;
        padding: 0 15px;
        margin: 10px 20px 10px 0;
        background: #fdf6ec;
        border: 1px solid #faecd8;
        color: #f56c6c;
        .CompletionAnswerIndex {
          width: 25px;
          height: 100%;
          display: flex;
          align-items: center;
          border-right: 1px solid #faecd8;
          margin-right: 20px;
        }
        .correctAnswer {
          height: 40px;
          margin-right: 40px;
        }
      }
    }
  }
}
.el-alert {
	margin: 20px 0 0;
}
.el-alert:first-child {
	margin: 0;
}
</style>