<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      :title="formValue.id > 0 ? '编辑课程管理 #' + formValue.id : '添加课程管理'"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="课程名" path="name">
                  <n-input placeholder="请输入课程名" v-model:value="formValue.name" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="封面图" path="cover">
                  <UploadImage :maxNumber="1" v-model:value="formValue.cover" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="所属分类" path="categoryId">
                  <n-select v-model:value="formValue.categoryId" :options="dict.getOptionUnRef('lessonCategoryOption')" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="介绍视频" path="video">
                  <UploadFile :maxNumber="1" v-model:value="formValue.video" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="所属学校" path="school">
                  <n-input placeholder="请输入所属学校" v-model:value="formValue.school" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="课程概述" path="description">
                  <Editor style="height: 450px" id="description" v-model:value="formValue.description" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="授课目标" path="target">
                  <n-input placeholder="请输入授课目标" v-model:value="formValue.target" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="课程大纲" path="outline">
                  <Editor style="height: 450px" id="outline" v-model:value="formValue.outline" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="预备知识" path="prerequisite">
                  <n-input placeholder="请输入预备知识" v-model:value="formValue.prerequisite" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="参考资料" path="reference">
                  <n-input placeholder="请输入参考资料" v-model:value="formValue.reference" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="学时安排" path="arrangement">
                  <n-input placeholder="请输入学时安排" v-model:value="formValue.arrangement" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="开课时间" path="startTime">
                  <DatePicker v-model:formValue="formValue.startTime" type="datetime" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="结课时间" path="endTime">
                  <DatePicker v-model:formValue="formValue.endTime" type="datetime" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="常见问题" path="question">
                  <Editor style="height: 450px" id="question" v-model:value="formValue.question" />
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { useDictStore } from '@/store/modules/dict';
  import { Edit, View } from '@/api/lesson';
  import { State, newState } from './model';
  import UploadImage from '@/components/Upload/uploadImage.vue';
  import UploadFile from '@/components/Upload/uploadFile.vue';
  import Editor from '@/components/Editor/editor.vue';
  import DatePicker from '@/components/DatePicker/datePicker.vue';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { useMessage } from 'naive-ui';
  import { adaModalWidth } from '@/utils/hotgo';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);
  const dialogWidth = computed(() => {
    return adaModalWidth(840);
  });

  // 提交表单
  function confirmForm(e) {
    e.preventDefault();
    formRef.value.validate((errors) => {
      if (!errors) {
        formBtnLoading.value = true;
        Edit(formValue.value)
          .then((_res) => {
            message.success('操作成功');
            closeForm();
            emit('reloadTable');
          })
          .finally(() => {
            formBtnLoading.value = false;
          });
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  // 关闭表单
  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);

      return;
    }

    // 编辑
    loading.value = true;
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

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

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