2025-07-23 16:57:31 +03:30
|
|
|
import { ref, reactive, computed } from 'vue';
|
|
|
|
|
import { cloneDeep } from 'lodash-es';
|
2025-08-24 22:15:30 +03:30
|
|
|
import { aq as supabaseService } from './server.mjs';
|
2025-07-23 16:57:31 +03:30
|
|
|
|
|
|
|
|
const getOneFeature = async (id) => supabaseService.fetch("attributes", "select", {
|
|
|
|
|
columns: "*, attribute_values(*)",
|
|
|
|
|
field: "id",
|
|
|
|
|
value: id
|
|
|
|
|
});
|
|
|
|
|
const previewImg = ref();
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
const priceType = ref(1);
|
|
|
|
|
let temp = [];
|
|
|
|
|
const defaultVariant = ref();
|
|
|
|
|
const myAttributeValue = ref([]);
|
|
|
|
|
const myAttributes = ref([]);
|
|
|
|
|
const mediaFilesUrl = ref([]);
|
|
|
|
|
const original = ref({ url: void 0, index: 0 });
|
|
|
|
|
const originalFile = ref();
|
|
|
|
|
ref();
|
|
|
|
|
const items = ref([]);
|
|
|
|
|
const variants = ref([]);
|
|
|
|
|
const selectedFeatures = ref([]);
|
2025-08-24 22:15:30 +03:30
|
|
|
ref([]);
|
2025-08-06 17:55:47 +03:30
|
|
|
const formFields = reactive(
|
|
|
|
|
{
|
|
|
|
|
title_fa: "",
|
|
|
|
|
title: "",
|
|
|
|
|
price: 0,
|
|
|
|
|
body: "",
|
|
|
|
|
is_show: true,
|
|
|
|
|
is_active: true,
|
|
|
|
|
box_size: 0,
|
|
|
|
|
sell_type: "numeric",
|
|
|
|
|
original_image: void 0,
|
|
|
|
|
product_attributes: [],
|
|
|
|
|
files: [],
|
|
|
|
|
price_type: "manual",
|
|
|
|
|
auto_calculate: false,
|
|
|
|
|
sku: null,
|
|
|
|
|
created_at: void 0,
|
|
|
|
|
updated_at: void 0,
|
|
|
|
|
deleted_at: null,
|
|
|
|
|
count_order: void 0,
|
|
|
|
|
count_fav: void 0,
|
|
|
|
|
count_view: void 0,
|
|
|
|
|
min_price: void 0,
|
|
|
|
|
max_price: void 0,
|
|
|
|
|
type: "simple",
|
|
|
|
|
owner_id: void 0,
|
|
|
|
|
users: void 0,
|
|
|
|
|
saved: [],
|
|
|
|
|
variants: [],
|
|
|
|
|
code: void 0,
|
|
|
|
|
brand_id: void 0,
|
|
|
|
|
// brand: undefined,
|
|
|
|
|
collection_id: void 0,
|
|
|
|
|
discount: void 0,
|
|
|
|
|
discount_type: void 0,
|
|
|
|
|
// main_slider: false,
|
|
|
|
|
// bottom_slider: false,
|
|
|
|
|
id: void 0,
|
|
|
|
|
count: void 0,
|
|
|
|
|
display_price: void 0,
|
|
|
|
|
// weight: null,
|
|
|
|
|
meta: void 0,
|
|
|
|
|
seo_title: void 0,
|
|
|
|
|
description: void 0,
|
|
|
|
|
product_type: void 0,
|
|
|
|
|
weight: void 0,
|
|
|
|
|
rial_fee: void 0,
|
|
|
|
|
percent_fee: void 0
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-08-04 18:56:56 +03:30
|
|
|
const baseFormFields = cloneDeep(formFields);
|
|
|
|
|
function resetFormFields() {
|
|
|
|
|
Object.assign(formFields, baseFormFields);
|
|
|
|
|
myAttributes.value = [];
|
|
|
|
|
myAttributeValue.value = [];
|
|
|
|
|
variants.value = [];
|
|
|
|
|
priceType.value = 1;
|
|
|
|
|
original.value = { url: void 0, index: 0 };
|
|
|
|
|
mediaFilesUrl.value = [];
|
|
|
|
|
defaultVariant.value = void 0;
|
2025-08-06 17:55:47 +03:30
|
|
|
previewImg.value = void 0;
|
2025-08-04 18:56:56 +03:30
|
|
|
}
|
2025-07-23 16:57:31 +03:30
|
|
|
const files = computed(() => {
|
|
|
|
|
let tempMedia = cloneDeep(mediaFilesUrl.value);
|
2025-08-19 21:38:30 +03:30
|
|
|
tempMedia?.splice(0, 0, original.value);
|
|
|
|
|
tempMedia = tempMedia?.map((value, index) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
return value;
|
|
|
|
|
});
|
|
|
|
|
console.log(tempMedia);
|
|
|
|
|
return tempMedia;
|
|
|
|
|
});
|
|
|
|
|
function addToVariants() {
|
|
|
|
|
let attrIds = [];
|
|
|
|
|
let attrNames = [];
|
2025-08-19 21:38:30 +03:30
|
|
|
myAttributeValue.value?.forEach((attributeValue) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
if (!attributeValue.valueId) ;
|
|
|
|
|
attrIds.push(attributeValue.valueId);
|
|
|
|
|
attrNames.push(attributeValue.valueName);
|
|
|
|
|
});
|
|
|
|
|
console.log(attrIds, myAttributeValue.value);
|
2025-08-19 21:38:30 +03:30
|
|
|
formFields.variants?.push({
|
2025-07-23 16:57:31 +03:30
|
|
|
id: void 0,
|
|
|
|
|
attribute_values: [],
|
|
|
|
|
attribute_values_name: [],
|
|
|
|
|
price: 0,
|
|
|
|
|
weight: 0,
|
|
|
|
|
stock: 0,
|
|
|
|
|
auto_calculate: false,
|
|
|
|
|
discount_type: void 0,
|
|
|
|
|
discount: void 0,
|
|
|
|
|
is_default: false,
|
|
|
|
|
sku: ""
|
|
|
|
|
});
|
2025-08-13 20:31:16 +03:30
|
|
|
console.log(variants, formFields.variants);
|
2025-07-23 16:57:31 +03:30
|
|
|
}
|
|
|
|
|
function updateMyAttrs(newValue, flag) {
|
|
|
|
|
let tempAttributes = newValue;
|
|
|
|
|
priceType.value = 1;
|
|
|
|
|
myAttributes.value = [];
|
|
|
|
|
myAttributeValue.value = [];
|
|
|
|
|
console.log(newValue);
|
|
|
|
|
console.log("why");
|
|
|
|
|
let tempIndex = 0;
|
2025-08-13 20:31:16 +03:30
|
|
|
let notShowInVarIndexArray = [];
|
2025-08-19 21:38:30 +03:30
|
|
|
newValue?.forEach((x, index) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
console.log(x, index);
|
|
|
|
|
if (x.show_in_variable) {
|
|
|
|
|
priceType.value = 2;
|
2025-08-13 20:31:16 +03:30
|
|
|
if (formFields.variants.length === 0)
|
|
|
|
|
addToVariants();
|
2025-07-23 16:57:31 +03:30
|
|
|
myAttributes.value.push({
|
|
|
|
|
attributeId: x.id,
|
|
|
|
|
name: x.name,
|
|
|
|
|
values: x.new_feature,
|
|
|
|
|
show: x.show_in_variable,
|
|
|
|
|
valueKey: index
|
|
|
|
|
});
|
2025-08-19 21:38:30 +03:30
|
|
|
x.new_feature?.forEach((val, i) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
myAttributeValue.value.push({
|
|
|
|
|
attributeName: x.name,
|
|
|
|
|
attributeId: x.id,
|
|
|
|
|
valueId: val.id,
|
|
|
|
|
valueName: val.value,
|
|
|
|
|
valueKey: tempIndex
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2025-08-13 20:31:16 +03:30
|
|
|
notShowInVarIndexArray.push(index);
|
2025-08-19 21:38:30 +03:30
|
|
|
variants.value?.forEach((val) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
console.log("val.attribute_values>", index);
|
|
|
|
|
console.log(val.attribute_values);
|
|
|
|
|
console.log("myAttributes.value.length " + tempAttributes.length);
|
|
|
|
|
console.log("val.attribute_values.length " + val.attribute_values.length);
|
|
|
|
|
if (tempAttributes.length > val.attribute_values.length && val.attribute_values[index] != null) {
|
|
|
|
|
if (val.attribute_values.length != index) {
|
|
|
|
|
val.attribute_values.splice(index, 0, null);
|
|
|
|
|
val.attribute_values_name.splice(index, 0, null);
|
|
|
|
|
} else {
|
|
|
|
|
val.attribute_values = [...val.attribute_values, null];
|
|
|
|
|
val.attribute_values_name = [...val.attribute_values_name, null];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log("val.attribute_values After slice>" + index);
|
|
|
|
|
console.log(val.attribute_values);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
tempIndex++;
|
2025-08-19 21:38:30 +03:30
|
|
|
variants.value?.forEach((val, ind) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
if (tempAttributes.length > val.attribute_values.length && val.attribute_values[index] != null) {
|
|
|
|
|
val.attribute_values = [...val.attribute_values, null];
|
|
|
|
|
val.attribute_values_name = [...val.attribute_values_name, null];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log("variants+++");
|
|
|
|
|
});
|
|
|
|
|
console.log("tempIndexArray");
|
2025-08-13 20:31:16 +03:30
|
|
|
console.log(notShowInVarIndexArray);
|
2025-07-23 16:57:31 +03:30
|
|
|
sortAttributeByValueKey(variants.value);
|
2025-08-13 20:31:16 +03:30
|
|
|
if (notShowInVarIndexArray.length != 0) {
|
2025-08-19 21:38:30 +03:30
|
|
|
variants.value?.forEach((val) => {
|
2025-08-13 20:31:16 +03:30
|
|
|
notShowInVarIndexArray.forEach((y) => {
|
|
|
|
|
val.attribute_values[y] = null;
|
|
|
|
|
val.attribute_values_name[y] = null;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-23 16:57:31 +03:30
|
|
|
}
|
|
|
|
|
function sortAttributeByValueKey(x) {
|
|
|
|
|
console.log("444444", x);
|
2025-08-19 21:38:30 +03:30
|
|
|
x?.forEach((val, ind) => {
|
2025-07-23 16:57:31 +03:30
|
|
|
temp = cloneDeep(val.attribute_values);
|
|
|
|
|
if (myAttributes.value.length <= temp.length) {
|
2025-08-19 21:38:30 +03:30
|
|
|
temp?.forEach((x2, i) => {
|
|
|
|
|
myAttributeValue.value?.forEach((item, index) => {
|
|
|
|
|
if (temp.find((e) => e == item?.valueId) && val.attribute_values[item.valueKey] != temp.find((e) => e == item.valueId)) {
|
2025-07-23 16:57:31 +03:30
|
|
|
val.attribute_values[item.valueKey] = temp.find((e) => e == item.valueId);
|
|
|
|
|
val.attribute_values_name[item.valueKey] = item.valueName;
|
|
|
|
|
}
|
2025-08-19 21:38:30 +03:30
|
|
|
console.log(val?.attribute_values + "AFTER CHANGE ITEM" + ind);
|
2025-07-23 16:57:31 +03:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (ind == 0) {
|
|
|
|
|
defaultVariant.value = showDefaultVal(val.attribute_values_name);
|
|
|
|
|
}
|
|
|
|
|
if (val.is_default) {
|
|
|
|
|
console.log("val.attribute_values", val.attribute_values_name);
|
|
|
|
|
defaultVariant.value = showDefaultVal(val.attribute_values_name);
|
|
|
|
|
console.log("defaultVariant", defaultVariant.value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
function showDefaultVal(val) {
|
|
|
|
|
console.log(val, defaultVariant.value);
|
|
|
|
|
let res = "";
|
|
|
|
|
val.forEach((x, index) => {
|
|
|
|
|
if (x) return res += (index !== 0 ? " - " : "") + x;
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
async function setItems(new_features) {
|
|
|
|
|
let featureTemp;
|
|
|
|
|
if (new_features[new_features.length - 1] != null)
|
|
|
|
|
featureTemp = await getOneFeature(new_features[new_features.length - 1]);
|
|
|
|
|
let feature;
|
|
|
|
|
feature = featureTemp[0];
|
|
|
|
|
Object.assign(feature, {
|
|
|
|
|
new_feature: [],
|
|
|
|
|
show_in_product: false,
|
|
|
|
|
show_in_variable: false
|
|
|
|
|
});
|
2025-08-09 21:39:49 +03:30
|
|
|
console.log("gggggggggggggggggggggggggggggggg", feature);
|
2025-07-23 16:57:31 +03:30
|
|
|
items.value.push(feature);
|
|
|
|
|
}
|
2025-07-26 21:54:04 +03:30
|
|
|
const findEdit = (val) => {
|
|
|
|
|
console.log("fsdfuygiuhguighuisdf", val);
|
|
|
|
|
return val.split("-")[0] === "edit";
|
|
|
|
|
};
|
2025-07-23 16:57:31 +03:30
|
|
|
const firstFiles = ref([]);
|
|
|
|
|
ref([]);
|
|
|
|
|
|
2025-08-24 22:15:30 +03:30
|
|
|
export { files as a, setItems as b, formFields as c, findEdit as d, originalFile as e, firstFiles as f, items as i, loading as l, mediaFilesUrl as m, original as o, previewImg as p, resetFormFields as r, selectedFeatures as s, updateMyAttrs as u, variants as v };
|