import da from "element-ui/src/locale/lang/da";

class Sku {

    constructor(page = null) {
        this._data = {};
        this._def = [];//当前循环的参数值
        this.columnsData = [];//列表数据
    }

    /**
     * 设置SKU 范围值
     * @param data
     */
    setSkuName(data) {
        this._data = data;
        console.log(this._data);
        this._value = [];
    }

    /**
     * 获取sku具体信息
     */
    getColumns() {
        this._def = [];
        var forSize = 0,index = this._data.length - 1;
        for (var i = 0; i < this._data.length; i++) {
            var len = this._data[i].data.length;
            this._def.push(0);
            forSize += (i + 1) * len;
        }
        for (var i = 0; i < forSize; i++) {
            this.inDef();
            this.defAdd(index);
        }
        var data = [];
        for (var i in this.columnsData) {
            var d = this.columnsData[i];
            var idAr = [];
            var ud = {};
            for(var i2 in d) {
                ud[d[i2].id] = d[i2].subName;
                idAr.push(d[i2].subId);
            }
            ud.price = 0;//售价
            ud.cost = 0;//成本价
            ud.ot_price = 0;//原价
            ud.stock = 1000;//库存
            ud.bar_code = "";//编码
            ud.weight = "0";//重量
            ud.img = "";
            ud.skuId = idAr.join(":");
            data.push(ud);
        }
        return data;
    }
    /**
     * 处理头部数据
     */
    inDef() {
        var data = [];
        for (var i in this._def) {
             var d = this._data[i];
             var sub = this._data[i].data[this._def[i]];
             data.push({
                 name    : d.name,
                 id      : d.id,
                 subName : sub.name,
                 subId   : sub.id
             });
        }
        this.columnsData.push(data);
    }

    /**
     * 循环点排序
     * @param index
     * @returns {undefined|*}
     * [0,0,0]  - [0,0,1]
     * [0,0,0]
     */
    defAdd(index) {
        if(index < 0) return;
        var defIndex = this._def[index];
        var defLen = this._data[index].data.length;
        if ((defIndex + 1) < defLen) {
            this._def[index]++;
        } else {
            this._def[index] = 0;
            index--;
            this.defAdd(index);
        }
    }


    /**
     * 生成唯一Id
     * @returns {undefined}
     */
    getId() {
        return Number(Math.random().toString().substr(3, 6) + Date.now()).toString(36);
    }

}


export default Sku
