<template>
  <div class="c_box">
    <!-- <div class="mask"></div> -->
    <div v-if="!checkJson">暂未设置题目</div>
    <div v-else class="choice_box">
      <div class="c_title">
        <div class="title">
          {{ tindex + 1 + '、' + `（${option[checkJson.type].name}）` + checkJson.title
          }}<span style="color: #efa030" v-if="checkJson.score">（{{ '分值：' + checkJson.score + '分' }}）</span>
        </div>
        <div class="p_box" v-if="isTeacher == 1 && checkJson.score">
          <el-input v-model="score2" class="c_input" disabled></el-input><span style="margin: 0 10px">/</span
          ><span>{{ checkJson.score }}分</span>
        </div>
        <div class="p_box" v-if="isTeacher == 2 && checkJson.score2">
          <span>{{ checkJson.score2 }}分</span><span style="margin: 0 10px">/</span><span>{{ checkJson.score }}分</span>
        </div>
      </div>
      <div class="choices">
        <div class="choice" v-for="(item, index) in checkJson.array" :key="index">
          <div class="choice_c" v-if="checkJson.type == 2">
            <el-checkbox v-model="checkJson.answer2" :label="index" :disabled="checktype == 2"></el-checkbox
            ><span :class="{ right: see && cJson.answer.indexOf(index) != -1 }" @click="check(checkJson.type, index)">{{
              item.option
            }}</span>
          </div>
          <div class="choice_c" v-if="checkJson.type == 1">
            <el-radio v-model="checkJson.answer2[0]" :label="index" :disabled="checktype == 2"></el-radio
            ><span :class="{ right: see && cJson.answer.indexOf(index) != -1 }" @click="check(checkJson.type, index)">{{
              item.option
            }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tindex: {
      type: Number
    },
    cJson: {
      type: Object
    },
    checktype: {
      type: Number,
      default: 1
    },
    see: {
      type: Boolean,
      default: false
    },
    isTeacher: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      option: {
        1: { name: '单选题' },
        2: { name: '多选题' }
      },
      checkJson: undefined
    }
  },
  computed: {
    score2() {
      let answer2 = this.checkJson.answer2 ? this.checkJson.answer2.sort().join(',') : ''
      let answer = this.checkJson.answer ? this.checkJson.answer.sort().join(',') : ''
      this.checkJson.score2 = answer2 == answer ? this.checkJson.score : 0
      this.$forceUpdate()
      return answer2 == answer ? this.checkJson.score : 0
    }
  },
  watch: {
    checkJson: {
      handler(newValue) {
        this.$emit('update:cJson', newValue)
      },
      deep: true
    }
  },
  methods: {
    depthCopy(s) {
      return JSON.parse(JSON.stringify(s))
    },
    check(type, index) {
      if (this.checktype == 2) {
        return
      }
      if (type == 2) {
        if (this.checkJson.answer2.indexOf(index) == -1) {
          this.checkJson.answer2.push(index)
        } else {
          this.checkJson.answer2.splice(this.checkJson.answer2.indexOf(index), 1)
        }
      } else if (type == 1) {
        this.checkJson.answer2[0] = index
      }
      this.$forceUpdate()
    }
  },
  mounted() {
    this.checkJson = this.cJson ? this.depthCopy(this.cJson) : undefined
  }
}
</script>

<style lang="scss" scoped>
.c_box {
  width: 100%;
  position: relative;
  .choice_box {
    .c_title {
      display: flex;
      justify-content: space-between;
      .title {
        font-weight: bold;
        width: 100%;
        word-break: break-all;
        font-size: 16px;
      }
      .p_box {
        margin-left: 5px;
        min-width: fit-content;
        display: flex;
        align-items: center;
        .c_input {
          width: 90px;
        }
        /deep/.c_input .el-input__inner {
          padding: 0 5px;
          text-align: right;
        }
      }
    }

    .choices {
      margin-top: 10px;
      padding: 0 10px;
    }
    .choice {
      word-break: break-all;
      .choice_c {
        display: flex;
        flex-direction: row;
        align-items: baseline;
        span {
          /* margin-left: 10px; */
          cursor: pointer;
          font-size: 14px;
        }
        .el-checkbox {
          margin-top: 4px;
          margin-right: 10px;
        }
        .el-radio {
          margin-top: 4px;
          margin-right: 10px;
        }
        .right {
          color: #efa030;
        }
        .right::after {
          content: '（正确答案）';
        }
      }
      /deep/.choice_c .el-checkbox__label {
        display: none;
      }
      /deep/.choice_c .el-radio__label {
        display: none;
      }
    }
    /deep/.choice .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner::after {
      border-color: #fff !important;
    }
    /deep/.choice .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {
      background-color: #409eff !important;
      border-color: #409eff !important;
    }
    /deep/.choice .el-radio__input.is-disabled.is-checked .el-radio__inner {
      background-color: #409eff !important;
      border-color: #409eff !important;
    }
    /deep/.choice .el-radio__input.is-disabled.is-checked .el-radio__inner::after {
      background-color: #fff !important;
    }
    .choice + .choice {
      margin-top: 5px;
    }
  }
}
</style>