<template>
  <div class="admin-container">
    <div class="container">
      <div class="content">
        <div class="title">添加管理员</div>
        <el-form
          ref="formRef"
          :model="state.form"
          :rules="state.rules"
          label-width="100px"
          label-position="left"
          hide-required-asterisk
        >
          <el-form-item label="姓名：" prop="FUserName">
            <el-input v-model="state.form.FUserName" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="手机号：" prop="FUserPhoneS">
            <el-input v-model="state.form.FUserPhoneS" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="职位：" prop="FJobTitle">
            <el-input v-model="state.form.FJobTitle" placeholder="请输入职位" />
          </el-form-item>
        </el-form>
      </div>
      <div class="confirm">
        <span @click="submit">确定</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { addAdmin } from '@/services/api/recruitmentManagement'

const router = useRouter()

const state = reactive({
  form: {
    FUserName: '',
    FUserPhoneS: '',
    FJobTitle: ''
  },
  rules: {
    FUserPhoneS: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      {
        pattern: /^1[3|4|5|6|7|8][0-9]\d{8}$/,
        message: '手机号码格式不正确',
        trigger: 'blur'
      }
    ],
    FUserName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    FJobTitle: [{ required: true, message: '请输入职位', trigger: 'blur' }]
  }
})

const formRef = ref()

const submit = async () => {
  formRef.value.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      ElMessageBox.confirm('确定添加管理员?', '添加管理员', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let data = {
            Parmeter: {
              IsDeleteEntry: 'false',
              Model: {
                FID: 0, //无需填写
                F_LQKJ_EnterpriseUserEntity: [
                  {
                    FEntryID: 0,
                    FUserPhoneS: '', //手机号
                    FUserName: '', //名称
                    FJobTitle: '' //职位
                  }
                ]
              }
            }
          }
          data.Parmeter.Model.F_LQKJ_EnterpriseUserEntity[0] = {
            ...data.Parmeter.Model.F_LQKJ_EnterpriseUserEntity[0],
            ...state.form
          }
          console.log(data.Parmeter.Model)

          const res: any = await addAdmin(data)
          if (res.code === 200) {
            ElNotification({
              title: '添加管理员',
              message: res.msg,
              type: 'success'
            })
            router.back()
          }
        })
        .catch(() => {})
    }
  })
}
</script>

<style lang="scss" scoped>
.admin-container {
  display: flex;
  justify-content: center;
  margin: 24px 0 46px;

  .container {
    width: 1316px;

    .content {
      padding: 24px 36px;
      background: #ffffff;
      box-shadow: 0px 1px 25px 0px rgba(0, 0, 0, 0.05);
      border-radius: 8px;
      display: flex;
      align-items: center;
      flex-direction: column;
      .title {
        font-size: 24px;
        font-weight: 500;
        color: #177cfa;
        text-align: center;
        margin-bottom: 16px;
      }

      .el-form {
        width: 600px;
        margin-top: 20px;
      }
    }

    .confirm {
      display: flex;
      justify-content: center;
      text-align: center;
      span {
        display: block;
        font-size: 20px;
        font-weight: 500;
        color: #ffffff;
        margin-top: 36px;
        width: 160px;
        height: 52px;
        line-height: 52px;
        background: linear-gradient(90deg, #177cfa 0%, #177cfa 100%);
        border-radius: 8px;
        cursor: pointer;
      }
    }
  }
}
</style>
