<template>
  <div v-show="show" class="edit-expected-position">
    <el-form ref="formRef" :model="form" :rules="rules" hide-required-asterisk label-position="top">
      <el-row :gutter="28">
        <el-col :span="12">
          <el-form-item label="学校名称" prop="FLearningName">
            <el-input v-model="form.FLearningName" size="large" placeholder="请输入学校名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="学制类型" prop="FTypeSchooling">
            <el-select
              v-model="form.FTypeSchooling"
              size="large"
              placeholder="请选择学制类型"
              style="width: 100%"
            >
              <el-option label="全日制" value="A" />
              <el-option label="非全日制" value="B" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学历" prop="FEducationBackground">
            <el-select
              v-model="form.FEducationBackground"
              size="large"
              placeholder="请选择学历"
              style="width: 100%"
            >
              <el-option
                v-for="item in informationData?.Degree2"
                :key="item.FNUMBER"
                :label="item.FDATAVALUE"
                :value="item.FNUMBER"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="专业" prop="FSpecialty">
            <el-input v-model="form.FSpecialty" size="large" placeholder="请输入专业" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="flx-align-center" style="display: flex; align-items: end">
          <el-form-item label="在校时间" prop="FStartTime">
            <el-date-picker
              v-model="form.FStartTime"
              type="month"
              placeholder="入学时间"
              size="large"
              :disabled-date="disabledDate"
            />
          </el-form-item>
          <span style="margin: 0 16px 28px">至</span>
          <el-form-item label="" prop="FEndTime">
            <el-date-picker
              v-model="form.FEndTime"
              type="month"
              placeholder="毕业时间"
              size="large"
              :disabled-date="disabledDate"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="在校经历(选填)">
            <div class="text-header">
              <el-icon size="28"><i-ep-MoreFilled /></el-icon>
            </div>
            <el-input
              v-model="form.FSchoolExperience_Tag"
              type="textarea"
              show-word-limit
              :maxlength="300"
              :rows="10"
              :placeholder="`1、在校担任职务... \n2、获得荣誉... \n3、所学主要课程...`"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="毕业设计/论文题目(选填)">
            <el-input
              v-model="form.FThesisTitle"
              size="large"
              placeholder="请输入毕业设计/论文题目"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="毕业设计/论文描述(选填)">
            <div class="text-header">
              <el-icon size="28"><i-ep-MoreFilled /></el-icon>
            </div>
            <el-input
              v-model="form.FThesisDescription_Tag"
              type="textarea"
              show-word-limit
              :maxlength="600"
              :rows="10"
              placeholder="请填写内容"
            />
          </el-form-item>
        </el-col>
        <el-col class="edit-resume-btn">
          <el-button size="large" style="width: 100px" @click="handleClose">取消</el-button>
          <el-button size="large" type="primary" style="width: 100px" @click="onConfirm"
            >完成</el-button
          >
          <el-icon
            v-show="form.FEntryID"
            color="#177CFA"
            style="margin-left: 30px"
            @click="delResume"
            ><i-ep-DeleteFilled
          /></el-icon>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { updateResume, deleteResume } from '@/services/api/recruitmentManagement'
defineProps({
  show: {
    type: Boolean,
    default: false
  },
  informationData: {
    type: Object,
    default: () => {}
  }
})

const emits = defineEmits(['update:show', 'init'])

const formRef = ref()
const form: any = ref({
  FEntryID: 0,
  FLearningName: '', //学校名称
  FTypeSchooling: '', //学制类型
  FEducationBackground: '', //学历
  FSpecialty: '', //专业
  FStartTime: '', //开始时间（日期）
  FEndTime: '', //结束时间（日期）
  FSchoolExperience: '', //无需填写
  FSchoolExperience_Tag: '', //在校经历
  FThesisTitle: '', //毕业设计/论文题目
  FThesisDescription: '', //无需填写
  FThesisDescription_Tag: '' //毕业设计/论文描述
})

const rules = {
  FLearningName: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
  FTypeSchooling: [{ required: true, message: '请输入学制类型', trigger: 'blur' }],
  FEducationBackground: [{ required: true, message: '请输入学历', trigger: 'blur' }],
  FSpecialty: [{ required: true, message: '请输入专业', trigger: 'blur' }],
  FStartTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
  FEndTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
}

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}

const filterChange = () => {
  let data = {
    Parmeter: {
      NeedUpDateFields: [
        'FLearningName',
        'FTypeSchooling',
        'FEducationBackground',
        'FSpecialty',
        'FStartTime',
        'FEndTime',
        'FSchoolExperience',
        'FSchoolExperience_Tag',
        'FThesisTitle',
        'FThesisDescription',
        'FThesisDescription_Tag'
      ],
      IsDeleteEntry: 'false',
      Model: {
        FID: 0,
        F_LQKJ_EducationalExperEntryS: [
          {
            FEntryID: 0,
            FLearningName: '', //学校名称
            FTypeSchooling: '', //学制类型
            FEducationBackground: {
              FNumber: ''
            }, //学历
            FSpecialty: '', //专业
            FStartTime: '', //开始时间（日期）
            FEndTime: '', //结束时间（日期）
            FSchoolExperience: '', //无需填写
            FSchoolExperience_Tag: '', //在校经历
            FThesisTitle: '', //毕业设计/论文题目
            FThesisDescription: '', //无需填写
            FThesisDescription_Tag: '' //毕业设计/论文描述
          }
        ]
      }
    }
  }

  data.Parmeter.Model.F_LQKJ_EducationalExperEntryS[0] = {
    ...data.Parmeter.Model.F_LQKJ_EducationalExperEntryS[0],
    ...form.value,
    FEducationBackground: { FNumber: form.value.FEducationBackground }
  }
  return data
}

const onConfirm = async () => {
  await formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      let data = filterChange()
      const res: any = await updateResume(data)
      if (res.code === 200) {
        ElNotification.success({
          title: `${form.value.FEntryID ? '更新' : '添加'}教育经历`,
          message: '更新成功'
        })
        handleClose()
        emits('init')
      }
    }
  })
}

const delResume = async () => {
  const res: any = await deleteResume({ Type: 'D', ID: form.value.FEntryID })
  if (res.code === 200) {
    handleClose()
    emits('init')
  }
}

const handleClose = () => {
  formRef.value?.resetFields()
  emits('update:show', false)
}

defineExpose({
  form
})
</script>

<style lang="scss" scoped>
.edit-expected-position {
  background: #f8f8f8;
  border-radius: 12px;
  padding: 24px;
  font-weight: 400;
  color: #222222;
  margin-top: 24px;
  .edit-user__info {
    font-size: 18px;
    margin-bottom: 24px;
  }

  .sex {
    display: flex;
    align-items: center;
    span {
      width: 227px;
      height: 38px;
      line-height: 38px;
      text-align: center;
      display: inline-block;
      background: #ffffff;
      border-radius: 8px;
      border: 1px solid #cccccc;
      margin-right: 15px;
      color: #999999;
      cursor: pointer;
    }
    .active {
      background: #dcecff;
      border: 1px solid #177cfa;
      color: #177cfa;
    }
  }

  .edit-resume-btn {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 10px;
  }

  .text-header {
    width: 100%;
    height: 44px;
    border-radius: 12px 12px 0px 0px;
    border: 1px solid #cccccc;
    border-bottom: 0;
    display: flex;
    align-items: center;
    .el-icon {
      transform: rotate(90deg);
      color: #d9d9d9;
    }
  }
  :deep(.el-textarea__inner) {
    border-radius: 0px 0px 12px 12px;
  }
}
</style>
