126 lines
11 KiB
Vue
126 lines
11 KiB
Vue
<script setup>
|
||
import { computed, onMounted, reactive, ref } from 'vue';
|
||
import CustomInput from './ui/CustomInput.vue';
|
||
import States from './ui/States.vue';
|
||
import Cities from './ui/Cities.vue';
|
||
import Segments from './ui/Segments.vue';
|
||
import TruckTypes from './ui/TruckTypes.vue';
|
||
import Spiner from './ui/Spiner.vue';
|
||
import { validateEmail } from '../helpers/validations';
|
||
import { useAuthStore } from '../stores/auth';
|
||
import { useCompanyStore } from '../stores/company';
|
||
import Swal from 'sweetalert2';
|
||
import { useI18n } from 'vue-i18n';
|
||
|
||
const props = defineProps({
|
||
user: {
|
||
type: Object,
|
||
required: false
|
||
}
|
||
});
|
||
|
||
const { t } = useI18n();
|
||
onMounted(() => {
|
||
if(props.user) {
|
||
console.log(props.user)
|
||
userForm.name = props.user.first_name;
|
||
userForm.last_name = props.user.last_name;
|
||
userForm.email = props.user.email;
|
||
userForm.phone = props.user.phone;
|
||
// userForm.phone2 = props.user.phone2;
|
||
userForm.job_role = props.user.job_role;
|
||
userForm.categories = props.user.categories;
|
||
userForm.user_city = props.user.user_city?.map(m =>{
|
||
return { city_name: m };
|
||
});
|
||
userForm.user_state = props.user.user_state?.map(m =>{
|
||
return { state_name:m };
|
||
});
|
||
userForm.truck_type = props.user.truck_type?.map(m =>{
|
||
return { meta_value:m };
|
||
});
|
||
userForm.user_description = props.user.user_description;
|
||
} else {
|
||
Object.assign(userForm, initState);
|
||
}
|
||
})
|
||
|
||
const authStore = useAuthStore();
|
||
const companyStore = useCompanyStore();
|
||
|
||
const initState = {
|
||
name: '',
|
||
last_name: '',
|
||
email: '',
|
||
phone: '',
|
||
// phone2: '',
|
||
job_role: '',
|
||
categories: [],
|
||
user_city: [],
|
||
user_state: [],
|
||
truck_type: [],
|
||
user_description: '',
|
||
};
|
||
|
||
const userForm = reactive({
|
||
...initState
|
||
})
|
||
|
||
const errors = ref({
|
||
name: null,
|
||
last_name: null,
|
||
email: null,
|
||
phone: null,
|
||
})
|
||
|
||
const formRef = ref(null);
|
||
const loading = ref(false);
|
||
|
||
const title = computed(() => {
|
||
return (props.user) ? t('labels.editUser') : t('labels.createUser');
|
||
});
|
||
|
||
defineEmits(['reset-user']);
|
||
|
||
const saveUser = async() => {
|
||
validations()
|
||
if(errors.value.name || errors.value.last_name || errors.value.email || errors.value.phone){
|
||
return;
|
||
} else {
|
||
let userData ={
|
||
first_name: userForm.name,
|
||
last_name: userForm.last_name,
|
||
email: userForm.email,
|
||
phone: userForm.phone,
|
||
// phone2: userForm.phone2,
|
||
job_role: userForm.job_role,
|
||
permissions: authStore.user.permissions,
|
||
compan¼
|
||
|