
<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=isStudent />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="学号" prop="userNumber">
          <el-input v-model="formData.userNumber" placeholder="" :disabled=isStudent />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row >
      <el-col :span="12">
        <el-form-item label="专业" prop="major">
          <el-input v-model="formData.major" placeholder="" :disabled=isStudent />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入联系电话"/>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="个人简历上传(PDF)" prop="introduction">
          <upload-file v-model="formData.introduction" />
        </el-form-item>
      </el-col>
    </el-row>

    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="userInfo.userType==='1'">确 定</el-button>
      <el-button @click="dialogVisible = false" v-if="userInfo.userType==='1'" >取 消</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 {ref} from "vue";

import {selectionBookVO,selectionBookApi} from '@/api/system/studentSelectSupervisorRecord/selectionBook'

defineOptions({ name: 'StudentForm' })

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,
  nickname: '',
  userNumber: '',//学号
  major:'',//专业
  mobile:"",
  introduction:"",//简介PDF
})
const formRules = reactive<FormRules>({
  mobile: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  introduction: [{ required: true, message: '个人简历不能为空', trigger: 'blur' }],
})
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 () => {
  dialogVisible.value = true
  dialogTitle.value = '信息填写'
  formType.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]; // 只取数组的第一个元素
      }
      await selectionBookApi.updateSelectionBook(data)
      console.log(data)
      message.success("填写互选数据成功")
      dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    sex: undefined,
    nickname: '',
    userNumber: '',//学号
    major:'',//专业
    mobile:"",
    introduction:"",//简介PDF
  }
  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 .el-form-item {
  margin-bottom: 20px;
}
.custom-form .el-input,
.custom-form .el-textarea {
  width: 100%;
  border-color: #d9d9d9; /* 输入框边框颜色 */
}
.custom-form .el-form-item__label {
  color: #333; /* 标签文字颜色 */
}
.custom-form .el-input__inner,
.custom-form .el-textarea__inner {
  background-color: #fff; /* 输入框背景颜色 */
  color: #333; /* 输入框文字颜色 */
}
.custom-form .el-button {
  background-color: #6a1b9a; /* 按钮背景颜色 */
  border-color: #6a1b9a;
  color: #fff; /* 按钮文字颜色 */
}
.custom-form .el-button--primary {
  background-color: #4a148c; /* 主要按钮背景颜色 */
  border-color: #4a148c;
}
.custom-form .el-button:hover,
.custom-form .el-button--primary:hover {
  background-color: #5a0073; /* 按钮悬浮背景颜色 */
  border-color: #5a0073;
}
</style>
