// import { getProductDataApi } from "@/api/product/index";
import { getProductDataApi,getAllProductDataApi } from "@/api/product/index";
import { STOCK_TYPE } from "@/utils/declare";
import { mapGetters, mapActions } from "vuex";
import { copyObject } from "@/utils/util";
export default {
	data() {
		return {
			pageType: "bill", //选择商品页面类别  用于区分不同功能的数据

			productInfo: [], //商品信息
			classIndex: 0, //当前商品类别下标
			py: "", //拼音搜索条件
			type: "", //库存预警时，固定为waring，其他情况不传或者传空
			autoLoad: true, //自动获取商品信息
			isLimit: true, //选择数量不能大于库存限制
			filterProductInfoContrapose:[],
			allFilterProductInfoContrapose:[],
			isMore:true,
			page:1,
		};
	},
	onLoad(options) {
		const { pageType } = options;
		if (pageType) {
			this.pageType = pageType;
		}

		if (this.autoLoad) {
			this.initDataContrapose();
		}
	},
	onUnload() {},
	computed: {
		...mapGetters(["getSelectInfo","getMyShopInfo"]),
	},
	methods: {
		...mapActions(["setSelectInfoByType", "resetSelectInfoByType"]),
		/**
			* 请求商品数据
			*/
		loadMore(){
			if(this.isMore){
				this.page++;
				let type = this.tabs[this.tabIndex].key == STOCK_TYPE.WARN ? "waring" : "";
				if(type=="waring"){
					this.initDataContrapose({ type,shopId: this.shopId});
				}
			}

		},
		// async initDataContrapose(extendInfo) {
		async initDataContrapose(extendInfo) {
			//extendInfo调用时的扩展参数  如  shopId
			let params = {
				py: this.py,
				type: this.type,
				// showPage: this.offShowPage?'':1,
				showPage: 1,
				page: this.page,
			};
			if (extendInfo) {
				params = { ...params, ...extendInfo };
			}
			const { data } = await getAllProductDataApi(params);
			data.list.forEach((ele)=>{
				this.filterProductInfoContrapose.push(ele);
			})
			this.allFilterProductInfoContrapose = this.filterProductInfoContrapose;
			if(this.filterProductInfoContrapose.length==data.totalNum){this.isMore = false;}
			this.productInfo = data.list;
			uni.stopPullDownRefresh();
			uni.hideLoading();
		},





		/**
			* 搜索对应花材
			*/
		searchFnContrapose() {
			let list = copyObject(this.allFilterProductInfoContrapose);
			if(!this.$util.isEmpty(this.py)){
				let commonChildList = [];
				console.log(this.py)
				list.forEach(ele => {
					let hasName = ele.itemName && ele.itemName.indexOf(this.py) >= 0;
					let elePy = (ele.py && ele.py.toLocaleUpperCase()) || "";
					let hasPy = elePy.indexOf(this.py.toLocaleUpperCase()) >= 0;
					if (hasName || hasPy) {
						console.log(ele)
						commonChildList.push(ele)
					}

				});
				// list = this.$util.unique(commonChildList)
				this.filterProductInfoContrapose = commonChildList;
			}else{
				this.filterProductInfoContrapose = this.allFilterProductInfoContrapose;
			}
		}
	}
};
