/*
 * @Author: chris 510148846@qq.com
 * @Date: 2023-08-30 15:04:53
 * @LastEditors: chris 510148846@qq.com
 * @LastEditTime: 2023-08-30 16:26:34
 * @FilePath: \gh-express\src\views\home\useForm.js
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { reactive, ref } from "vue";
// import { objectMerge } from "@/utils";

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

  // 校验表单
  function validateForm() {
    return formRef.value.validate((status, fields) => {
      return status ? Promise.resolve(status) : Promise.reject(fields);
    });
  }

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

  return { form, validateForm, resetForm };
}

export default useForm;
