CviLux_fe/src/views/energyManagement/components/CarbonEmissionModal.vue

106 lines
2.4 KiB
Vue

<script setup>
import { inject, defineProps, watch, ref } from "vue";
import useFormErrorMessage from "@/hooks/useFormErrorMessage";
import { postAlertMember } from "@/apis/alert";
import * as yup from "yup";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const { openToast } = inject("app_toast");
const props = defineProps({
openModal: Function,
onCancel: Function,
editRecord: Object,
fetchData: Function,
});
let scheme = yup.object({
factor: yup.number().required(t("button.required")),
});
const form = ref(null);
const formState = ref({
factor: null,
});
const SaveCheckAuth = ref([]);
const { formErrorMsg, handleSubmit, handleErrorReset } = useFormErrorMessage(
scheme.value
);
watch(
() => props.editRecord,
(newValue) => {
if (newValue) {
formState.value = {
...newValue,
};
}
}
);
const onOk = async () => {
const values = await handleSubmit(scheme, formState.value);
const res = await postAlertMember({
...values,
});
if (res.isSuccess) {
props.fetchData();
closeModal();
} else {
openToast("error", res.msg, "#carbon_emission_item");
}
};
const closeModal = () => {
SaveCheckAuth.value = [];
handleErrorReset();
props.onCancel();
};
</script>
<template>
<button class="btn btn-sm btn-success ms-auto me-3" @click.stop.prevent="openModal">
{{ $t("button.edit") }}
</button>
<Modal
id="carbon_emission_item"
:title="t('energy.edit_carbon_emission')"
:open="open"
:onCancel="closeModal"
width="300"
>
<template #modalContent>
<form ref="form" class="mt-5 flex flex-col items-center">
<Input :value="formState" class="w-full" name="factor">
<template #topLeft>{{$t('energy.carbon_emission_coefficient')}}</template>
<template #bottomLeft>
<span class="text-error text-base">
{{ formErrorMsg.factor }}
</span>
</template>
</Input>
</form>
</template>
<template #modalAction>
<button
type="reset"
class="btn btn-outline-success mr-2"
@click.prevent="closeModal"
>
{{ $t("button.cancel") }}
</button>
<button
type="submit"
class="btn btn-outline-success"
@click.prevent="onOk"
>
{{ $t("button.submit") }}
</button>
</template>
</Modal>
</template>
<style lang="scss" scoped></style>