<template>
  <view>
    <Navbar title="文件学习" />
    <div class="content">
      <div class="time" v-if="this.opt.studyProgress < 100">
        <div>倒计时：</div>
        <div>
          <u-count-down :time="30 * 1000" format="mm:ss" @finish="endFinish"></u-count-down>
        </div>
      </div>
      <div style="width: 100vw; height: 100vh">
        <bns-kkFile-preview :items="filePreview" :isStudy="true"></bns-kkFile-preview>
      </div>
    </div>
  </view>
</template>

<script>
import bnsKkFilePreview from '../component/bns-kkFile-preview'
import { updateProgress } from '@/api/educationTraining'
import { getLearningTaskDetail } from '@/api/educationTraining'

export default {
  components: {
    bnsKkFilePreview
  },
  data() {
    return {
      opt: {},
      userId: uni.getStorageSync('userInfo').userId,
      filePreview: {
        // filePreviewUrl: '2024/10/17/工作分析辅导新3(1)_20241017170902A129.pdf?preventDownload=true',
        filePreviewUrl: '',
        fileName: ''
      }
    }
  },
  onLoad(opt) {
    this.opt = JSON.parse(opt.params)
    this.filePreview.filePreviewUrl = this.opt.url
    console.log('🚀 ~ onLoad ~ this.opt:', this.opt)
    this.getDetail()
  },
  methods: {
    async getDetail() {
      console.log('🚀 ~ getDetail ~ this.opt:', this.opt)
      const res = await getLearningTaskDetail({ studyTaskId: this.opt.taskId })
      console.log('🚀 ~ getDetail ~ res:', res)
      // 查找 res.data 中 id 与 this.opt.coursewareId 相同的数据
      const data = res.data.find(item => item.id == this.opt.coursewareId)
      console.log('🚀 ~ getDetail ~ data:', data)
      if (data && data.studyProgress == 100) {
        this.opt.studyProgress = 100
      }
    },
    async endFinish() {
      const params = {
        taskId: this.opt.taskId,
        type: this.opt.type,
        coursewareId: this.opt.coursewareId,
        userUuid: this.userId,
        studyTime: 30,
        studyProgress: 100,
        taskLength: this.opt.taskLength
      }
      console.log('用户点击确定', params)
      if (this.opt.studyProgress < 100) {
        uni.showToast({
          title: '已完成学习',
          icon: 'none',
          duration: 1500
        })
        const res = await updateProgress(params)
        console.log('🚀 ~ endStudy ~ 结束学习:', res)
        this.opt.studyProgress = 100
      }
    }
  }
}
</script>

<style lang="scss">
.time {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  text-align: center;
  padding: 10px;
  font-size: 16px;
  color: #333;
}
</style>
