import { computed, defineComponent, reactive, ref } from 'vue'
import styles from './newspaper-item.module.less'
import msgIcon from '@/school/images/msg-icon.png'
import sendmsgIcon from '@/school/images/sendmsg-icon.png'
import phoneIcon from '@/school/images/phone-icon.png'
import { postMessage } from '@/helpers/native-message'
import clockIcon from '@/school/attendance/images/clock-icon.png'
import { Icon, ActionSheet, Button } from 'vant'
import { useRouter } from 'vue-router'
import { unitData } from '../unit-edit-test/data'

export default defineComponent({
  props: ['item'],
  name: 'newspaper-item',

  setup(props) {
    const router = useRouter()
    const gotoDetail = () => {
      // window.open()
    }

    const wTotalScore = computed(() => {
      let countScore = 0
      if (props.item?.level) {
        const aQuestionList = unitData['level' + props.item.level]?.questionList || []
        countScore = props.item.totalScore
        aQuestionList.forEach((s: any) => {
          countScore += Number(s.question.totalScore) || 0
        })
        unitData['level' + props.item.level].totalScore = countScore
      }

      return countScore
    })

    // 重新计算题目数
    const wQuestionNum = computed(() => {
      let aQuestionList: any = []
      if (props.item?.level) {
        aQuestionList = unitData['level' + props.item.level]?.questionList || []
      }
      return (props.item?.questionNum || 0) + aQuestionList.length
    })

    // 合格分
    const wPassScore = computed(() => {
      let score = 0
      if (props.item?.level) {
        const level1 = unitData['level' + props.item.level]
        if (level1.passScore > 0) {
          score = level1.passScore
        } else {
          score = props.item?.passScore
        }
      }
      return score
    })

    // 测验时间
    const wTimeMinutes = computed(() => {
      let timeMinutes = 0
      if (props.item?.level) {
        const level1 = unitData['level' + props.item.level]
        if (level1.timeMinutes > 0) {
          timeMinutes = level1.timeMinutes
        } else {
          timeMinutes = props.item?.timeMinutes
        }
      }
      return timeMinutes
    })
    //
    const onEdit = () => {
      //
      router.push({
        path: '/unit-edit-test',
        query: {
          ...props.item
        }
      })
    }
    return () => (
      <div>
        <div class={styles.itemWrap} onClick={gotoDetail}>
          <div class={styles.titleWrap}>{props.item?.unitExaminationName}</div>
          <div class={styles.itemBottom}>
            <div class={[styles.itemBottomDot, 'gridBorderRight']}>
              <p class={styles.dotMain}>{wTotalScore.value || 0}</p>
              <p class={styles.dotSub}> 总分</p>
            </div>
            <div class={[styles.itemBottomDot, 'gridBorderRight']}>
              <p class={styles.dotMain} style={{ color: '#F67146' }}>
                {wPassScore.value || 0}
              </p>
              <p class={styles.dotSub}>合格分 </p>
            </div>
            <div class={styles.itemBottomDot}>
              <p class={styles.dotMain}>{wQuestionNum.value || 0}</p>
              <p class={styles.dotSub}>题目数量 </p>
            </div>
          </div>
          <div class={styles.uniTimeWrap}>
            <div class={styles.uniTimeWrapLeft}>
              <Icon name={clockIcon} class={styles.clockIcon}></Icon> 测验时长：
              <span style={{ color: '#F67146' }}>{wTimeMinutes.value || 0} </span> 分钟
            </div>

            <Button plain round class={styles.editBtn} onClick={onEdit}>
              编辑内容
            </Button>
          </div>
        </div>
      </div>
    )
  }
})
