Files
appco_dist/.output/server/chunks/build/handle-CbilF0ny.mjs
T

247 lines
7.4 KiB
JavaScript

import { ref, reactive, computed } from 'vue';
import { cloneDeep } from 'lodash-es';
import { aq as supabaseService } from './server.mjs';
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([]);
ref([]);
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
}
);
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;
previewImg.value = void 0;
}
const files = computed(() => {
let tempMedia = cloneDeep(mediaFilesUrl.value);
tempMedia?.splice(0, 0, original.value);
tempMedia = tempMedia?.map((value, index) => {
return value;
});
console.log(tempMedia);
return tempMedia;
});
function addToVariants() {
let attrIds = [];
let attrNames = [];
myAttributeValue.value?.forEach((attributeValue) => {
if (!attributeValue.valueId) ;
attrIds.push(attributeValue.valueId);
attrNames.push(attributeValue.valueName);
});
console.log(attrIds, myAttributeValue.value);
formFields.variants?.push({
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: ""
});
console.log(variants, formFields.variants);
}
function updateMyAttrs(newValue, flag) {
let tempAttributes = newValue;
priceType.value = 1;
myAttributes.value = [];
myAttributeValue.value = [];
console.log(newValue);
console.log("why");
let tempIndex = 0;
let notShowInVarIndexArray = [];
newValue?.forEach((x, index) => {
console.log(x, index);
if (x.show_in_variable) {
priceType.value = 2;
if (formFields.variants.length === 0)
addToVariants();
myAttributes.value.push({
attributeId: x.id,
name: x.name,
values: x.new_feature,
show: x.show_in_variable,
valueKey: index
});
x.new_feature?.forEach((val, i) => {
myAttributeValue.value.push({
attributeName: x.name,
attributeId: x.id,
valueId: val.id,
valueName: val.value,
valueKey: tempIndex
});
});
} else {
notShowInVarIndexArray.push(index);
variants.value?.forEach((val) => {
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++;
variants.value?.forEach((val, ind) => {
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");
console.log(notShowInVarIndexArray);
sortAttributeByValueKey(variants.value);
if (notShowInVarIndexArray.length != 0) {
variants.value?.forEach((val) => {
notShowInVarIndexArray.forEach((y) => {
val.attribute_values[y] = null;
val.attribute_values_name[y] = null;
});
});
}
}
function sortAttributeByValueKey(x) {
console.log("444444", x);
x?.forEach((val, ind) => {
temp = cloneDeep(val.attribute_values);
if (myAttributes.value.length <= temp.length) {
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)) {
val.attribute_values[item.valueKey] = temp.find((e) => e == item.valueId);
val.attribute_values_name[item.valueKey] = item.valueName;
}
console.log(val?.attribute_values + "AFTER CHANGE ITEM" + ind);
});
});
}
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
});
console.log("gggggggggggggggggggggggggggggggg", feature);
items.value.push(feature);
}
const findEdit = (val) => {
console.log("fsdfuygiuhguighuisdf", val);
return val.split("-")[0] === "edit";
};
const firstFiles = ref([]);
ref([]);
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 };