<template>
  <layout-card>
    <div class="tr">
      <el-button v-print="'#print'" type="primary">打印</el-button>
    </div>
    <div id="print">
      <div v-html="PRINT_STYLE" />
      <div class="page-break print_use cover">
        <div class="tr">档案编号：</div>
        <div class="title" style="">生产经营单位从业人员安全培训档案</div>
        <div class="content">
          <div>
            <span class="item"> 姓名 </span>
            <span>：{{ data.info.NAME }}</span>
          </div>
          <div class="pt-20">
            <span class="item">身份证号</span>
            <span>：{{ data.info.USER_ID_CARD }}</span>
          </div>
          <div class="pt-20">
            <span class="item">建档日期</span>
            <span>：{{ dayjs().format("YYYY-MM-DD") }}</span>
          </div>
          <div class="pt-20">单位名称（公章）：</div>
        </div>
      </div>
      <div class="page-break">
        <el-divider content-position="left">学时证明</el-divider>
        <div class="flex">
          <span>证书编号:</span>
        </div>
        <table>
          <tr>
            <td class="title">姓名</td>
            <td>{{ data.info.NAME }}</td>
          </tr>
          <tr>
            <td class="title">证件类型</td>
            <td>身份证</td>
          </tr>
          <tr>
            <td class="title">证件编号</td>
            <td>{{ data.info.USER_ID_CARD }}</td>
          </tr>
          <tr>
            <td class="title">培训单位名称</td>
            <td>{{ data.info.CORP_NAME }}</td>
          </tr>
          <tr>
            <td class="title">任务名称</td>
            <td>{{ data.info.STUDY_NAME }}</td>
          </tr>
          <tr>
            <td class="title">培训日期</td>
            <td>
              {{ dayjs(data.info.PEIXUE_START_TIME).format("YYYY年MM月DD日") }}
              至
              {{ dayjs(data.info.PEIXUE_END_TIME).format("YYYY年MM月DD日") }}
            </td>
          </tr>
          <tr>
            <td class="title">培训类型</td>
            <td>{{ data.info.train_type_name }}</td>
          </tr>
          <tr>
            <td class="title">视频学习时长</td>
            <td>
              {{ data.info.video_class_hours }}分钟(包含{{
                data.info.video_count
              }}个视频)
            </td>
          </tr>
          <tr>
            <td class="title">资料学习时长</td>
            <td>
              {{ data.info.data_class_hours }}分钟(包含{{
                data.info.data_count
              }}个资料)
            </td>
          </tr>
          <tr>
            <td class="title">合计学习时长</td>
            <td>
              {{ data.info.video_class_hours + data.info.data_class_hours }}分钟
            </td>
          </tr>
          <tr style="vertical-align: top">
            <td>
              <div style="height: 150px" />
              <div>培训单位:(盖章)</div>
              <div>日期:{{ dayjs().format("YYYY年MM月DD日") }}</div>
            </td>
            <td></td>
          </tr>
        </table>
      </div>
      <div class="page-break">
        <el-divider content-position="left">课程材料</el-divider>
        <div class="flex">
          <span>姓名:{{ data.info.NAME }}</span>
          <span>身份证号:{{ data.info.USER_ID_CARD }}</span>
        </div>
        <table>
          <tr>
            <td class="title">任务名称</td>
            <td class="tc" colspan="3">{{ data.info.STUDY_NAME }}</td>
          </tr>
          <tr>
            <td class="title">序号</td>
            <td class="title">课程内容</td>
            <td class="title">课时</td>
            <td class="title">讲师</td>
          </tr>
          <tr v-for="(item, index) in data.curriculumList" :key="index">
            <td class="tc">{{ index }}</td>
            <td class="tc">{{ item.COURSEWARENAME }}</td>
            <td class="tc">{{ (item.CLASSHOUR / 45).toFixed(2) }}</td>
            <td class="tc">{{ item.SPEAKER }}</td>
          </tr>
        </table>
      </div>
      <div class="page-break">
        <el-divider content-position="left">考卷详情</el-divider>
        <div class="paper-details">
          <h1 class="tc">
            {{ data.info.EXAMNAME }}
          </h1>
          <div class="tc">(满分:{{ data.info.EXAMSCORE || 0 }}分)</div>
          <div class="sub_flex">
            <span>任务名称:{{ data.info.STUDY_NAME }}</span>
            <span>姓名:{{ data.info.NAME }}</span>
          </div>
          <div class="sub_flex">
            <span>考试时间:{{ data.info.OPERATTIME }}</span>
            <span>分数:{{ data.info.userSecond?.[0] }}</span>
          </div>
          <template v-if="data.selectList.length > 0">
            <div class="paper-class">单选题</div>
            <div
              v-for="(item, index) in data.selectList"
              :key="item.QUESTION_ID"
              class="paper-item"
            >
              <div class="paper-ask">
                {{ index + 1 }}.{{ item.QUESTIONDRY }}
              </div>
              <div class="paper-answer">
                <div>A.{{ item.OPTIONA }}</div>
                <div>B.{{ item.OPTIONB }}</div>
                <div>C.{{ item.OPTIONC }}</div>
                <div>D.{{ item.OPTIOND }}</div>
                <div>参考答案:{{ item.ANSWER }}</div>
              </div>
            </div>
          </template>
          <template v-if="data.multipleList.length > 0">
            <div class="paper-class">多选题</div>
            <template
              v-for="(item, index) in data.multipleList"
              :key="item.QUESTION_ID"
            >
              <div v-if="item.QUESTIONTYPE === '2'" class="paper-item">
                <div class="paper-ask">
                  {{ index + 1 }}.{{ item.QUESTIONDRY }}
                </div>
                <div class="paper-answer">
                  <div>A.{{ item.OPTIONA }}</div>
                  <div>B.{{ item.OPTIONB }}</div>
                  <div>C.{{ item.OPTIONC }}</div>
                  <div>D.{{ item.OPTIOND }}</div>
                  <div>参考答案:{{ item.ANSWER }}</div>
                </div>
              </div>
            </template>
          </template>
          <template v-if="data.judgeList.length > 0">
            <div class="paper-class">判断题</div>
            <template
              v-for="(item, index) in data.judgeList"
              :key="item.QUESTION_ID"
            >
              <div v-if="item.QUESTIONTYPE === '3'" class="paper-item">
                <div class="paper-ask">
                  {{ index + 1 }}.{{ item.QUESTIONDRY }}
                </div>
                <div class="paper-answer">
                  <div>A.{{ item.OPTIONA }}</div>
                  <div>B.{{ item.OPTIONB }}</div>
                  <div>参考答案:{{ item.ANSWER }}</div>
                </div>
              </div>
            </template>
          </template>
        </div>
      </div>
      <div class="page-break">
        <el-divider content-position="left">培训考核记录</el-divider>
        <h3 class="tc">
          {{ data.info.train_type_name }}生产经营单位{{
            data.info.post_type_name
          }}安全培训考核记录
        </h3>
        <div class="flex">
          <span>生产经营单位或安全生产管理机构名称:(盖章)</span>
          <span>档案编号:</span>
        </div>
        <table>
          <tr>
            <td class="title">姓名</td>
            <td class="tc">{{ data.info.NAME }}</td>
            <td class="title">性别</td>
            <td class="tc">{{ data.info.SEX_NAME }}</td>
            <td class="title">身份证</td>
            <td class="tc">{{ data.info.USER_ID_CARD }}</td>
            <td class="title">学历</td>
            <td class="tc">{{ data.info.DEGREE_OF_EDUCATION_NAME }}</td>
            <td class="title">专业</td>
            <td class="tc" />
          </tr>
          <tr>
            <td class="title">职务</td>
            <td class="tc">{{ data.info.DUTIES_NAME }}</td>
            <td class="title">部门</td>
            <td class="tc">{{ data.info.DEPARTMENT_NAME }}</td>
            <td class="title">工种</td>
            <td class="tc">{{ data.info.TYPE_OF_WORK_NAME }}</td>
            <td class="title">行业类别</td>
            <td class="tc">{{ data.info.train_type_name }}</td>
            <td class="title">联系电话</td>
            <td class="tc">{{ data.info.USERNAME }}</td>
          </tr>
          <tr>
            <td class="title">人员类型</td>
            <td class="tc" colspan="9">
              <template v-for="(item, index) in personnelTypeList" :key="index">
                <input
                  type="checkbox"
                  :checked="item.DICTIONARIES_ID === data.checkPerType"
                  disabled
                  style="margin-left: 15px"
                />
                {{ item.NAME }}
              </template>
            </td>
          </tr>
          <tr>
            <td class="title" colspan="10">安全培训及考核实施情况</td>
          </tr>
          <tr>
            <td colspan="10" style="padding: 0">
              <table class="table-inside">
                <tr>
                  <td class="title" width="6%">序号</td>
                  <td class="title" width="10%">培训时间</td>
                  <td class="title" width="10%">培训地点</td>
                  <td class="title" width="40%">培训主要内容</td>
                  <td class="title" width="8%">学时</td>
                  <td class="title" width="8%">培训教师</td>
                  <td class="title" width="8%">考试成绩</td>
                  <td class="title" width="8%">补考成绩</td>
                  <td class="title" width="10%">本人签字</td>
                </tr>
                <tr v-for="(item, index) in data.curriculumList" :key="index">
                  <td class="tc">{{ index }}</td>
                  <td class="tc">
                    {{
                      dayjs(data.info.PEIXUE_START_TIME).format(
                        "YYYY年MM月DD日"
                      )
                    }}
                    至
                    {{
                      dayjs(data.info.PEIXUE_END_TIME).format("YYYY年MM月DD日")
                    }}
                  </td>
                  <td class="tc">一体化危化平台</td>
                  <td class="tc">{{ item.COURSEWARENAME }}</td>
                  <td class="tc">{{ (item.CLASSHOUR / 45).toFixed(2) }}学时</td>
                  <td class="tc">{{ item.SPEAKER }}</td>
                  <td
                    v-if="index === 0"
                    :rowspan="data.curriculumList.length + 1"
                    class="tc"
                  >
                    {{ data.info.userSecond?.[0] }}
                  </td>
                  <td
                    v-if="index === 0"
                    :rowspan="data.curriculumList.length + 1"
                    class="tc"
                  >
                    {{ data.info.userSecond?.[1] }}
                  </td>
                  <td
                    v-if="index === 0"
                    :rowspan="data.curriculumList.length + 1"
                    class="tc"
                  />
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td colspan="10" class="td_style_none">
              <div class="flex">
                <div>
                  记录人员(签字):
                  <div
                    v-for="(item, index) in data.studyTaskSignList"
                    :key="index"
                  >
                    <img
                      v-if="item.TYPE === '1' && item.USER_SIGN_FILE_PATH"
                      :src="VITE_FILE_URL + item.USER_SIGN_FILE_PATH"
                      style="height: 50px; width: 50px"
                      v-viewer
                      alt=""
                      class="mt-10"
                    />
                  </div>
                </div>
                <div>考核人员(签字):</div>
                <div>
                  安全生产管理机构负责人(签章):
                  <div
                    v-for="(item, index) in data.studyTaskSignList"
                    :key="index"
                  >
                    <img
                      v-if="item.TYPE === '2' && item.USER_SIGN_FILE_PATH"
                      :src="VITE_FILE_URL + item.USER_SIGN_FILE_PATH"
                      style="height: 50px; width: 50px"
                      v-viewer
                      alt=""
                      class="mt-10"
                    />
                  </div>
                </div>
                <div>归档日期:{{ dayjs().format("YYYY年MM月DD日") }}</div>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </layout-card>
