var regeneratorRuntime = require('../../../../utils/runtime.js');
module.exports = {
    //主要的作用，就是把组合购多余的商品进行拆分，
    //如果没有达成组合购的要求，所有的商品都打回原价购买
    fir_set_arr: function (c_item,th) {
        //组合活动，以及组合活动从表的商品
        let zh_prom_goods = c_item.zh_prom_goods;
        let goods = c_item.goods;  //一个门店中所有的商品
        let tfeel = 0,cut_price=0, offline_price = 0, offline_num = 0;
        let txt_ite = c_item.need_list;
        //多个活动要拿来循环一下
        for (let i in zh_prom_goods) {
            let title = "";  //提示语；
            //其中的一个活动
            let act_item = zh_prom_goods[i];
            let act_goos = act_item.gdlist;
            let act = act_item.act;

            let all_num = 0; //商品数量之和
            let all_price0 = 0; //商品数量之和
            let aprice=0;  //达到活动条件的购买价格
            let need_to_buy = 0;
            let all_zhqty = 0; //所有商品要求起购数之和
            let no_in_arr = []; //剩余的未加入组合购
            let out_arr=[];     //超出活动限购的商品放入

            //-- 寻找一下 --
            function get_num2(ite) {
                for (let v2 in goods) {
                    var vh = goods[v2];
                    if (vh.goods_id == ite.goods_id) {
                        return vh;
                    }
                }
                return 0;
            }

            for (let j in act_goos) {
                let item = act_goos[j];
                let item_j = goods.find(function (ele) {
                    return ele.goods_id == act_goos[j].goods_id;
                })
                //-- 如果有找到的话 --
                if (item_j) {
                    act_goos[j].num = item_j.goods_num;
                    act_goos[j].goods_price = item_j.goods_price;
                    var cart_num=item.cart_num =item_j.goods_num;
                    var zh_b_num=item_j.promgoodsbuynum;
                    if(item.buyqty>0){

                        var key=item.goods_id+""+act.id;
                        var cbuy=item.buyqty-zh_b_num;
                        if(th.data.in_zhact_gdmap[key]){
                            var cbuy=item.buyqty-zh_b_num-th.data.in_zhact_gdmap[key];
                        }


                        //当可买的数量为0
                        if(cbuy<=0){
                            item.num=0;
                            out_arr.push({
                                price: item.goods_price,offline_price:item.offline_price,goods_id:item.goods_id,num:cart_num
                            })
                        }else{
                            if(cbuy>=cart_num) item.num =cart_num;
                            else{
                                item.num =cbuy;
                                out_arr.push({
                                    price: item.goods_price,offline_price:item.offline_price,goods_id:item.goods_id,num:cart_num-cbuy
                                })
                            }
                        }
                    }

                    all_num += item.num;
                    //当有起购数的控制的时候
                    if (item.zhqty) {
                        all_zhqty += item.zhqty;
                        if (item.num < item.zhqty) {
                            need_to_buy += item.zhqty - item.num;
                        }
                        if (item.num > item.zhqty) {
                            for (let i = 0; i < item.num - item.zhqty; i++) {
                                no_in_arr.push({
                                    price: item.goods_price, goods_id: item.goods_id, offline_price: item_j.offline_price
                                })
                            }
                        }
                    } else {
                        for (let j = 0; j < item.num; j++) {
                            no_in_arr.push({
                                price: item.goods_price, goods_id: item.goods_id, offline_price: item_j.offline_price
                            })
                        }
                    }
                } else {
                    if (item.zhqty) {
                        title = "必买商品未加购，下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
                        need_to_buy += item.zhqty;
                    }
                }

            }

            //当满足组合的要求：总数要满足，起购数要满足
            if (all_num >= act.zhbuyqty && !need_to_buy) {

                //商品的列表
                for (var u in act_goos) {
                    var ite=act_goos[u];
                    if(ite.buyqty>0 && ite.num>0){
                        var key=ite.goods_id+""+act.id;
                        if(th.data.in_zhact_gdmap[key]){
                            th.data.in_zhact_gdmap[key]+=ite.num;
                        }else{
                            th.data.in_zhact_gdmap[key]=ite.num;
                        }
                    }
                }


                function sortData(a, b) {
                    return a.price - b.price
                }

                if(no_in_arr.length>0)  no_in_arr.sort(sortData);

                aprice = act.zhprice;
                if (act.zhbuyqty > all_zhqty) {
                    for (let n = 0; n < act.zhbuyqty-all_zhqty; n++) {
                        no_in_arr.pop();
                    }
                }
                //看一下剩下的数量有没有满足组合购的要求，以及要不要倍增
                if (act.is_bz && no_in_arr.length >= act.zhbuyqty) {
                    //看一下是几倍
                    let be = parseInt(no_in_arr.length / act.zhbuyqty);
                    aprice += be * act.zhprice;
                    for (let m = 0; m < be * act.zhbuyqty; m++) {
                        no_in_arr.pop();
                    }
                }


                let goods_map = {};
                //算一下剩余的商品
                if(no_in_arr.length) {
                    for (let ii in no_in_arr) {
                        let item = no_in_arr[ii];
                        if (item.offline_price) {
                            offline_price += item.price - item.offline_price;
                            offline_num += 1;
                        }
                        if (goods_map[item.goods_id]) {
                            var num = goods_map[item.goods_id];
                            goods_map[item.goods_id] = ++num;
                        } else {
                            goods_map[item.goods_id] = 1;
                        }
                    }
                }

                //超量的部分也要计算一下
                if(out_arr.length){
                    for (var m in out_arr) {
                        var item = out_arr[m];
                        if (item.offline_price) {
                            offline_price += (item.price - item.offline_price)*item.num;
                            offline_num += item.num;
                        }
                        if (goods_map[item.goods_id]) {
                            var num = goods_map[item.goods_id];
                            num+=item.num
                            goods_map[item.goods_id] =num;
                        } else {
                            goods_map[item.goods_id] = item.num;
                        }
                    }
                }

                if(Object.keys(goods_map).length) {
                    //这里就开始拆分提交订单时的列表
                    for (let j in goods_map) {
                        //map中存的就是商品的数量
                        let num = goods_map[j];
                        //找到相应的商品
                        let idx = goods.findIndex(function (ele) {
                            return ele.goods_id == j;
                        })
                        goods[idx].goods_num -= num;
                        let new_g = JSON.parse(JSON.stringify(goods[idx]));
                        new_g.goods_num = num;
                        new_g.prom_type = 0;
                        new_g.prom_id = 0;
                        goods.push(new_g);

                        //如果商品的数量已经为空了
                        if(goods[idx].goods_num<=0){
                            goods.splice(idx,1);
                        }
                    }
                }


                //-- 只统计是参与活动的商品 --
                for(var ij in goods){
                    var iter=goods[ij];
                    if(iter.prom_type!=7 || iter.prom_id!=act.id){ continue; }
                    all_price0+=iter.goods_num*iter.goods_price;
                }
                //-- 活动的条件已经满足 --
                c_item.zh_prom_goods[act.id].is_has_zh = 1;
                c_item.is_has_zh = 1;
                c_item.zh_prom_goods[act.id].actual_price = aprice;
                c_item.zh_prom_goods[act.id].cut_price =all_price0-aprice;

                //-- 设置还需要购买多少件，享受活动，前段显示 --
                let need_to = c_item.need_list;
                let index = this.find_need_to(need_to, act);
                if (index > -1) {
                    need_to.splice(index, 1);
                    txt_ite = need_to
                }


            } else {
                for (let ii in goods) {
                    let item = goods[ii];
                    if (item.prom_type != 7) continue;
                    if (item.prom_id != act.id) continue;
                    if (item.offline_price) {
                        offline_price += item.goods_price - item.offline_price;
                        offline_num += 1;
                    }
                    item.prom_type = 0;
                    item.prom_id = 0;
                }
                if (act.zhbuyqty - all_num > need_to_buy) {
                    need_to_buy = act.zhbuyqty - all_num;
                }
                //-- 设置还需要购买多少件享受活动，前端显示 --
                let need_to = c_item.need_list;
                let index = this.find_need_to(need_to, act);
                if (title == '') title = "再买" + need_to_buy + "件，下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
                if (index != -1) {
                    need_to[index].title = title;
                } else {
                    if (!need_to) need_to = [];
                    let it = {
                        id: act.id,
                        pickup_id: c_item.pickup_id,
                        title: title
                    };
                    need_to.push(it);
                }
                txt_ite = need_to;
            }
            tfeel += aprice;
            cut_price+=all_price0-aprice;;
        }

        c_item.zh_all_price = tfeel;
        c_item.zh_cut_price = cut_price;

        if (c_item.is_offline == 1) {
            c_item.offline_price += offline_price;
            c_item.offline_num += offline_num;
        } else {
            c_item.is_offline == 1;
            c_item.offline_price = offline_price;
            c_item.offline_num = offline_num;
        }

    },
    //筛选组合购，纯粹的数组按活动id分组
    find_split: function (arr) {
        //过滤只有团购的商品
        let oarr = arr.goods.filter(function (ele) {
            return ele.prom_type == 7
        })
        if (!oarr || oarr.length == 0) return null;
        //看一下有多少个不同的团购
        let map = {},
            dest = [];
        for (let i = 0; i < oarr.length; i++) {
            let ai = oarr[i];
            let index = map[ai.prom_id]
            if (!index) {
                dest.push({
                    prom_id: ai.prom_id,
                    act: ai.act,
                    data: [ai]
                });
                map[ai.prom_id] = dest.length; //存储下标
            } else {
                let dj = dest[index - 1];
                dj.data.push(ai);
            }
        }
        return dest;
    },

    find_need_to: function (list, iter) {
        if (!list || list.length <= 0) return -1;
        for (var i in list) {
            var item = list[i];
            if (item.id == iter.id) {
                return i;
            }
        }
        return -1;
    },
    //-- 平摊组合购的价格 --
    calc_zh_split_price: async function (c_arr, th) {
        //-- 循环处理 --
        for (var k in c_arr) {
            var cart_item = c_arr[k];      //就是每一单的意思
            var ord_goods = c_arr[k].goods;   //就是每一单的从表的意思

            var zh_prom_goods = cart_item.zh_prom_goods;  //一单中所有的组合购的集合
            var item_map = {};
            for (let i in zh_prom_goods) {
                let item = zh_prom_goods[i];
                let prom_id = i;
                //如果这个活动还没有达到要求，继续下一个计算
                if (!item.is_has_zh) continue;
                //过滤出相应组合购活动的商品
                let glist = ord_goods.filter(function (ele) {
                    return ele.prom_type == 7 && ele.prom_id == i;
                })
                var all_good_price = 0;
                var post_gd=[];
                for (let j in glist) {
                    let item = glist[j];
                    all_good_price += item.goods_price * item.goods_num;
                    var gitem={
                        goods_id:item.goods_id,
                        goods_num:item.goods_num,
                        goods_price:item.goods_price
                    }
                    post_gd.push(gitem);
                }
                //要进行优惠的计算
                if (all_good_price - item.actual_price) {
                    if (cart_item.prom_pt_json) {
                        cart_item.prom_pt_json.push({
                            "zhprom_id": prom_id,
                            "dis": (all_good_price - item.actual_price).toFixed(2),
                            "ispt": 0
                        })
                    } else {
                        cart_item.prom_pt_json = [{
                            "zhprom_id": prom_id,
                            "dis": (all_good_price - item.actual_price).toFixed(2),
                            "ispt": 0
                        }];
                    }
                }

                //-- 如果系统要平摊到单品 --
                var pt_data = {
                    'prom_id': prom_id,
                    'dis': parseFloat(all_good_price - item.actual_price).toFixed(2),
                    'goods': post_gd
                }
                var pt_res = null;
                await  getApp().request.promisePost("/api/weshop/order/getGoodsSplit", {
                    is_json: 1,
                    data: pt_data
                }).then(res => {
                    if (res.data.code == 0) {
                        pt_res = res.data.data;
                    }
                })
                if (pt_res) {
                    for (var io in glist) {
                        var goods_id = glist[io].goods_id;
                        item_map[goods_id]={};
                        //平摊赋值
                        item_map[goods_id].account_fir = th.arr_get_goods(goods_id, pt_res).fisrt_account;
                        item_map[goods_id].account_yu_fir = th.arr_get_goods(goods_id, pt_res).fisrt_account_yu;
                        if (!th.data.ispt_goods) {
                            item_map[goods_id].account = item_map[goods_id].account_fir;
                            item_map[goods_id].account_yu = item_map[goods_id].account_yu_fir;
                        }
                    }
                }
            }

            //--------循环计算商品是不是包邮，是不是使用优惠券,此时循环是商品从表-----------
            for (var jj = 0; jj < ord_goods.length; jj++) {
                if (ord_goods[jj].is_gift) continue;
                if (ord_goods[jj].prom_type == 7 && item_map[ord_goods[jj].goods_id]) {
                    ord_goods[jj].account_fir = item_map[ord_goods[jj].goods_id].account_fir;
                    ord_goods[jj].account_yu_fir = item_map[ord_goods[jj].goods_id].account_yu_fir;
                    ord_goods[jj].account = item_map[ord_goods[jj].goods_id].account;
                    ord_goods[jj].account_yu = item_map[ord_goods[jj].goods_id].account_yu;
                }
            }
        }


    }


}
