<template>
  <view>
    <Navbar title="学习任务详情" :goTo="'/pages/learningTasks/index'" />
    <div class="content">
      <div class="title-wrapper">
        <div class="title">{{ opt.name }}</div>
        <!-- <div class="period">合格标准：{{ standard }}</div> -->
        <div class="period">学习期限：{{ opt.learningDate }}</div>
        <div class="list-wrapper">
          <div
            v-for="(item, index) in studyList"
            :key="index"
            style="max-height: 200px; overflow: auto"
            @click="goToDocLeaning(item)"
          >
            <ListItem :itemData="item" />
          </div>

          <div @click="goToExercise">
            <ListItem
              v-if="exerciseObj.id"
              :itemData="exerciseObj"
              background="#F1FFED"
              :showProgress="false"
              icon="/static/images/imgs/lianxi.png"
            />
          </div>

          <div @click="goToExam">
            <ListItem
              v-if="examObj.id"
              :itemData="examObj"
              background="#FDF4F2"
              :showProgress="false"
              icon="/static/images/imgs/zhihuikaoshi.png"
            />
          </div>
        </div>
        <div style="height: 30px;"></div>
      </div>
    </div>
  </view>
</template>

<script>
import ListItem from './components/ListItem'
import { getLearningTaskDetail } from '@/api/educationTraining'

export default {
  components: { ListItem },
  data() {
    return {
      opt: {},
      // 学习列表
      studyList: [],
      // 练习
      exerciseObj: {},
      // 考试
      examObj: {}
    }
  },
  onShow() {
    this.$nextTick(() => {
      this.getDetail()
    })
  },
  onLoad(opt) {
    this.opt = JSON.parse(opt.params)
    console.log('🚀 ~ onLoad ~ this.opt:', this.opt)
    // this.getDetail()
  },
  methods: {
    // 获取详情
    async getDetail() {
      this.studyList = []
      this.exerciseObj = {}
      this.examObj = {}
      console.log('🚀 ~ getDetail ~ this.opt:', this.opt)
      const res = await getLearningTaskDetail({ studyTaskId: this.opt.id })
      console.log('🚀 ~ getDetail ~ res:', res)
      this.exerciseObj = {
        ...res.data.find(item => item.type == '练习'),
        title: '练习'
      }
      this.examObj = {
        ...res.data.filter(item => item.type == '考试')[0],
        title: '考试'
      }
      console.log('🚀 ~ getDetail ~ this.examObj:', this.examObj)
      this.studyList = res.data.filter(item => item.type == '课件')
      this.studyList.forEach(item => {
        item.filePath = item.filePath.replace(/\\/g, '/')
        item.url = item.filePath
        // 如果url 是.MP4视频文件 item.title = '视频' 否则 item.title = '文件'
        if (item.url.includes('.mp4') || item.url.includes('.MP4')) {
          item.title = '视频'
        } else {
          item.title = '文件'
        }
      })
      console.log('🚀 ~ getDetail ~ this.studyList:', this.studyList)
    },
    goToDocLeaning(item) {
      console.log('🚀 ~ goToDocLeaning ~ item:', item, this.examObj)
      const params = {
        taskId: this.opt.id,
        coursewareId: item.id,
        duration: item.duration || 0,
        learningProgress: item.learningProgress || 0,
        url: item.url,
        studyProgress: item.studyProgress || 0,
        taskLength: this.studyList.length,
        singId: item.singId,
        type: this.examObj?.id ? '' : '1'
      }
      if (item.title == '视频') {
        uni.navigateTo({
          url: '/pages/learningTasks/videoLearning?params=' + JSON.stringify(params)
        })
      } else {
        uni.navigateTo({
          url: '/pages/learningTasks/documentLearning?params=' + JSON.stringify(params)
        })
      }
    },
    // 点击练习
    goToExercise() {
      console.log('练习', this.exerciseObj)
      if (this.exerciseObj.id) {
        uni.navigateTo({
          url: '/pages/myExercise/exercises?params=' + JSON.stringify(this.exerciseObj)
        })
      }
    },
    // 点击考试
    goToExam() {
      console.log('考试', this.examObj)
      if (Number(this.examObj.score) > Number(this.examObj.passScore)) {
        uni.showToast({
          title: '已通过考试',
          icon: 'none'
        })
        return
      }
      if (this.examObj.id) {
        const params = {
          ...this.examObj,
          name: this.examObj.fileName,
          isStudyTask: true,
          taskId: this.opt.id,
          type: this.studyList.length > 0 ? '' : '1'
        }
        uni.navigateTo({
          url: '/pages/myExam/beforeExamExplain?params=' + JSON.stringify(params)
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  padding: 0 20px;
  line-height: 1.5;

  .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
  }

  .period {
    font-size: 12px;
    color: #8a8a8a;
  }

  .collapse {
    margin-top: 30px;
  }
  .list-wrapper {
    margin: 20px 0;
  }
}
</style>
