<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: []
})

let infoText = $ref('')
let infoIndex = $ref(0)
// 获取课程信息
function fetchInfo() {
  if (!courseId || !semesterId) {
    return
  }
  loading = true
  api.getCourse({ course_id: courseId, semester_id: semesterId }).then(res => {
    Object.assign(detail, res.data.detail)
    infoText = detail.represent
    loading = false
  })
}
onMounted(() => {
  fetchInfo()
})

const dialogInfo = reactive({
  visible: false,
  title: '',
  content: ''
})
function showInfo(content: string, index: number) {
  infoText = content
  infoIndex = index
  // dialogInfo.visible = true
  // dialogInfo.title = title
  // dialogInfo.content = content
}

const courseViewChapterRef = ref()
function handleStudy() {
  courseViewChapterRef.value.study()
}
</script>

<template>
  <div class="course-view" v-loading="loading">
    <div class="course-view-h">
      <div class="view-h-l">
        <img src="https://webapp-pub.oss-cn-beijing.aliyuncs.com/pages/campbellsville/20220804/lc23925.png" />
        <div class="btn" @click="handleStudy">立即学习</div>
      </div>
      <div class="course-view-r">
        <div class="name">{{ detail.course_alias_name || detail.name }}</div>
        <div class="tag-box">
          <div class="tag">{{ electiveTypeText }}</div>
          <div class="tag">{{ detail.credit }}学分</div>
          <div class="tag" v-if="detail.semester">{{ detail.semester.name }}</div>
        </div>
        <div class="view-content">
          <div class="view-content-hd">
            <div :class="infoIndex === 0 ? 'btn active' : 'btn'" @click="showInfo(detail.represent, 0)">课程简介</div>
            <div :class="infoIndex === 1 ? 'btn active' : 'btn'" @click="showInfo(detail.previous_preparation, 1)">
              预备知识
            </div>
            <div :class="infoIndex === 2 ? 'btn active' : 'btn'" @click="showInfo(detail.target, 2)">授课目标</div>
          </div>
          <div class="view-content_c" v-if="infoText === ''">
            <el-empty :image-size="50" style="padding: 0" />
          </div>
          <div class="view-content_c" v-html="infoText" v-else></div>
        </div>
      </div>
    </div>
    <!-- <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>
      </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-content">
      <div class="course-view-bd">
        <el-tabs>
          <el-tab-pane label="学习">
            <CourseViewChapter ref="courseViewChapterRef" />
          </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 class="course-view-teacher">
        <div class="teacher-hd">课程讲师</div>
        <div class="teacher-content" v-for="item in detail.lecturers" :key="item.id">
          <img :src="item.avatar" class="lecturer-item__pic" />
          <div class="name">{{ item.name }}</div>
          <div class="dec" v-html="item.summarize"></div>
        </div>
      </div>
    </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-h {
  background: #ab0a3d;
  box-shadow: 0px 1px 12px 1px rgba(0, 0, 0, 0.1098);
  border-radius: 10px 10px 10px 10px;
  margin-left: 20px;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  .view-h-l {
    margin-right: 20px;
    width: 243px;
    img {
      height: 151px;
      width: 100%;
      display: block;
      border-radius: 6px 6px 6px 6px;
    }
    .btn {
      width: 243px;
      background: #ffffff;
      border-radius: 6px 6px 6px 6px;
      text-align: center;
      font-size: 14px;
      font-weight: 400;
      color: #ab0a3d;
      line-height: 37px;
      margin-top: 10px;
      cursor: pointer;
    }
  }
  .course-view-r {
    width: 100%;
    .name {
      font-size: 18px;
      font-weight: bold;
      color: #ffffff;
      line-height: 100%;
      margin-bottom: 6px;
    }
    .tag-box {
      display: flex;
      .tag {
        padding: 0 9px;
        line-height: 23px;
        background: #e6b6c4;
        border-radius: 4px 4px 4px 4px;
        font-size: 12px;
        color: #ab0a3d;
        margin-right: 10px;
      }
    }
    .view-content {
      background-color: #fff;
      min-height: 140px;
      border-radius: 9px 9px 9px 9px;
      margin-top: 10px;
      overflow: hidden;
      .view-content_c {
        padding: 12px 15px;
        font-size: 12px;
        color: #535353;
      }
      .view-content-hd {
        background: #f7e6ec;
        display: flex;
        .btn {
          line-height: 33px;
          padding: 0 18px;
          font-size: 14px;
          color: #666666;
          cursor: pointer;
          &.active {
            color: #ba143e;
            background-color: #fff;
          }
        }
      }
    }
  }
}
.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: 146px;
  height: 106px;
  object-fit: cover;
  display: block;
  border-radius: 10px;
  overflow: hidden;
  margin: 18px auto;
}
.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 {
  flex: 1;
  padding: 10px 20px;
  background-color: #fff;
  border-radius: 10px 10px 10px 10px;
}
.dialog-info {
  :deep(img) {
    max-width: 100%;
  }
}
.course-view-content {
  display: flex;
  padding-left: 20px;
  margin-top: 20px;
  .course-view-teacher {
    width: 217px;
    margin-left: 20px;
    border-radius: 10px 10px 10px 10px;
    background-color: #fff;
    padding: 0 13px 52px;
    height: fit-content;
    .teacher-hd {
      line-height: 55px;
      text-align: center;
      position: relative;
      border-bottom: 1px solid #eaeaea;
      font-size: 16px;
      font-weight: bold;
      color: #aa1941;
      &::after {
        content: '';
        position: absolute;
        bottom: -1px;
        left: 50%;
        transform: translateX(-50%);
        width: 84px;
        height: 2px;
        background: #aa1941;
      }
    }
    .teacher-content {
      .name {
        text-align: center;
        font-size: 16px;
        font-weight: bold;
        line-height: 100%;
        color: #333333;
        margin-bottom: 12px;
      }
      .dec {
        font-size: 14px;
        color: #666666;
        text-align: center;
      }
    }
  }
}
::v-deep {
  .el-empty__description {
    margin-top: 5px;
    font-size: 12px;
  }
}
</style>
