<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="FProjectName">
            <el-input v-model="form.FProjectName" size="large" placeholder="请输入项目名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="项目角色" prop="FProjectRole">
            <el-input v-model="form.FProjectRole" size="large" placeholder="请输入项目角色">
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="项目链接(选填)" prop="">
            <el-input v-model="form.FProjectLink" 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="FProjectStartDate">
            <el-date-picker
              v-model="form.FProjectStartDate"
              type="month"
              placeholder="项目开始时间"
              size="large"
              :disabled-date="disabledDate"
            />
          </el-form-item>
          <span style="margin: 0 16px 28px">至</span>
          <el-form-item label="" prop="FProjectEndDate">
            <el-date-picker
              v-model="form.FProjectEndDate"
              type="month"
              placeholder="项目结束时间"
              size="large"
              :disabled-date="disabledDate"
              :shortcuts="shortcuts"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="项目描述" prop="FProjectDescription_Tag">
            <div class="text-header">
              <el-icon size="28"><i-ep-MoreFilled /></el-icon>
            </div>
            <el-input
              v-model="form.FProjectDescription_Tag"
              type="textarea"
              show-word-limit
              :maxlength="1600"
              :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
  }
})

const form = ref({
  FEntryID: 0,
  FProjectStartDate: '', //开始时间
  FProjectEndDate: '', //结束时间
  FProjectName: '', //项目名称
  FProjectDescription: '', //无需填写
  FProjectDescription_Tag: '', //项目描述
  FProjectPerformance: '', //无需填写
  FProjectPerformance_Tag: '', //项目业绩
  FProjectLink: '', //跳转链接
  FProjectRole: '' //项目角色
})
const shortcuts = [
  {
    text: '至今',
    value: new Date(new Date().getFullYear(), new Date().getMonth())
  }
]

const formRef = ref()

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

const rules = {
  FProjectStartDate: [{ required: true, message: '请输入开始时间', trigger: 'blur' }],
  FProjectEndDate: [{ required: true, message: '请输入结束时间', trigger: 'blur' }],
  FProjectName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
  FProjectDescription_Tag: [{ required: true, message: '请输入项目描述', trigger: 'blur' }],
  FProjectRole: [{ required: true, message: '请输入项目角色', trigger: 'blur' }]
}

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

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

const filterChange = () => {
  let data = {
    Parmeter: {
      NeedUpDateFields: [
        'FProjectStartDate',
        'FProjectEndDate',
        'FProjectName',
        'FProjectDescription',
        'FProjectDescription_Tag',
        'FProjectPerformance',
        'FProjectPerformance_Tag',
        'FProjectLink',
        'FProjectRole'
      ],
      IsDeleteEntry: 'false',
      Model: {
        FID: 0,
        F_LQKJ_ProjectExperienceEntityS: [
          {
            FEntryID: form.value.FEntryID ? form.value.FEntryID : 0,
            FProjectStartDate: '', //开始时间
            FProjectEndDate: '', //结束时间
            FProjectName: '', //项目名称
            FProjectDescription: '', //无需填写
            FProjectDescription_Tag: '', //项目描述
            FProjectPerformance: '', //无需填写
            FProjectPerformance_Tag: '', //项目业绩
            FProjectLink: '', //跳转链接
            FProjectRole: '' //项目角色
          }
        ]
      }
    }
  }
  data.Parmeter.Model.F_LQKJ_ProjectExperienceEntityS[0] = {
    ...data.Parmeter.Model.F_LQKJ_ProjectExperienceEntityS[0],
    ...form.value
  }

  return data
}

const onConfirm = async () => {
  console.log('’kkkk')

  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 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>
