
<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      class="custom-form"
    >
    <el-row>
      <el-col :span="12">
        <el-form-item label="姓名" prop="nickname" >
          <el-input v-model="formData.nickname" placeholder="自动链接" :disabled="!isSupervisor" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="职称" prop="title">
          <el-input v-model="formData.title" placeholder="" :disabled="!isSupervisor"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" >
        <el-form-item label="外聘导师" style="font-weight: 550;">
          <p></p>
        </el-form-item>
      </el-col>
      <el-col :span="24" >
        <el-form-item label="工作单位" prop="externalSupervisorWorkPlace">
          <el-input v-model="formData.externalSupervisorWorkPlace" placeholder="请输入工作单位" :disabled="!isSupervisor"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row  >
      <el-col :span="24">
        <el-form-item label="合作的校内导师" prop="supervisor">
          <el-input v-model="formData.supervisor" placeholder="自动链接" :disabled="!isSupervisor"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row  >
      <el-col :span="24">
        <el-form-item label="研究方向" prop="major">
          <el-input v-model="formData.major" placeholder="请输入研究方向" :disabled="!isSupervisor" />
        </el-form-item>
      </el-col>
    </el-row>
      <el-row >
        <!--        导师上传-->
        <el-col :span="24" v-if="userInfo.userType==='3'&&formType==='update'">
          <el-form-item label="个人简历上传" prop="introduction">
            <!-- <upload-file v-model="formData.introduction" /> -->
            <Editor v-model="formData.introduction" ref="editorRef" class="full-width-editor"/>
          </el-form-item>
        </el-col>
        <!--        学院和学生下载-->
        <el-col :span="24" v-if="userInfo.userType==='4'||userInfo.userType==='1'&&formType==='detail'">
          <el-form-item label="导师简历下载(PDF)" prop="introduction">
              <el-button
                v-if="formData.introduction"
                type="text"
                icon="el-icon-download"
                @click="handleDownload"
                class="custom-download-button"
              >
                下载导师简历
              </el-button>
              <span v-else>暂无文件</span>
          </el-form-item>
        </el-col>
      </el-row>
    <el-row  >
      <el-col :span="24">
        <el-form-item label="对研究生毕业时学术成果的要求" prop="studentAchievementRequirement">
          <el-input type="textarea" v-model="formData.studentAchievementRequirement" placeholder="未填写时，默认为按学校及学院发表学术成果的要求执行" :disabled="!isSupervisor" />
        </el-form-item>
      </el-col>
    </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="userInfo.userType==='3'">确 定</el-button>
      <el-button @click="dialogVisible = false" v-if="userInfo.userType==='3'" >取 消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { CommonStatusEnum } from '@/utils/constants'
import * as UserApi from '@/api/system/user'
import { FormRules } from 'element-plus'
import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
import {selectionBookVO,selectionBookApi} from '@/api/system/studentSelectSupervisorRecord/selectionBook'
import download from '@/utils/download'

defineOptions({ name: 'TeacherRequireForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  sex: undefined,
  supervisorId:undefined,
  nickname: '',
  title: '',//职称
  status: CommonStatusEnum.ENABLE,
  userNumber: '',//工号
  major:'',//研究方向
  studentAchievementRequirement:"",
  introduction:"",//简介
  externalSupervisorWorkPlace:""
})
const formRules = reactive<FormRules>({
})
const formRef = ref() // 表单 Ref
const deptList = ref<Tree[]>([]) // 树形结构

//获取登录人员信息
const userInfo = ref({} as ProfileVO)
const getUserInfo = async () => {
  const users = await getUserProfile()
  userInfo.value = users
}
// const isStudent = computed(() => userInfo.value.userType === "1");

  const open = async (type:string,supervisorId: number) => {
  dialogVisible.value = true
    formType.value=type
  if (formType.value == 'detail'){
    dialogTitle.value = '查看详情'
    resetForm()
    if (supervisorId) {
      formLoading.value = true
      try {
        const user = await UserApi.getUser(supervisorId)
        formData.value = {
          ...user,
          supervisorId: supervisorId, // 确保 supervisorId 也被设置
        }
        console.log(formData.value)
      } finally {
        formLoading.value = false
      }
    }
  } else if (formType.value == 'update'){
      dialogTitle.value = '填写互选表信息'
      resetForm()
      // 获取导师信息
        formLoading.value = true
        try {
          const user = await UserApi.getUser(userInfo.value.id)
          formData.value =user
        } finally {
          formLoading.value = false
        }
      }
}
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as selectionBookVO
    if (Array.isArray(data.introduction) && data.introduction.length > 0) {
      data.introduction = data.introduction[0]; // 只取数组的第一个元素
    }
    if (formType.value === 'update') {
      await selectionBookApi.updateSelectionBook(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const isSupervisor = computed(() => userInfo.value.userType === '3');

//下载PDF
const handleDownload = () => {
  console.log(formData.value.introduction)
  download.markdown(formData.value.introduction, '个人简历.pdf');
};


/** 重置表单 */
const resetForm = () => {
  formData.value = {
    supervisorId:undefined,
    nickname: '',
    id: undefined,
    sex: undefined,
    title: '',//职称
    status: CommonStatusEnum.ENABLE,
    userNumber: '',//工号
    major:'',//研究方向
    studentAchievementRequirement:"",
    introduction:"",//简介
    externalSupervisorWorkPlace:""
  }
  formRef.value?.resetFields()
}
onMounted(() => {
   getUserInfo()
})

</script>

<style scoped>
.custom-form {
  background-color: #f0f4f8; /* 浅灰色背景 */
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 轻微阴影 */
}
.custom-form .custom-download-button {
  color: #4b7bec; /* 灰蓝色字体颜色 */
  border: 1px solid #4b7bec; /* 灰蓝色边框 */
  background-color: transparent; /* 透明背景 */
  padding: 10px 20px; /* 按钮内边距 */
  border-radius: 4px; /* 圆角边框 */
  font-size: 16px; /* 字体大小 */
  transition: all 0.3s ease; /* 平滑过渡效果 */
  outline: none; /* 去除点击时的轮廓 */
}
.custom-form .custom-download-button:hover,
.custom-form .custom-download-button:focus {
  color: #fff; /* 悬浮时字体颜色 */
  background-color: #4b7bec; /* 悬浮时背景颜色 */
  border-color: #4b7bec; /* 悬浮时边框颜色 */
}
.custom-form .custom-download-button:active {
  background-color: skyblue; /* 点击时背景颜色 */
  border-color: #3a66b1; /* 点击时边框颜色 */
}
.full-width-editor {
  width: 100%;
  border: #e5e7ec;
  border-radius: 4px;
}
</style>
