<script setup lang="ts">
import { useMapStore } from '@/stores/map'
import type { CourseType } from '@/types'
import * as api from '../api'

const CourseViewChapter = defineAsyncComponent(() => import('../components/CourseViewChapter.vue'))
const CourseViewBBS = defineAsyncComponent(() => import('../components/CourseViewBBS.vue'))
const CourseViewWork = defineAsyncComponent(() => import('../components/CourseViewWork.vue'))
const CourseViewExam = defineAsyncComponent(() => import('../components/CourseViewExam.vue'))
const CourseViewAssess = defineAsyncComponent(() => import('../components/CourseViewAssess.vue'))
const CourseViewLive = defineAsyncComponent(() => import('../components/CourseViewLive.vue'))
const CourseViewMaterial = defineAsyncComponent(() => import('../components/CourseViewMaterial.vue'))

const { query } = useRoute()
const courseId = $ref<string | null>(query.course_id as string)
const semesterId = $ref<string | null>(query.semester_id as string)

let loading = $ref<boolean>(false)

const mapStore = useMapStore()
// 选课类型
const electiveTypes = mapStore.getMapValuesByKey('system_elective_type')
// 选课类型文本
const electiveTypeText = computed(() => {
  return electiveTypes.find(item => parseInt(item.value) === detail.elective_type)?.label || ''
})
const detail = reactive<CourseType>({
  id: '',
  course_id: '',
  represent: '',
  is_finished: 0,
  is_top: 0,
  name: '',
  course_alias_name: '',
  cover: '',
  elective_type: 0,
  online_type: 0,
  semester: { id: '', name: '' },
  credit: 0,
  previous_preparation: '',
  target: '',
  lecturers: []
})
// 获取课程信息
function fetchInfo() {
  if (!courseId || !semesterId) {
    return
  }
  loading = true
  api.getCourse({ course_id: courseId, semester_id: semesterId }).then(res => {
    Object.assign(detail, res.data.detail)
    loading = false
  })
}
onMounted(() => {
  fetchInfo()
})

const dialogInfo = reactive({
  visible: false,
  title: '',
  content: ''
})
function showInfo(title: string, content: string) {
  dialogInfo.visible = true
  dialogInfo.title = title
  dialogInfo.content = content
}
</script>

<template>
  <div class="course-view" v-loading="loading">
    <section class="course-view-hd">
      <div class="course-info">
        <h1>{{ detail.course_alias_name || detail.name }}</h1>
        <ul>
          <li>{{ electiveTypeText }}</li>
          <li>{{ detail.credit }}学分</li>
          <li v-if="detail.semester">{{ detail.semester.name }}</li>
        </ul>
        <el-button round @click="showInfo('课程简介', detail.represent)" :disabled="!detail.represent"
          >课程简介</el-button
        >
        <el-button
          round
          @click="showInfo('预备知识', detail.previous_preparation)"
          :disabled="!detail.previous_preparation"
          >预备知识</el-button
        >
        <el-button round @click="showInfo('授课目标', detail.target)" :disabled="!detail.target">授课目标</el-button>
      </div>
      <div class="course-lecturers" v-if="detail.lecturers?.length">
        <el-carousel
          :autoplay="false"
          indicator-position="none"
          :arrow="detail.lecturers.length > 1 ? 'always' : 'never'"
          height="126px">
          <el-carousel-item v-for="item in detail.lecturers" :key="item.id" class="lecturer-item">
            <img :src="item.avatar" class="lecturer-item__pic" />
            <div class="lecturer-item__info">
              <h2>{{ item.name }}</h2>
              <el-popover :width="400" trigger="hover" v-if="item.summarize">
                <template #reference>
                  <div
                    class="lecturer-item__content"
                    v-html="item.summarize"
                    style="height: 66px; overflow: hidden"></div>
                </template>
                <div class="lecturer-item__content" v-html="item.summarize"></div>
              </el-popover>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </section>
    <div class="course-view-bd">
      <el-tabs>
        <el-tab-pane label="学习">
          <CourseViewChapter />
        </el-tab-pane>
        <el-tab-pane label="论坛" lazy>
          <CourseViewBBS />
        </el-tab-pane>
        <el-tab-pane label="大作业" lazy>
          <CourseViewWork />
        </el-tab-pane>
        <el-tab-pane label="考试" lazy>
          <CourseViewExam />
        </el-tab-pane>
        <el-tab-pane label="课程考核" lazy>
          <CourseViewAssess />
        </el-tab-pane>
        <el-tab-pane label="直播" lazy>
          <CourseViewLive />
        </el-tab-pane>
        <el-tab-pane label="资料" lazy>
          <CourseViewMaterial />
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" width="472px" center>
    <div v-html="dialogInfo.content" class="dialog-info"></div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.course-view {
  height: 100%;
}
.course-view-hd {
  display: flex;
  padding: 20px;
  border-bottom: 1px solid #e6e6e6;
}
.course-info {
  flex: 1;
  h1 {
    font-size: 18px;
    font-weight: 500;
    line-height: 30px;
    color: #333333;
  }
  ul {
    margin-top: 20px;
    margin-bottom: 30px;
    display: flex;
  }
  li {
    font-size: 14px;
    font-weight: 400;
    line-height: 1;
    color: #bcbcbc;
  }
  li + li {
    padding-left: 10px;
    margin-left: 10px;
    border-left: 1px solid #bcbcbc;
  }
}

.course-lecturers {
  width: 444px;
  background: #f7f8fa;
  border-radius: 10px;
  :deep(.el-carousel__arrow) {
    background: none;
    color: #333;
    font-size: 20px;
  }
  :deep(.el-carousel__arrow--left) {
    left: 0;
  }

  :deep(.el-carousel__arrow--right) {
    right: 0;
  }
}
.lecturer-item {
  width: 364px;
  display: flex;
  justify-content: center;
  height: 106px;
  margin: 10px 40px;
  overflow: hidden;
}
.lecturer-item__pic {
  width: 90px;
  height: 106px;
  object-fit: cover;
  border-radius: 10px;
  overflow: hidden;
}
.lecturer-item__info {
  flex: 1;
  margin-left: 10px;
  padding-top: 10px;
  height: 100px;
  overflow: hidden;
  h2 {
    font-size: 16px;
    font-weight: 500;
    line-height: 27px;
    color: #333;
  }
  .lecturer-item__content {
    font-size: 14px !important;
    font-weight: 400;
    line-height: 24px !important;
    color: #666666;
    :deep(p) {
      font-size: 14px !important;
      line-height: 24px !important;
    }
  }
}

.course-view-bd {
  padding: 10px 20px;
}
.dialog-info {
  :deep(img) {
    max-width: 100%;
  }
}
</style>
