var regeneratorRuntime = require('../../../../utils/runtime.js');
module.exports = {
    //主要的作用，就是把阶梯促销多余的商品进行拆分，
    //如果没有达成阶梯促销的要求，所有的商品都打回原价购买
    fir_set_arr: function (c_item, th) {
        //寻找一下
        function get_num2(ite) {
            var vh = c_item.goods.findIndex(function (e) {
                return e.goods_id == ite.goods_id
            })
            if (vh > -1) return vh;
            return 0;
        }

        //组合活动，以及组合活动从表的商品
        let ladder_map = c_item.ladder_map;
        let goods = c_item.goods;  //一个门店中所有的商品



        let tfeel = 0, cut_price = 0, offline_price = 0, offline_num = 0;
        //多个活动要拿来循环一下
        for (let i in ladder_map) {
            let title = "";  //提示语；
            //其中的一个活动
            let act_item = ladder_map[i];
            let ladder_list = act_item.ladder_list;

            let all_num = 0; //商品数量之和
            let all_price0 = 0; //商品数量之和
            let aprice = 0;  //达到活动条件的购买价格

            let no_in_arr = []; //剩余的未加入组合购
            let del_g=[];
            for (var li in goods) {
                if (goods[li].prom_type != 10) continue;
                if (goods[li].prom_id != act_item.id) continue;
                var item = goods[li];
                item.goods_price = item.goods_price;
                item.offline_price = item.offline_price;
                item.cart_num = item.goods_num;
                all_num += item.cart_num;
                for (var j = 0; j < item.cart_num; j++) {
                    no_in_arr.push({
                        price: item.goods_price,
                        goods_market_price:item.goods_market_price,
                        offline_price: item.offline_price,
                        goods_id: item.goods_id,
                        shop_price: item.shop_price,
                        item:item,
                        prom_id:act_item.id,
                        prom_type:10,
                    })
                }
                del_g.push(item);
            }

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

            //开始阶梯计算价格
            var lev = 0;
            for (let j in ladder_list) {
                if (no_in_arr.length == 0) break;
                let item_j =ladder_list[j];
                var end = no_in_arr.pop();
                var new_price = parseInt(item_j.discount_field) == 2?end.goods_market_price:(parseInt(item_j.discount_field) == 1 ? end.shop_price : end.price);
                var account=new_price * parseFloat(item_j.discount) / 10;
                aprice +=account;

                //-- 单个文件新增 --
                let new_g = JSON.parse(JSON.stringify(end.item));
                new_g.goods_num = 1;
                //如果有平摊到单品的时候，account要显示
                if(!th.data.ispt_goods) new_g.account = account.toFixed(2);
                new_g.ld_account = account.toFixed(2);
                new_g.discount = item_j.discount;
                new_g.goods_price =new_price;
                new_g.ladder_list_id =item_j.id;
                goods.push(new_g);
                lev++;
            }

            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;
                    }
                }
            }


            //-- 暂存的数组 --
            var temp_arr=[];
            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 && ele.prom_id==act_item.id && ele.prom_type==10
                    })
                    //goods[idx].goods_num -= num;
                    let new_g = JSON.parse(JSON.stringify(goods[idx]));

                    //多余的商品如果活动不参与订单促销，那么变成普通商品的也不能参与订单促销，2024-2-26
                    if(!act_item.is_useorderyh){
                        new_g.is_no_order_yh=1;
                    }
                    new_g.goods_num = num;
                    new_g.prom_type = 0;
                    new_g.prom_id = 0;
                    new_g.is_ld_split=1;
                    temp_arr.push(new_g);

                }
            }

            //-- 最后剔除不要参与的数据 --
            for(var h in del_g){
                var it=del_g[h];
                let idx = goods.findIndex(function (ele) {
                    return ele.goods_id == it.goods_id  && ele.prom_id==act_item.id && ele.prom_type==10
                })
                goods.splice(idx, 1);
            }

            var ls=-1;
            //-- 只统计是参与活动的商品 --
            for (var ij in goods) {
                var iter = goods[ij];
                if (iter.prom_type != 10 || iter.prom_id != act_item.id) {
                    continue;
                }
                goods[ij].is_act_last=0;
                all_price0 += iter.goods_num * iter.goods_price;
                ls= parseInt(ij);
            }

            //-- 要在特定的位置插入商品 --
            if(temp_arr){
                for(var gh=0;gh<temp_arr.length;gh++){
                    var idx=gh+ls+1;
                    goods.splice(idx, 0,temp_arr[gh]);
                }
                ls+=temp_arr.length;
            }
            goods[ls].is_act_last=1;


            if(!c_item.ladder_prom_goods) c_item.ladder_prom_goods={};
            if(!c_item.ladder_prom_goods[act_item.id]) c_item.ladder_prom_goods[act_item.id]={};

            //-- 活动的条件已经满足 --
            c_item.ladder_prom_goods[act_item.id].is_has_ladder = 1;
            c_item.is_has_ladder = 1;
            c_item.ladder_prom_goods[act_item.id].actual_price = aprice;
            c_item.ladder_prom_goods[act_item.id].cut_price = all_price0 - aprice;

            tfeel += aprice;
            cut_price += all_price0 - aprice;

        }

        c_item.ladder_all_price = tfeel;
        c_item.ladder_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 == 10
        })
        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;
    },

    //-- 组装阶梯购的价格 --
    calc_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 ladder_map = cart_item.ladder_prom_goods;  //一单中所有的组合购的集合
            var item_map = {};
            for (let i in ladder_map) {
                let item = ladder_map[i];
                let prom_id = i;
                //如果这个活动还没有达到要求，继续下一个计算
                if (!item.is_has_ladder) continue;
                //过滤出相应组合购活动的商品
                let glist = ord_goods.filter(function (ele) {
                    return ele.prom_type == 10 && 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({
                            "ladder_prom_id": prom_id,
                            "dis": (all_good_price - item.actual_price).toFixed(2),
                            "ispt": 0
                        })
                    } else {
                        cart_item.prom_pt_json = [{
                            "ladder_prom_id": prom_id,
                            "dis": (all_good_price - item.actual_price).toFixed(2),
                            "ispt": 0
                        }];
                    }
                }
            }

        }
    }


}
