import { defineComponent } from 'vue'
import Steps from './create-components/steps'
import { createState } from './create-components/createState'
import Course from './create-components/course'
import CoursePlan from './create-components/course-plan'
import CourseStart from './create-components/course-start'
import Detail from './create-components/detail'
import { Popup, Sticky } from 'vant'
import Arrange from './create-components/arrange'
import request from '@/helpers/request'
import styles from './create.module.less'
import ColHeader from '@/components/col-header'
import TheSticky from '@/components/the-sticky'
import iconQuestion from '@/teacher/practice-class/tip-model/images/icon-question.png'
import TipModel from '../practice-class/tip-model'

export default defineComponent({
  name: 'LiveCreate',
  data() {
    return {
      tipStatus: false
    }
  },
  async mounted() {
    try {
      // 获取手续费和分钟数
      const config = await request.get(
        '/api-teacher/sysConfig/queryByParamNameList',
        {
          params: {
            paramNames: 'live_service_rate,live_time_setting'
          }
        }
      )
      const configData = config.data || []
      configData.forEach((item: any) => {
        if (item.paramName === 'live_time_setting') {
          const mins = item.paramValue ? JSON.parse(item.paramValue) : []
          const tempArr = [] as any
          mins.forEach((item: any) => {
            tempArr.push({
              ...item,
              name: item.courseMinutes
            })
          })
          createState.minutes = [...tempArr]
        }
        if (item.paramName === 'live_service_rate') {
          createState.rate = item.paramValue
        }
      })

      const teacher = await request.post('/api-teacher/teacher/querySubject')
      createState.subjectList = teacher.data || []
    } catch (err: any) {
      console.log(err)
    }
    this.getLiveClassDetail()
  },
  methods: {
    // 获取直播课详情
    async getLiveClassDetail() {
      const groupId = this.$route.query.groupId
      if (!groupId) return
      const res = await request.get(
        `/api-teacher/courseGroup/queryLiveCourseInfo?groupId=${groupId}`
      )
      console.log(res, createState)
      if (res.code == 200) {
        const data = res.data
        createState.live.courseGroupId = data.courseGroupId
        createState.live.teacherId = data.teacherId
        createState.live.name = data.courseGroupName
        createState.live.subjectId =
          (
            createState.subjectList.find(
              (n: any) => n.name === data.subjectName
            ) as any
          )?.id || ''
        createState.live.courseIntroduce = data.courseIntroduce
        createState.live.courseNum = data.courseNum
        createState.live.singleMins = data.singleCourseMinutes
        createState.live.coursePrice = data.coursePrice
        createState.live.coursePlanList = data.planList
        createState.live.salesStartDate = data.salesStartDate
        createState.live.salesEndDate = data.salesEndDate
        createState.live.mixStudentNum = data.mixStudentNum
        createState.live.backgroundPic = data.backgroundPic
      }
    }
  },
  render() {
    return (
      <div class={styles['live-create']}>
        <TheSticky position="top">
          <ColHeader border={false} v-slots={{
                right: () => (
                  <img
                    src={iconQuestion}
                    class={styles.iconQuestion}
                    onClick={() => (this.tipStatus = true)}
                  />
                )
              }} />
          {createState.active !== 5 && (
            <Steps
              style={{ backgroundColor: '#f6f8f9', paddingBottom: '12px' }}
            />
          )}
        </TheSticky>

        {createState.active === 1 && <Course />}

        {createState.active == 2 && <CoursePlan />}

        {createState.active == 3 && <Arrange />}

        {createState.active == 4 && <CourseStart />}

        {createState.active == 5 && <Detail />}

        <Popup
          show={this.tipStatus}
          class={styles.tipPopup}
          closeable
          onClose={() => (this.tipStatus = false)}
        >
          <TipModel
            onClose={() => (this.tipStatus = false)}
            title={'什么是直播课？'}
            content={
              '直播课是现代教育领域中一种广受欢迎的课程形式，它集实时互动、多媒体展示和高度便利性于一体，为学习者带来了独特且丰富的学习体验。特别是在管乐直播课中，教师可以通过播放经典音乐作品，加深学生对音乐之美的感知与理解。对于那些需要具体操作演示的教学内容，直播课能够提供清晰直观的视角，让教师的每一个动作细节都展现在学生面前，确保学习效果。直播课程的内容围绕特定主题精心设计，旨在满足不同学习者的需求，促进知识与技能的有效传递。'
            }
          />
        </Popup>
      </div>
    )
  }
})
