<template>
  <div class="c_box">
    <!-- <div class="mask"></div> -->
    <div v-if="!checkJson">暂未设置题目</div>
    <div v-else class="choice_box">
      <!-- <div class="title"><div>{{ `（${option[checkJson.type].name}）` }}</div><div v-html="checkJson.title"></div></div> -->
      <div class="c_title">
        <div class="title">
          {{ tindex + 1 + '、' + `（${option[checkJson.type].name}）` + checkJson.title
          }}<span v-if="see" style="color: #efa030"
            >（{{ checkJson.answer ? '参考答案：' + checkJson.answer : '暂无参考答案' }}
            {{ cJson.score ? '分值：' + cJson.score + '分' : '' }}）</span
          >
          <span style="color: #efa030" v-if="checkJson.score && !see">（{{ '分值：' + checkJson.score + '分' }}）</span>
          <!-- </div><div v-html="checkJson.title"></div> -->
        </div>
        <div class="p_box" v-if="isTeacher == 1 && checkJson.score">
          <el-input v-model="checkJson.score2" class="c_input" @change="numberPan" placeholder="请输入得分"></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">
        <textarea
          :readonly="checktype == 2"
          rows="2"
          v-autoHeight="68"
          class="binfo_input binfo_textarea"
          cols
          v-model="checkJson.answer2"
          placeholder=""
        ></textarea>
      </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: '问答题' }
      },
      checkJson: undefined
    }
  },
  watch: {
    checkJson: {
      handler(newValue) {
        this.$emit('update:cJson', newValue)
      },
      deep: true
    },
    cJson: {
      handler(newValue) {
        if (newValue.answer2 !== this.checkJson.answer2) {
          this.checkJson = this.depthCopy(newValue)
          this.$forceUpdate()
        }
      },
      deep: true
    }
  },
  directives: {
    autoHeight: {
      update(el, binding) {
        const { value } = binding
        if (value && typeof value === 'number') {
          el.style.height = `${value}px`
        } else {
          el.style.height = 'auto'
        }
      },
      componentUpdated(el) {
        el.style.height = `${el.scrollHeight + 5}px`
      }
    }
  },
  methods: {
    depthCopy(s) {
      return JSON.parse(JSON.stringify(s))
    },
    numberPan() {
      if (/[^\d]/.test(this.checkJson.score2) || parseInt(this.checkJson.score2) < 0) {
        this.$message.error('请输入大于0的数字')
        this.checkJson.score2 = ''
      }

      if (parseInt(this.checkJson.score2) > parseInt(this.checkJson.score)) {
        this.$message.error('不能输入大于得分的数字')
        this.checkJson.score2 = this.checkJson.score
      }
    }
  },
  mounted() {
    this.checkJson = this.cJson ? this.depthCopy(this.cJson) : undefined
    if (this.checkJson.answer2) {
      setTimeout(() => {
        this.checkJson.answer2 += '*0*%*'
        setTimeout(() => {
          this.checkJson.answer2 = this.checkJson.answer2.replaceAll('*0*%*', '')
        }, 0)
      }, 100)
    }
  }
}
</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;
      .binfo_input {
        width: 100%;
        margin: 0;
        padding: 12px 14px;
        display: block;
        min-width: 0;
        outline: none;
        box-sizing: border-box;
        background: none;
        border: none;
        border-radius: 4px;
        background: #fff;
        font-size: 16px;
        resize: none;
        font-family: 'Microsoft YaHei';
        min-height: 48px;
        /* border: 1px solid #3682fc00; */
        border: 1.5px solid #cad1dc;
      }
      .binfo_textarea {
        border: 1.5px solid #cad1dc;
        font-size: 16px;
        resize: none;
        /* background: #f6f6f6; */
        font-family: 'Microsoft YaHei';
      }
      .binfo_input:focus-visible {
        border: 1.5px solid #3681fc !important;
      }
    }
  }
}
</style>
