<template>
  <div>
    <el-dialog v-model="props.examShow" title="错题练习" width="1300px" draggable :before-close="handleClose">
      <div class="main" v-if="item">
        <div class="left">
          <!-- 判断题 -->
          <div class="content" v-if="item.qtype == '0'">
            <span class="qtype">【判断题】</span>{{ item.questionname }}
          </div>
          <div class="answer_area" v-if="item.qtype == '0'">
            <el-radio-group v-model="item.checked" @change="changeAnswer($event)">
              <div class="exam">
                <el-radio :label="'T'" size="large">{{ "T、" + item.a }}</el-radio>
                <el-radio :label="'F'" size="large">{{ "F、" + item.b }}</el-radio>
              </div>
            </el-radio-group>
          </div>
          <!-- 单选题 -->
          <div class="content" v-if="item.qtype == '1'">
            <span class="qtype">【单选题】</span>
            {{ item.questionname }}
          </div>
          <div class="answer_area" v-if="item.qtype == '1'">
            <el-radio-group v-model="item.checked" @change="changeAnswer($event)">
              <div class="exam">
                <el-radio :label="'A'" size="large">{{ "A、" + item.a }}</el-radio>
                <el-radio :label="'B'" size="large">{{ "B、" + item.b }}</el-radio>
                <el-radio :label="'C'" size="large">{{ "C、" + item.c }}</el-radio>
                <el-radio :label="'D'" size="large">{{ "D、" + item.d }}</el-radio>
              </div>
            </el-radio-group>
          </div>
          <!-- 多选题 -->
          <div class="content" v-if="item.qtype == '2'">
            <span class="qtype">【多选题】</span>
            {{ item.questionname }}
          </div>
          <div class="answer_area" v-if="item.qtype == '2'">
            <el-checkbox-group v-model="item.checked" @change="changeAnswer($event)">
              <div class="exam">
                <el-checkbox :label="'A'" size="large">{{ "A、" + item.a }}</el-checkbox>
                <el-checkbox :label="'B'" size="large">{{ "B、" + item.b }}</el-checkbox>
                <el-checkbox :label="'C'" size="large">{{ "C、" + item.c }}</el-checkbox>
                <el-checkbox :label="'D'" size="large">{{ "D、" + item.d }}</el-checkbox>
              </div>
            </el-checkbox-group>
          </div>
          <div class="answer">
            <div>正确答案：{{ item.answer }}</div>
            <div>答案解析：{{ item.answerparse || '无' }}</div>
          </div>
          <div class="btns">
            <el-button @click="clickFlag"><el-icon>
                <Flag />
              </el-icon><span v-if="!item.isFlag">&nbsp;标记</span><span v-if="item.isFlag">&nbsp;取消标记</span></el-button>
            <el-button type="primary" v-if="index != 0" @click="prev">上一题</el-button>
            <el-button type="primary" v-if="index < list.length - 1" @click="next">下一题</el-button>
          </div>
        </div>
        <div class="right">
          <div class="right_item" v-if="pdList && pdList.length > 0">
            <div>判断题（共{{ pdList.length }}道）</div>
            <div class="list">
              <div v-for="(it, inde) in list" :key="inde" @click="selectOne(inde)">
                <div class="listItem" v-if="it.qtype == '0'">
                  <div class="index" :style="{
                    backgroundColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)',
                    borderColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)'
                  }">{{ inde + 1 }}</div>
                  <div class="flag">
                    <el-icon v-if="it.isFlag" :color="'var(--el-color-primary)'">
                      <Flag />
                    </el-icon>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="right_item" v-if="danXList && danXList.length > 0">
            <div>单选题（共{{ danXList.length }}道）</div>
            <div class="list">
              <div v-for="(it, inde) in list" :key="inde" @click="selectOne(inde)">
                <div class="listItem" v-if="it.qtype == '1'">
                  <div class="index" :style="{
                    backgroundColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)',
                    borderColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)'
                  }">{{ inde + 1 }}</div>
                  <div class="flag">
                    <el-icon v-if="it.isFlag" :color="'var(--el-color-primary)'">
                      <Flag />
                    </el-icon>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="right_item" v-if="duoXList && duoXList.length > 0">
            <div>多选题（共{{ duoXList.length }}道）</div>
            <div class="list">
              <div v-for="(it, inde) in list" :key="inde" @click="selectOne(inde)">
                <div class="listItem" v-if="it.qtype == '2'">
                  <div class="index" :style="{
                    backgroundColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)',
                    borderColor: it.isTrue ? 'var(--el-color-primary)' : 'rgb(231, 61, 61)'
                  }">{{ inde + 1 }}</div>
                  <div class="flag">
                    <el-icon v-if="it.isFlag" :color="'var(--el-color-primary)'">
                      <Flag />
                    </el-icon>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <div>
          <el-button type="primary" @click="handleClose">
            <el-icon>
              <Close />
            </el-icon>关闭
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted, defineProps, defineEmits } from "vue";
import { getApi } from "@/api/tobAcco_api.js";
// 数据
const props = defineProps({
  examShow: {
    type: Boolean,
    default: true
  },
  resultId: {
    type: String,
    default: ""
  },
  examId: {
    type: String,
    default: ""
  }
});
// 题目列表
const list = ref([]);
// 当前题目下标
const index = ref(0);
// 当前题目对象
const item = ref(null);
const emits = defineEmits(["changeVi3"]);
const pdList = ref([])
const danXList = ref([])
const duoXList = ref([])
// 初始化
onMounted(() => {
  // 查询考试试题列表
  getErrorExamList(props.resultId);
});
// 右侧选中一个
const selectOne = (inde) => {
  index.value = inde;
  item.value = list.value[index.value];
};
// 标记、取消标记
const clickFlag = () => {
  item.value.isFlag = !item.value.isFlag
}
// 上一题
const prev = () => {
  index.value = index.value - 1;
  item.value = list.value[index.value];
};
// 下一题
const next = () => {
  index.value = index.value + 1;
  item.value = list.value[index.value];
};
// 切换答案
const changeAnswer = (e) => {
  list.value[index.value].checked = e
  item.value.checked = e
  let answer; let checked;
  if (item.value.qtype == '2') {
    checked = item.value.checked.join('')
  } else {
    checked = item.value.checked
  }
  answer = item.value.answer
  answer = answer.split('').sort().join('')
  checked = checked.split('').sort().join('')
  item.value.isTrue = answer === checked
  list.value[index.value].isTrue = answer === checked
};
const getErrorExamList = async (id) => {
  getApi({}, "/mosty-jcgl/paper/practiceErrorPaper?id=" + id).then((res) => {
    list.value = res
    list.value.forEach((item) => {
      item.isTrue = false
      if (item.qtype == "2") {
        item.checked = item.error.split('');
      } else {
        item.checked = item.error
      }
    });
    item.value = res[index.value];
    pdList.value = res.filter(item => { return item.qtype == '0' })
    danXList.value = res.filter(item => { return item.qtype == '1' })
    duoXList.value = res.filter(item => { return item.qtype == '2' })
  });
};
// 关闭弹窗
const handleClose = () => {
  emits("changeVi3", false);
};
</script>
<style lang="scss" scoped>
.main {
  height: 500px;
  display: flex;

  .right {
    width: 30%;
    padding: 0 20px;
    overflow: auto;

    .right_item {
      margin-bottom: 15px;

      .list {
        display: flex;
        flex-wrap: wrap;

        .listItem {
          cursor: pointer;
          margin: 15px 0 0 15px;
          width: 30px;
          height: 42px;
          background: #e9e6e6;

          >div {
            display: flex;
            align-items: center;
            justify-content: center;
            box-sizing: border-box;
          }

          .index {
            height: 25px;
            border-bottom: 2px solid #fff;
            color: #fff;
          }
        }
      }
    }
  }

  .left {
    .btns {
      width: 100%;
      margin-top: 20px;
      padding-right: 30px;
      text-align: right;
    }

    .answer {
      border-top: 1px solid var(--el-color-primary);
      padding-top: 20px;
      line-height: 30px;
    }

    .answer_area {
      margin-top: 15px;

      .exam {
        display: flex;
        flex-direction: column;
      }
    }

    .content {
      margin: 10px 10px 0 10px;
      font-size: 16px;
      line-height: 22px;

      .qtype {
        color: var(--el-color-primary);
        font-size: 18px;
        font-weight: 900;
      }
    }

    .top {
      font-size: 20px;
      font-weight: 900;

      >span {
        color: var(--el-color-primary);
      }
    }

    width: 70%;
    border-right: 1px solid var(--el-color-primary);
  }
}

.countdown {
  line-height: 50px;
  color: red;
  font-size: 16px;
  font-weight: 800;
}

::v-deep .el-checkbox {
  color: #000;
}

::v-deep .el-radio {
  color: #000;
}

::v-deep .el-radio__input.is-checked .el-radio__inner {
  border-color: red;
  background: red;
}

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: red;
  border-color: red;
}
</style>
