<template>
  <view>
    <!-- 答题组件 -->
    <kz-question v-if="questions" mode="TRAINING" :title="cateName" :questions="questions" :questionCount="questionCount" :pageCount="pageCount"
                 :currentPage="currentPage" :viewMode="mode" @loadQuestion="getQuestion"></kz-question>

    <!-- toast提示 -->
    <tui-toast ref="toast"></tui-toast>
    <login ref="login" v-on:succ="ajax"></login>
  </view>
</template>

<script>
export default {
  data () {
    return {
      cateId: 0,
      cateName: '',
      questions: [],
      questionCount: 0,
      pageCount: 1000,
      currentPage: 0,
      lastPage: 0,
      mode: 'normal'
    }
  },
  onLoad (e) {
    this.cateId = e.cateId
    this.cateName = e?.cateName
    this.mode = e.mode
    this.getQuestion()
  },
  methods: {
    ajax(){
      this.getQuestion()
    },

    // 筛选options_json中value为空的题目ID
    getEmptyValueQuestionIds(dataArray) {
      if (!Array.isArray(dataArray)) {
        return ''
      }

      const emptyValueQuestions = dataArray.filter(item => {
        // 检查是否有options_json数组
        if (!item.options_json || !Array.isArray(item.options_json)) {
          return false
        }

        // 检查options_json中是否有任何选项的value为空
        return item.options_json.some(option => {
          const value = option.value
          // 检查value是否为空：null、undefined、空字符串或仅包含空白字符
          return  value.length==0 ||value === null ||
                 value === undefined ||
                 value === '' ||
                 (typeof value === 'string' && value.trim() === '')
        })
      })

      // 提取ID并用逗号连接
      const ids = emptyValueQuestions.map(item => item.id).filter(id => id !== undefined && id !== null)
      return ids.join(',')
    },
    // 获取试题（包括延迟获取）
    getQuestion (page = 1, callback = null) {
      console.log('getQuestion', page, this.currentPage, this.lastPage)
      // 避免重复获取
      if (page <= this.currentPage || (this.lastPage && page > this.lastPage)) {
        return
      }

      // 请求参数
      let params = {
        page: page,
        page_count: this.pageCount,
        cate_id: this.cateId,
        mode: this.mode,
        type: 'train'
      }

      this.http('question/train', params).then(res => {
		  

		  // 获取options_json中value为空的题目ID
		  // console.log(res.data.data);
		  // const emptyValueIds = this.getEmptyValueQuestionIds(res.data.data)
		  // if (emptyValueIds) {
		  //   console.log('value为空的题目ID:', emptyValueIds)
		  // }

        if (res.code == 1) {
          // 默认分页方式
          if (this.mode == 'normal') {
            this.questionCount = res.data.total
            this.currentPage = res.data.current_page
            this.lastPage = res.data.last_page
            this.questions = this.questions.concat(res.data.data)
          } else {
            // 记忆、随机模式暂时只能获取全部题目了
            this.questions = res.data.data
          }

          // 积分提示
          let point = res.data.point
          console.log('point', point)
          if (point?.get_point > 0) {
            this.$refs.toast.show({
              title: "积分+" + point.get_point,
              content: point.type,
              imgUrl: "/static/toast/info-circle.png",
              icon: true,
              duration: 4000
            })
          }
        } else {
          if (res.data.need_open_member) {
            uni.showModal({
              title: '提示',
              content: res.msg,
              confirmText: '前往开通',
              success: (res) => {
                if (res.confirm) {
                  this.utils.goto('/pages/user/member-center?from_train=1')
                } else if (res.cancel) {
                  this.utils.goback()
                }
              }
            });
          } else {
            this.utils.alert(res.msg, () => {
              this.utils.goback()
            })
          }
        }

        if (callback) {
          callback()
        }
      })
    }
  }
}
</script>

<style>

</style>
