//规格属性参考网址代码： fanyi.baidu.com/translate?aldtype=16047&query=Reinitialized+existing+Git+repository+in+D%3A%2Fgerr%2Fcode%2Fwxos%2Fwxos-projectnew%2F.git%2F%0D%0A%0D%0Az&keyfrom=baidu&smartresult=dict&lang=auto2zh

export function Spec(ary, skus, curr) {
    this.ary = ary;
    this.skus = skus;
    this.curr = curr;
	this.defaultData = null;
    /**
     * 获取sku
     */
   function getSid  (curr) {
        if (curr.length != ary.length) {
        return;
        }
        let a;
        let b;
        for (let i = 0; i < ary.length; i++) {
			let index = curr[i];
			// 如果当前的[i]值没没有直接返回
			if (index == null || !ary[i].data[index]) {
				return;
			}

			if (a == null) {
				a = ary[i].data[index].child;
			} else if (b == null) {
				b = ary[i].data[index].child;
			}
			if (a != null && b != null) {
				a = getArrEqual(a, b);
				b = null;
			}
        }
        if (a.length != 1) {
			console.info("警告：出现多个sku");
        }
        return a[0];
    }

    /**
     * 根据sku获得库存
     */
    function getNum (sid) {
	  
        for (let i = 0; i < skus.length; i++) {
			let item = skus[i];
			if (sid == item.id) {
				
				return item.num;
			}
        }
    }
    // 获取sku金钱
    function getPrice (sid) {
        for (let i = 0; i < skus.length; i++) {
        let item = skus[i];
        if (sid == item.id) {
            return item.price;
        }
        }
    }
    //获取图片
    function getImg (sid) {
        for (let i = 0; i < skus.length; i++) {
        let item = skus[i];
        if (sid == item.id) {
            return item.img;
        }
        }
    }
    //规格属性id
    function getAttrId (sid) {  //原来叫attrId后来更改为id
        for (let i = 0; i < skus.length; i++) {
        let item = skus[i];
        if (sid == item.id) {
            return item.id;
        }
        }
    }
    // 规格属性id
    function getdiscountSellPrice (sid) {  //原来叫attrId后来更改为id
        for (let i = 0; i < skus.length; i++) {
        let item = skus[i]
            if (sid == item.id) {
                return item.discountSellPrice;
            }
        }
    }
    
    function getDiscount (sid) {  //原来叫attrId后来更改为id
        for (let i = 0; i < skus.length; i++) {
        let item = skus[i]
            if (sid == item.id) {
                return item.discount;
            }
        }
    }
    /**
     * 获取两数组相同的元素
     */
    function getArrEqual  (arr1, arr2) {
        let newArr = [];
        for (let i = 0; i < arr2.length; i++) {
        for (let j = 0; j < arr1.length; j++) {
            if (arr1[j] === arr2[i]) {
            newArr.push(arr1[j]);
            }
        }
        }
        return newArr;
    }


    /**
     * 深复制
     */
    function clone (obj) {
        let objStr = JSON.stringify(obj)
        return JSON.parse(objStr); 
    }

    function getData (){
        if (curr == null || curr.length <= 0) {
          return;
        }
        for (let i = 0; i < ary.length; i++) {
          let item = ary[i]
            for (let j = 0; j < item.data.length; j++) {
                let c = clone(curr);
                c[i] = j;
                let sid = getSid(c);
                let num = getNum(sid);
                let price = getPrice(sid);
                let img = getImg(sid);
                let id = getAttrId(sid);
                let  discountSellPrice = getdiscountSellPrice(sid);
                let discount = getDiscount(sid);
                item.data[j].num = num;
                item.data[j].price = price;
                item.data[j].active= false;
                item.data[j].img = img;
                item.data[j].id = id;
                item.data[j].discountSellPrice = discountSellPrice;
                item.data[j].discount = discount;
				
            }
        }
        return ary;
    }
	function defaultData(){
		let attr = [];
		let data = getData();
		if(curr.length>0) {
			data.map((item,i) => {
				for(let c = 0; c<curr.length;curr++){
					attr.push(item.data[curr]) ;
				}  
			})
		} 
		return attr;
	}
	this.defaultData = defaultData();
}