<template>
  <div>
    <n-drawer v-model:show="showModal" :width="dialogWidth">
      <n-drawer-content title="课程管理详情" closable>
        <n-spin :show="loading" description="请稍候...">
          <n-descriptions label-placement="left" class="py-2" :column="1">
            <n-descriptions-item>
              <template #label>
                课程名
              </template>
              {{ formValue.name }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                封面图
              </template>
              <n-image style="margin-left: 10px; height: 100px; width: 100px" :src="formValue.cover"/>
            </n-descriptions-item>
            <n-descriptions-item label="所属分类">
              <n-tag :type="dict.getType('lessonCategoryOption', formValue.categoryId)" size="small" class="min-left-space">
                {{ dict.getLabel('lessonCategoryOption', formValue.categoryId) }}
              </n-tag>
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                介绍视频
              </template>
              <div class="upload-card"  v-show="formValue.video !== ''" @click="download(formValue.video)">
                <div class="upload-card-item" style="height: 100px; width: 100px">
                  <div class="upload-card-item-info">
                    <div class="img-box">
                      <n-avatar :style="fileAvatarCSS">
                        {{ getFileExt(formValue.video) }}
                      </n-avatar>
                    </div>
                  </div>
                </div>
              </div>
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                所属学校
              </template>
              {{ formValue.school }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                课程概述
              </template>
              <span v-html="formValue.description"></span>
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                授课目标
              </template>
              {{ formValue.target }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                课程大纲
              </template>
              <span v-html="formValue.outline"></span>
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                预备知识
              </template>
              {{ formValue.prerequisite }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                参考资料
              </template>
              {{ formValue.reference }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                学时安排
              </template>
              {{ formValue.arrangement }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                开课时间
              </template>
              {{ formValue.startTime }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                结课时间
              </template>
              {{ formValue.endTime }}
            </n-descriptions-item>
            <n-descriptions-item>
              <template #label>
                常见问题
              </template>
              <span v-html="formValue.question"></span>
            </n-descriptions-item>
          </n-descriptions>
        </n-spin>
      </n-drawer-content>
    </n-drawer>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { View } from '@/api/lesson';
  import { State, newState } from './model';
  import { adaModalWidth } from '@/utils/hotgo';
  import { getFileExt } from '@/utils/urlUtils';
  import { useDictStore } from '@/store/modules/dict';

  const message = useMessage();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref(newState(null));
  const dialogWidth = computed(() => {
    return adaModalWidth(580);
  });
  const fileAvatarCSS = computed(() => {
    return {
      '--n-merged-size': `var(--n-avatar-size-override, 80px)`,
      '--n-font-size': `18px`,
    };
  });

  // 下载
  function download(url: string) {
    window.open(url);
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;
    loading.value = true;
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less" scoped></style>