import { defineComponent, onMounted, onUnmounted, reactive, ref, nextTick } from 'vue';
import styles from './index.module.less';
import { useRoute, useRouter } from 'vue-router';
import { useInterval, useIntervalFn } from '@vueuse/core';
import 'tcplayer.js/dist/tcplayer.css';
import { _initVideo } from './initVideo'

export default defineComponent({
  name: 'intention-questionnaire-show',
  setup() {
    // 页面定时
    const pageTimer = useInterval(1000, { controls: true });
    pageTimer.pause();
    const router = useRouter();
    const route = useRoute();
    const forms = reactive({
      loading: true,
      code: null,
      openId: '' as any,
      isPageHide: false,
      contentA:
        '', // 第一段
      contentB:
        '', // 第二段
      contentC:
        '',
      contentD:
        '',
      contentE: null as any,  
      introductionVideo: "https://oss.dayaedu.com/ktyq/03/1742283035204.mp4",
      introductionVideoTime: 117,
      coverImg: "https://oss.dayaedu.com/ktyq/02/1739362815061.png",
      introductionVideo2: "https://oss.dayaedu.com/ktyq/02/1739345326291.mp4",
      introductionVideoTime2: 111,
      coverImg2: "https://oss.dayaedu.com/ktyq/02/1739324215341.png",
      player1: null as any,
      player2: null as any,
      player1Speed: 1,
      player2Speed: 1,
      videoLoading1: true,
      videoLoading2: true,
      meetingType: 'primarySchoolNo' as 'primarySchoolNo' | 'primarySchoolYes' | 'juniorSchoolNo' | 'juniorSchool' | any,
      intentionInfo: null as any,
      contentShow: false,
    });

    const showPopup = ref(false);
    const showPopupMessage = ref('');

    // 播放视频总时长
    const videoIntervalRef = useInterval(1000, { controls: true });
    videoIntervalRef.pause();

    const videoIntervalRef2 = useInterval(1000, { controls: true });
    videoIntervalRef2.pause();

    const getMessage = (ev: any) => {
      if (ev.data.api === 'parent-agenda') {
        forms.contentShow = true
        // console.log('消息',ev.data)
        forms.contentA = ev.data.message.contentA || forms.contentA
        forms.contentB = ev.data.message.contentB || forms.contentB
        forms.contentC = ev.data.message.contentC || forms.contentC
        forms.contentD = ev.data.message.contentD || forms.contentD
        forms.contentE = ev.data.message.contentE || forms.contentE || null;
        console.log('消息',forms.contentD)
      }
    }

    onMounted(async () => {
      forms.meetingType = route.query.type || forms.meetingType;
      nextTick(() => {
        // 是否加载完成
        window.parent &&
        window.parent.postMessage(
          {
            api: 'onLoad',
            status: true
          },
          '*'
        )
        // const videoRef: any = document.querySelector('#register-video')
        // const videoRef2: any = document.querySelector('#register-video2')
        // if(videoRef) {
        //   const rect = videoRef?.getBoundingClientRect()
        //   console.log(rect)
        //   videoRef.style.height = rect.width / 16 * 9 + 'px'
        // }
        // if(videoRef2) {
        //   const rect = videoRef2?.getBoundingClientRect()
        //   console.log(rect)
        //   videoRef2.style.height = rect.width / 16 * 9 + 'px'
        // }
      })
      _initVideo('one', forms, videoIntervalRef)
      _initVideo('two', forms, videoIntervalRef2)
      window.addEventListener('message', getMessage)
    });

    // const nextSkip = () => {
    //   router.push({
    //     path: '/fill-questionnaire',
    //     query: {
    //       openId: forms.openId, //
    //       meetingType: forms.meetingType
    //     }
    //   });
    // };
    const onPageShow = () => {
      console.log(forms.isPageHide, 'showInfo');
      if (forms.isPageHide) {
        window.location.reload();
      }
    };
    // 处理监听页面返回不刷新的问题
    window.addEventListener('pageshow', onPageShow);

    const onPageHide = () => {
      console.log(forms.isPageHide, 'showInfo');
      forms.isPageHide = true;
    };
    window.addEventListener('pagehide', onPageHide);

    onUnmounted(() => {
      window.removeEventListener('pageshow', onPageShow);
      window.removeEventListener('pagehide', onPageHide);
      window.removeEventListener('message', getMessage)
    });
    return () => (
      <div class={styles['intention-page']}>
        <div class={[styles['content-box'], !forms.contentShow && styles.hideContent]}>

          <div class={styles.contentBody} v-html={forms.contentA}></div>
          <div class={styles.contentBody}>
            <div class={styles.cbTitle} style="font-size:15px;">
              <strong>一、数字化音乐课堂定位</strong>
              <p style="text-align: left; line-height: 2;">
                <span style="font-size:15px; color: #333;">以“科技赋能美育”为理念，通过“课堂教学+课后Ai练习”的模式，帮助学生掌握乐器技能，提高学生专注力及审美能力。</span>
              </p>
            </div>
            <div class={styles.videoBoxCon}>
              <div class={styles.videoBox}>
                <div class={[styles['video-content']]}>
                  <video
                    id="register-video"
                    class={styles['video']}
                    src={forms.introductionVideo}
                    playsinline={true}
                    poster={forms.coverImg}
                    preload="auto"></video>
                </div>
              </div>
            </div>
          </div>
          <div class={styles.contentBody} v-html={forms.contentB}></div>
          <div class={styles.contentBody}>
            <div v-html={forms.contentC}></div>
            <div class={styles.videoBoxCon}>
              <div class={styles.videoBox}>
                <div class={[styles['video-content']]}>
                  <video
                    id="register-video2"
                    class={styles['video']}
                    src={forms.introductionVideo2}
                    playsinline={true}
                    poster={forms.coverImg2}
                    preload="auto"></video>
                </div>
              </div>
            </div>            
          </div>

          {/* <div class={styles.contentBody} v-html={forms.contentD}></div>
          <div class={styles.contentBody}>
            <div class={styles.cbTitle}>五、什么是器乐数字 Ai<span>（详见视频介绍）</span></div>
            <div class={styles.videoBoxCon}>
              <div class={styles.videoBox}>
                <div class={[styles['video-content']]}>
                  <video
                    id="register-video2"
                    class={styles['video']}
                    src={forms.introductionVideo2}
                    playsinline={true}
                    poster={forms.coverImg2}
                    preload="auto"></video>
                </div>
              </div>
            </div>
          </div>           */}

          {/* <div class={styles.contentBody} v-html={forms.contentA}></div>  
          <div class={styles.contentBody}>
            <div v-html={forms.contentB}></div>
            <div class={styles.videoBoxCon}>
              <div class={styles.videoBox}>
                <div class={[styles['video-content']]}>
                  <video
                    id="register-video"
                    class={styles['video']}
                    src={forms.introductionVideo}
                    playsinline={true}
                    poster={forms.coverImg}
                    preload="auto"></video>
                </div>
              </div>
            </div>            
          </div>
          <div class={styles.contentBody} v-html={forms.contentC}></div>
          <div class={styles.contentBody}>
            <div v-html={forms.contentD}></div>
            <div class={styles.videoBoxCon}>
              <div class={styles.videoBox}>
                <div class={[styles['video-content']]}>
                  <video
                    id="register-video2"
                    class={styles['video']}
                    src={forms.introductionVideo2}
                    playsinline={true}
                    poster={forms.coverImg2}
                    preload="auto"></video>
                </div>
              </div>
            </div>            
          </div> */}


        </div>

        {/* 是否在微信中打开 */}
        {/* <OWxTip /> */}


        {/* <div class={styles.bottomBtn}>
          <p><i>如有意参加，请点击下一步进行意见</i></p>
          <img class={styles.nextBtn} src={nextBtn} onClick={nextSkip} />
        </div> */}
      </div>
    );
  }
});
