import html from "html";
import { ref, reactive, watch } from "vue";
import AppFormItem from "./form-item.js";
export default {
components: { AppFormItem },
name: "AppForm",
template: html`
$t('confirm')
`,
props: ["modelValue", "schema", "action", "hideButton", "isQueryForm", "mode"],
emits: ["update:modelValue", "submit"],
setup(props, context) {
// init
const model = reactive(props.modelValue);
watch(model, (value) => {
context.emit("update:modelValue", value);
});
// ref
const formRef = ref(null);
const loading = ref(false);
//
const errors = ref({});
// reset
const reset = () => {
formRef.value.resetFields();
};
// validate
const validate = async () => {
return formRef.value.validate();
};
// submit
const submit = async () => {
try {
//const valid = await validate();
//if (valid) {
loading.value = true;
context.emit("submit", (serverErrors) => {
if (serverErrors) {
errors.value = serverErrors;
}
});
//}
} catch (error) {
console.error(error);
} finally {
loading.value = false;
}
};
// expose
context.expose({ validate, reset });
return {
model,
formRef,
loading,
errors,
reset,
submit,
};
},
};