</template>

<script setup>
import { reactive } from "vue";
import dayjs from "dayjs";
import { useUserStore } from "@/pinia/user";
import { useRoute } from "vue-router";
import {
  getStudyTaskCoursewareList,
  getArchivesStudyTaskPaper,
  getStudyTaskSign,
  getUserExamScore,
} from "@/request/file_management.js";
import { layoutFnGetPersonnelType } from "@/assets/js/data_dictionary";
import { PRINT_STYLE } from "@/assets/js/constant.js";
import { getUserView } from "@/request/enterprise_management.js";
import {
  getTaskViewInfo,
  getTaskViewUserList,
} from "@/request/online_learn_exam.js";

const VITE_FILE_URL = import.meta.env.VITE_FILE_URL;
const userStore = useUserStore();
const route = useRoute();
const { STUDYTASK_ID, USER_ID } = route.query;
const data = reactive({
  info: {},
  curriculumList: [],
  selectList: [],
  multipleList: [],
  judgeList: [],
  studyTaskSignList: [],
  checkPerType: [],
});
const fnGetData = async () => {
  let resData = {};
  const studyTaskResData = await getTaskViewInfo({ STUDYTASK_ID });
  const studentInformationResData = await getUserView({ USER_ID });
  const studyTaskCoursewareListResData = await getStudyTaskCoursewareList({
    STUDYTASK_ID,
  });
  const examinationResultsResData = await getTaskViewUserList({
    STUDYTASK_ID,
    USER_ID,
  });
  const studyTaskPaperResData = await getArchivesStudyTaskPaper({
    STUDYTASK_ID,
    USER_ID,
  });
  const studyTaskSignResData = await getStudyTaskSign({
    STUDYTASK_ID,
  });
  const userExamScoreResData = await getUserExamScore({
    STUDYTASK_ID,
  });
  resData = {
    ...studyTaskResData.pd,
    ...studentInformationResData.pd,
    data_class_hours: 0,
    data_count: 0,
    video_class_hours: 0,
    video_count: 0,
    CORP_NAME: userStore.getUserInfo.CORP_NAME,
    userSecond: userExamScoreResData.userExamScore[USER_ID]?.split(",") || [
      0, 0,
    ],
    ...examinationResultsResData.varList[0],
    ...studyTaskPaperResData.paper,
  };
  studyTaskCoursewareListResData.varList.forEach((item) => {
    if (item.COURSEWARENAME.indexOf("[资料课件]") > -1) {
      resData.data_class_hours += item.CLASSHOUR;
      resData.data_count++;
    } else if (item.COURSEWARENAME.indexOf("[视频课件]") > -1) {
      resData.video_class_hours += item.CLASSHOUR;
      resData.video_count++;
    }
  });
  studyTaskPaperResData.questionList.forEach((item) => {
    if (item.QUESTIONTYPE === "1") {
      data.selectList.push(item);
    } else if (item.QUESTIONTYPE === "2") {
      data.multipleList.push(item);
    } else if (item.QUESTIONTYPE === "3") {
      data.judgeList.push(item);
    }
  });
  data.checkPerType = studentInformationResData.pd.PERSONNEL_TYPE;
  data.curriculumList = studyTaskCoursewareListResData.varList;
  data.studyTaskSignList = studyTaskSignResData.varList;
  data.info = resData;
};
fnGetData();
const personnelTypeList = await layoutFnGetPersonnelType();
</script>

