|
@@ -0,0 +1,130 @@
|
|
|
|
|
+<template>
|
|
|
|
|
+ <q-dialog ref="dialogRef" @hide="onDialogHide">
|
|
|
|
|
+ <q-card class="q-dialog-plugin" style="width: 700px; max-width: 90vw">
|
|
|
|
|
+ <DefaultDialogHeader :title="title" @close="onDialogCancel" />
|
|
|
|
|
+ <q-form ref="formRef" @submit="onOKClick">
|
|
|
|
|
+ <q-card-section class="row q-col-gutter-sm">
|
|
|
|
|
+ <!-- User -->
|
|
|
|
|
+ <UserSelect
|
|
|
|
|
+ v-model="selectedUser"
|
|
|
|
|
+ :label="$t('common.terms.user')"
|
|
|
|
|
+ :rules="[inputRules.required]"
|
|
|
|
|
+ :error="!!serverErrors?.user_id"
|
|
|
|
|
+ :error-message="serverErrors?.user_id"
|
|
|
|
|
+ :initial-id="client ? client.user_id : null"
|
|
|
|
|
+ class="col-12"
|
|
|
|
|
+ @update:model-value="serverErrors.user_id = null"
|
|
|
|
|
+ />
|
|
|
|
|
+
|
|
|
|
|
+ <!-- Document (CPF/CNPJ) -->
|
|
|
|
|
+ <q-input
|
|
|
|
|
+ v-model="form.document"
|
|
|
|
|
+ :mask="documentMask"
|
|
|
|
|
+ fill-mask
|
|
|
|
|
+ unmasked-value
|
|
|
|
|
+ :label="$t('client.fields.document')"
|
|
|
|
|
+ :rules="[inputRules.required, validateDocument]"
|
|
|
|
|
+ :error="!!serverErrors?.document"
|
|
|
|
|
+ :error-message="serverErrors?.document"
|
|
|
|
|
+ class="col-12"
|
|
|
|
|
+ @update:model-value="serverErrors.document = null"
|
|
|
|
|
+ />
|
|
|
|
|
+ </q-card-section>
|
|
|
|
|
+ <q-card-actions align="center">
|
|
|
|
|
+ <q-btn color="primary" label="Cancel" @click="onDialogCancel" />
|
|
|
|
|
+ <q-space />
|
|
|
|
|
+ <q-btn
|
|
|
|
|
+ color="primary"
|
|
|
|
|
+ label="OK"
|
|
|
|
|
+ :type="'submit'"
|
|
|
|
|
+ :loading="loading"
|
|
|
|
|
+ :disable="!hasUpdatedFields"
|
|
|
|
|
+ />
|
|
|
|
|
+ </q-card-actions>
|
|
|
|
|
+ </q-form>
|
|
|
|
|
+ </q-card>
|
|
|
|
|
+ </q-dialog>
|
|
|
|
|
+</template>
|
|
|
|
|
+
|
|
|
|
|
+<script setup>
|
|
|
|
|
+import { ref, useTemplateRef, watch, computed } from 'vue';
|
|
|
|
|
+import { useInputRules } from 'src/composables/useInputRules';
|
|
|
|
|
+import { useDialogPluginComponent } from 'quasar';
|
|
|
|
|
+import { useI18n } from 'vue-i18n';
|
|
|
|
|
+import { createClient, updateClient } from 'src/api/client';
|
|
|
|
|
+import { useFormUpdateTracker } from 'src/composables/useFormUpdateTracker';
|
|
|
|
|
+import { useSubmitHandler } from 'src/composables/useSubmitHandler';
|
|
|
|
|
+import { dynamicCpfCnpjMask, validateCpfCnpj } from 'src/helpers/utils';
|
|
|
|
|
+
|
|
|
|
|
+import DefaultDialogHeader from 'src/components/defaults/DefaultDialogHeader.vue';
|
|
|
|
|
+import UserSelect from 'src/components/user/UserSelect.vue';
|
|
|
|
|
+
|
|
|
|
|
+defineEmits([
|
|
|
|
|
+ ...useDialogPluginComponent.emits,
|
|
|
|
|
+]);
|
|
|
|
|
+
|
|
|
|
|
+const { client, title } = defineProps({
|
|
|
|
|
+ client: {
|
|
|
|
|
+ type: Object,
|
|
|
|
|
+ default: null,
|
|
|
|
|
+ },
|
|
|
|
|
+ title: {
|
|
|
|
|
+ type: Function,
|
|
|
|
|
+ default: () => useI18n().t('common.terms.title'),
|
|
|
|
|
+ },
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+const { t } = useI18n();
|
|
|
|
|
+const { inputRules } = useInputRules();
|
|
|
|
|
+
|
|
|
|
|
+const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
|
|
|
|
|
+ useDialogPluginComponent();
|
|
|
|
|
+
|
|
|
|
|
+const formRef = useTemplateRef('formRef');
|
|
|
|
|
+
|
|
|
|
|
+const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
|
|
|
|
|
+ user_id: client ? client?.user_id : null,
|
|
|
|
|
+ document: client ? client?.document : '',
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+const {
|
|
|
|
|
+ loading,
|
|
|
|
|
+ serverErrors,
|
|
|
|
|
+ execute: submitForm,
|
|
|
|
|
+} = useSubmitHandler({
|
|
|
|
|
+ onSuccess: () => onDialogOK(true),
|
|
|
|
|
+ formRef: formRef,
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+const selectedUser = ref(null);
|
|
|
|
|
+
|
|
|
|
|
+// Dynamic mask for CPF/CNPJ
|
|
|
|
|
+const documentMask = computed(() => {
|
|
|
|
|
+ return dynamicCpfCnpjMask(form.document);
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+// CPF/CNPJ validation
|
|
|
|
|
+const validateDocument = (val) => {
|
|
|
|
|
+ if (!val) return true;
|
|
|
|
|
+ return validateCpfCnpj(val) || t('validation.rules.cpf') + ' / ' + t('validation.rules.cnpj');
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const onOKClick = async () => {
|
|
|
|
|
+ let response;
|
|
|
|
|
+ if (client) {
|
|
|
|
|
+ await submitForm(() => {
|
|
|
|
|
+ response = updateClient(getUpdatedFields.value, client.id);
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ await submitForm(() => {
|
|
|
|
|
+ response = createClient({ ...form });
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ response;
|
|
|
|
|
+// if(response.data.success == false)
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+watch(selectedUser, () => {
|
|
|
|
|
+ form.user_id = selectedUser.value?.value;
|
|
|
|
|
+});
|
|
|
|
|
+</script>
|