<template>
  <view class="pa-3">
    <uni-forms ref="baseFormRef" v-model="baseFormData" validateTrigger="bind" :rules="rules" label-width="100px" label-align="right">
      <uni-forms-item label="姓名" required  name="name">
        <uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
      </uni-forms-item>
      <uni-forms-item label="联系手机号" required name="phone">
        <uni-easyinput v-model="baseFormData.phone" placeholder="请输入联系手机号" />
      </uni-forms-item>
      <uni-forms-item label="企业名称" required name="enterpriseName">
        <uni-easyinput v-model="baseFormData.enterpriseName" placeholder="请输入企业名称" />
      </uni-forms-item>
      <button type="primary" @click="submit" :disabled="loading">提交</button>
    </uni-forms>
  </view>
</template>

<script setup>
import { ref, unref } from 'vue'
import { huntSubmit } from '@/api/common'

const baseFormRef = ref()
const baseFormData = ref({
  name: null,
  phone: null,
  enterpriseName: null
})
const rules = {
  name: {
    rules: [{
      required: true,
      errorMessage: '姓名不能为空'
    }]
  },
  phone: {
    rules: [{required: true, errorMessage: '请输入手机号码' }]
  },
  enterpriseName: {
    rules: [{required: true, errorMessage: '请输入企业名称' }]
  }
}
const loading = ref(false)
const submit = async () => {
  const valid = await unref(baseFormRef).validate()
  if (!valid) return
  loading.value = true
  try {
    const { code, msg } = await huntSubmit(baseFormData.value)
    if (code !== 0) {
      loading.value = false
      uni.showToast({ title: msg, icon: 'none' })
      return
    }
    uni.showToast({ title: '提交成功，我们会尽快与您联系', icon: 'none', success: () => {
      setTimeout(() => {
        loading.value = false
        uni.navigateBack()
      }, 1000)
    } })
  } catch (error) {
    console.log('error', error)
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.pa-3 {
  padding: 30rpx;
}
</style>