import repoApi from "~/apis/repoApi"; import { mapState } from "pinia"; export const propertyMixin = { mounted() { this.httpService = useNuxtApp()["$http"]; }, data() { return { uploading: false, errors: {}, httpService: undefined, }; }, computed: { ...mapState("jahat", ["files"]), ...mapState("common", ["mediaAssetsUrl", "mediaBaseUrlGetter"]), }, methods: { getPropertyValueFull(entity, key) { let value = undefined; value = entity[key]; return value; }, getPropertyValue(entity, key, index) { let value = undefined; if (key in entity) { if (entity[key] instanceof Array) { if (index > -1 && index < entity[key].length) value = entity[key][index]; } else value = entity[key]; } else { value = entity[key]; } return value; }, getKey() { let key = this.$route.meta.slug; if (this.data?.tabKey) key = this.data.tabKey; // qmodel else if (this.$route.params.key) key = this.$route.params.key; else if (this.$route.query.key) key = this.$route.query.key; return key; }, saveProperty(value = undefined, isFullEdit = false) { if (value === undefined) value = this.value; if (value == undefined || !this.data) { this.mySwalToast({ title: "خطا", html: response.result, icon: "error", }); return; } let formData = { [this.data.keyName]: value, }; // setting payload type according to the selected good/bad | favorite/unfavorite checkbox. if ( this.data.keyName == "favorite" || this.data.keyName == "unfavorite" ) { if (this.goodBadType) { formData = { favorite: value, }; } else { formData = { unfavorite: value, }; } } let vm = this; // // "public/{{index_key}}/edit/{{entity_id}}/{{property}}/id/{{prop_id}}", // let url = ""; // if (isFullEdit) { // url = repoApi.public.updateProperty_full; // } else if (this.data.index != -1) { // if (value.id) { // url = repoApi.public.updateProperty_byid.replace( // "{{prop_id}}", // value.id // ); // } else { // url = repoApi.public.updateProperty_byindex.replace( // "{{index}}", // this.data.index // ); // } // } else url = repoApi.public.updateProperty; // url = url.replace("{{index_key}}", this.getKey()); // url = url.replace("{{entity_id}}", this.data.entity.id); // url = url.replace("{{property}}", this.data.keyName); let url = repoUrl() + repoApi.public.updateProperty_full; url = url.replace("{{index_key}}", this.$route.params.key); url = url.replace("{{entity_id}}", this.data?.entity?.id); url = url.replace("{{property}}", this.data?.keyName); this.httpService.postRequest(url, formData).then((response) => { if (response.status == 0) { // this.entity = response._sourde; this.$emit("change", { data: formData, keyName: vm.data.keyName, index: vm.data.index, type: "update", entity: response._source, }); } else this.mySwalToast({ title: "خطا", html: response.message, icon: "error", }); }); }, // saveQuestion(value = undefined) { // if (this.uploading) return; // this.uploading = true; // if (value === undefined) value = this.value; // const formData = { // [this.data.keyName]: { // title: value, // }, // }; // if (value == undefined || !this.data) { // this.mySwalToast({ // title: "خطا", // html: response.result, // icon: "error", // }); // return; // } // let vm = this; // let url = ""; // if (this.data.index != -1) { // if (value.id) { // url = repoApi.public.updateProperty_byid.replace('{{prop_id}}', value.id) // } // else { // url = repoApi.public.updateProperty_byindex.replace('{{index}}', this.data.index) // } // } // else // url = repoApi.public.updateProperty // url= url.replace('{{index_key}}', this.getKey()); // url= url.replace('{{entity_id}}', this.data.entity.id); // url= url.replace('{{property}}', this.data.keyName); // this.httpService.postRequest(url, formData).then((response) => { // this.uploading = false; // if (response.result == "updated") // this.mySwalToast({ // title: response.result, // html: "", // }); // // this.$emit("change", { // // data: formData, // // keyName: vm.data.keyName, // // index: vm.data.index, // // entity: response._source, // // }); // else // this.mySwalToast({ // title: "خطا", // html: response.result, // icon: "error", // }); // this.closeModal(); // }); // }, saveFile(questions) { if (this.uploading) return; this.uploading = true; let url = repoUrl()+ repoApi.public.upload; url = url.replace("{{index_key}}", this.getKey()); const formData = new FormData(); // formData.append('file', files) formData.append("file", this.$refs["file-uploader"].currentFile); this.httpService .postRequest(url, formData, { headers: { "Content-Type": "multipart/form-data", }, }) .then((response) => { this.value.file = response.file; // this.value.file_name = response.file; this.mySwalToast({ title: "فایل با موفقیت بارگزاری شد.", html: "", }); }) .catch((err) => {}) .finally(() => { this.uploading = false; }); }, deleteProperty(value = undefined) { if (value === undefined) value = this.value; if (value == undefined || !this.data) { this.mySwalToast({ title: "خطا", html: response.result, icon: "error", }); return; } this.mySwalConfirm({ title: "هشدار!!!", html: `از حذف این مورد اطمینان دارید؟ `, icon: "warning", }).then((result) => { if (result.isConfirmed) { let url = ""; //برای خصوصیتهای آرایه ای if (this.data.index != -1) { if (value.id) { url = repoUrl()+repoApi.public.deleteProperty_byid.replace( "{{prop_id}}", value.id ); } else { url = repoUrl()+repoApi.public.deleteProperty_byindex.replace( "{{index}}", this.data.index ); } } // کل خصوصیت else url = repoUrl()+repoApi.public.deleteProperty; url = url.replace("{{index_key}}", this.getKey()); url = url.replace("{{entity_id}}", this.data.entity.id); url = url.replace("{{property}}", this.data.keyName); const formData = { // برای لاگ ساده تر [this.data.keyName]: value, }; let vm = this; this.httpService .postRequest(url, formData) .then((response) => { this.$emit("change", { data: formData, keyName: vm.data.keyName, index: vm.data.index, type: "delete", entity: response._source, }); this.mySwalToast({ title: response.message, html: null, icon: "success", }); }) .catch((err) => { this.mySwalToast({ title: "خطا", html: err.message, icon: "error", }); }); } }); }, closeModal() { this.$emit("close-modal"); }, }, };