import { defineStore } from 'pinia' const emptyCase = () => ({ petId: null, name: null, type: null, gender: null, breed: null, age: null, weight: null, sterilized: null, }) export const useFastRequestStore = defineStore('FastRequestStore', { state: () => ({ endpoints: { create: '/dashboard/fast-requests/create', store: '/dashboard/fast-requests', }, step: 1, requestType: null, requestCase: emptyCase(), requestDetails: { priority: null, note: null, }, historyFiles: [], scanFiles: [], doctorId: null, pets: [], doctors: [], submitting: false, submittedDocument: null, }), getters: { selectedPet: (state) => state.pets.find((pet) => pet.id === state.requestCase.petId) || null, selectedDoctor: (state) => state.doctors.find((doctor) => doctor.id === state.doctorId) || null, canProceed: (state) => { switch (state.step) { case 1: return !!state.requestType case 2: return !!state.requestCase.petId || (!!state.requestCase.name && !!state.requestCase.type) case 3: return !!state.requestDetails.priority default: return true } }, }, actions: { async fetchCreateData() { const { data } = await window.axios.get(this.endpoints.create) this.pets = data.pets this.doctors = data.doctors }, selectArchivePet(petId) { this.requestCase = emptyCase() this.requestCase.petId = petId }, clearArchivePet() { this.requestCase.petId = null }, nextStep() { if (this.step < 5) this.step++ }, prevStep() { if (this.step > 1) this.step-- }, async submit() { this.submitting = true try { const formData = new FormData() formData.append('modality', this.requestType) formData.append('priority', this.requestDetails.priority) if (this.requestDetails.note) formData.append('note', this.requestDetails.note) if (this.requestCase.petId) { formData.append('pet_id', this.requestCase.petId) } else { Object.entries(this.requestCase).forEach(([key, value]) => { if (key !== 'petId' && value !== null && value !== '') { formData.append(`pet[${key}]`, key === 'sterilized' ? (value ? 1 : 0) : value) } }) } if (this.doctorId) formData.append('doctor_id', this.doctorId) this.historyFiles.forEach((file) => formData.append('history_files[]', file)) this.scanFiles.forEach((file) => formData.append('scan_files[]', file)) const { data } = await window.axios.post(this.endpoints.store, formData) this.submittedDocument = data.document return data.document } finally { this.submitting = false } }, reset() { this.$reset() }, }, })