import { reactive } from 'vue'

function useForm(formRef, defaultForm) {
  const form = reactive({ ...defaultForm })

  // 校验表单
  function validateForm() {
		return new Promise((resolve, reject) => {
			formRef.value.validate((failList, fields) => {
				console.log(failList, fields)
			  return (failList && failList.length) ? reject(failList) : resolve(true)
			})
		})
    
  }

  // 重置表单
  function resetForm(customForm) {
    const rForm = customForm || defaultForm
    form.value = { ...rForm }
    formRef.resetFields()
  }

  return { form, validateForm, resetForm }
}

export default useForm