<style scoped lang="scss">
.flex {
  width: 80%;
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  font-size: 14px;
}

table {
  border-collapse: collapse;
  width: 100%;

  td {
    border: 1px solid var(--el-border-color);
    padding: 8px 12px;
    font-size: 14px;
    line-height: 1.6;
  }

  .td_style_none {
    border: none;
    padding: 0;
    line-height: 1;
  }

  .title {
    background: var(--el-fill-color-light);
    width: 200px;
    text-align: center;
  }

  img {
    width: 100%;
    height: 100%;
  }
}

.table-inside {
  border-collapse: collapse;
  width: calc(100% + 2px);
  margin: -1px -2px -1px -1px;
}

.cover {
  width: 700px;
  margin: auto;

  .title {
    font-size: 30px;
    font-weight: 700;
    text-align: center;
    padding-top: 100px;
  }

  .content {
    width: 300px;
    margin: auto;
    padding-top: 400px;

    .item {
      text-align: justify;
      text-align-last: justify;
      width: 64px;
      display: inline-block;
    }
  }
}

.page-break {
  page-break-after: always;

  .paper-details {
    border: 1px solid var(--el-border-color);
    padding: 0 20px;

    .sub_flex {
      justify-content: space-between;
      display: flex;
      width: 80%;
      font-size: 14px;
      margin-bottom: 10px;
    }

    .paper-class {
      font-size: 14px;
      font-weight: bold;
      margin-top: 10px;
    }

    .paper-item {
      border-bottom: 1px dashed var(--el-border-color);
      padding: 0 10px 10px 10px;

      &:last-child {
        border-bottom: none;
      }

      .paper-ask {
        font-size: 14px;
        margin: 10px 0;
      }

      .paper-answer {
        display: flex;
        padding: 0 40px;

        div {
          flex: 1;
        }
      }
    }
  }
}
</style>
