const t_pay = require("../../../utils/pay2.js");
module.exports = {
    set_py_fir(){
        t_pay.set_fir();
    },
    //开启定位
    wait_for_store_config: function (th) {
        var t_time = setInterval(function () {
            if (th.data.bconfig == null) false;
            var e = th.data.bconfig;
            if (e && e.is_sort_storage) {
                wx.getLocation({
                    type: 'gcj02',
                    success: function (res) {
                        th.data.lat = res.latitude;
                        th.data.lon = res.longitude;
                        th.data.is_get_local_ok = 1;
                    },
                    fail: function (res) {
                        if (res.errCode == 2) {
                            th.setData({
                                is_gps: 0
                            });
                            if (th.data.is_gps == 0) {
                                getApp().confirmBox("请开启GPS定位", null, 10000, !1);
                            }

                        } else {
                            th.setData({
                                is_gps: "3"
                            });
                        }

                        th.data.is_get_local_ok = 1;
                    }
                })
            } else {
                th.data.is_get_local_ok = 1;
            }
            clearInterval(t_time);
        }, 500)
    },
	set_user_mo_store(th,os,func){
    	var that=th;
        //获取用户的默认门店
        getApp().get_user_store(function (e) {
            if (!e) {
                th.data.fir_def_store = {}; //赋值空对象
                return false;
            }

            if(getApp().globalData.is_dj_pk) th.setData({has_def:1})

            var ee = JSON.parse(JSON.stringify(e));
            //--定时器推迟一下--
            setTimeout(function () {
                if (th.data.fir_goods) var g_distr_type = th.data.fir_goods.distr_type;
                //--如果默认门店的配送方式不对，就不能被选择，这里不控制，如果不一样，就说明配送方式不对--
                if (ee.distr_type != 0 && g_distr_type != 0 && ee.distr_type != g_distr_type) {
                    ee.is_no_dis = 1;
                }

                var appd = getApp().globalData;
                var w_time = setInterval(function () {
                    if (that.data.is_get_local_ok == 0) return false;
                    clearInterval(w_time);
                    var distance = null;
                    var e = JSON.parse(JSON.stringify(ee));

                    //如果有开启近距离的话,同时距离优不一样了
                    if (that.data.lat != null) {
                        //如果经纬度有变化的话
                        if (e && appd.lat == that.data.lat && appd.lon == that.data.lon && e.distance > 0) {
                            that.set_def_storage(e);
                            if(func) func();
                        } else {
                            //要用接口是获取距离，js的计算不准
                            getApp().request.promiseGet("/api/weshop/pickup/list", {
                                data: {
                                    store_id: os.stoid,
                                    pickup_id: e.pickup_id,
                                    lat: th.data.lat,
                                    lon: th.data.lon,
                                    isstop: 0,
                                    is_pos: 1
                                },
                            }).then(res => {
                                if (res.data.code == 0) {
                                    e = res.data.data.pageData[0];
                                    if (e) {
                                        e.is_no_dis = ee.is_no_dis;
                                        appd.pk_store = e;
                                        that.set_def_storage(e);
                                    }

                                }
                                if(func) func();
                            })
                        }
                        //e.distance = distance;
                        appd.lat = that.data.lat;
                        appd.lon = that.data.lon;

                    } else {
                        if (e) {
                            e.distance = null;
                            that.set_def_storage(e);
                        }
                        if(func) func();
                    }
                }, 500)
             }, 700)
	    })

    },

    //---------拿出门店分类和门店------------
    get_sto(th,os) {
    var that = th;
        var self=this;
    var timer_get = setInterval(function () {
        if (th.data.is_get_local_ok == 0) return false;
        var dd = null;

        dd = {
            store_id: os.stoid,
            isstop: 0,
            is_pos: 1,
            pageSize: 2000,
        }

        //如果有距离的话
        if (th.data.lat != null) {
            dd.lat = th.data.lat;
            dd.lon = th.data.lon;
        }

        if(th.data.sele_g && th.data.sele_g.pickup_list){
            dd.ids=th.data.sele_g.pickup_list
        }

        clearInterval(timer_get);

        //如果会员是有默认的门店话
        if (!th.data.def_pick_store && th.data.fir_def_store) {
            th.setData({def_pick_store: th.data.fir_def_store});
        }
        wx.showLoading({
            title: '加载中.'
        });
        //----------获取门店----------------
        getApp().request.promiseGet("/api/weshop/pickup/list", {
            data: dd,
        }).then(res => {
            var e = res;

            if (e.data.code == 0 && e.data.data && e.data.data.pageData && e.data.data.pageData.length > 0) {

                var his_cate_num = 0;
                for (let i in e.data.data.pageData) {
                    let item = e.data.data.pageData[i];
                    if (item.category_id > 0) {
                        his_cate_num = 1;
                        break;
                    }
                }
                e.his_cate_num = his_cate_num;

                //如果有开启距离的功能，没有设置默认门店，要用最近的门店作为默认门店
                if (dd.lat && (!th.data.def_pick_store || JSON.stringify(th.data.def_pick_store) == '{}') && th.data.bconfig && th.data.bconfig.is_sort_storage) {
                    th.setData({
                        def_pick_store: e.data.data.pageData[0],
                        sto_sele_name: e.data.data.pageData[0].pickup_name,
                        sto_sele_id: e.data.data.pageData[0].pickup_id,
                        sto_sele_distr: e.data.data.pageData[0].distr_type
                    });
                    th.data.fir_def_store = e.data.data.pageData[0];
                }

                //-- 如果有默认选择门店的时候，要把默认门店放在第一位，修改不要配送方式的判断 --
                if (th.data.def_pick_store && JSON.stringify(th.data.def_pick_store) != '{}') {
                    for (var k = 0; k < e.data.data.pageData.length; k++) {
                        if (e.data.data.pageData[k].pickup_id == th.data.def_pick_store.pickup_id) {
                            e.data.data.pageData.splice(k, 1); //删除
                            break;
                        }
                    }
                    e.data.data.pageData.splice(0, 0, th.data.def_pick_store); //添加
                }

                th.setData({all_pick_list: e.data.data.pageData});

                setTimeout(function () {
                    self.deal_pickup(e,th,os);  //--普通门店排版--
                }, 800)

            }
        })
    }, 200)

   },

    //-- 统一购买 --
    buy_libao:function (th) {
        var _that=this;
        if(th.data.sele_g.is_pickup){
            if(!th.data.def_pick_store || (th.data.def_pick_store && !th.data.def_pick_store.pickup_id)){
                getApp().my_warnning("请先选择门店", 1, th);
                return false;
            }

            if(th.data.def_pick_store.is_no_dis){
                getApp().my_warnning("门店不匹配，请重新选择", 1, th);
                return false;
            }
        }

        if(th.data.submit) return false;
        th.setData({submit:1})

        if(th.pp_bacK_func){
            th.pp_bacK_func();
            return  false;
        }

        var id=th.data.sele_g.lbId;
        var that=th;
        var json = {
            "actId": '', //活动Id
            "actType": '', //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
            "buyType":""+that.data.buyType, //1=积分兑换 2=余额购买
            "giftBagId": id, //礼包Id
            "storeId": that.data.getStorageID, //商家Id
            "userId": that.data.getUserID, //用户ID
            "buyFrom": 2,
        };

        if(that.data.first_leader){
            json.first_leader=that.data.first_leader
        }
        if(that.data.guide_id){
            json.guide_id=that.data.guide_id
        }

        if(th.data.sele_g.is_pickup && that.data.def_pick_store){
            json.pickup_id=that.data.def_pick_store.pickup_id;
        }

        //-- 分享导购要记录 --
        if (getApp().globalData.guide_id) {
            json.guide_id = getApp().globalData.guide_id;
        }

        var url ="/api/weshop/marketing/buy/receive/gift/record/insert";
        getApp().request.json_post(url, json,
            function (res) {
                if (res.data.code == 0 && res.data.data) {

                    switch(that.data.buyType){
                        case 1://立即兑换
                            var order_sn = res.data.data;

                            th.setData({submit:0})
                            getApp().my_warnning("兑换成功！", 1, th);
                            th.closeSpecModal();
                            _that.go_detail(id,order_sn,th);
                            break;
                        case 2://立即购买

                            var order_sn = res.data.data.orderSn;
                            that.data.ok_order_sn=order_sn;

                            var money=that.data.payMoney;
                            var url = "/packageF/pages/giftpack/payment/payment?money=" + money + "&order_sn=" + order_sn;
                            getApp().globalData.no_clear=1;

                            t_pay.pay_data(res,function (){
                                th.setData({submit:0})
                                getApp().goto(url);
                            },function (e){
                                th.setData({submit:0})
                                wx.showToast({
                                    title:e,
                                    icon:'none',
                                    duration:2500
                                })
                            })

                            break;
                        case 3://立即领取

                            var order_sn = res.data.data;
                            th.setData({submit:0})
                            getApp().my_warnning("领取成功！", 1, th);
                            th.closeSpecModal();
                            _that.go_detail(id,order_sn,th);
                            break;
                    }

                } else {
                    th.setData({submit:0})
                    getApp().my_warnning(res.data.msg, 0, th);
                }
            },
            function (res) {
                th.setData({submit:0})
            }
        )
    },

    //-- 跳转到商品使用页面 --
    go_detail(id,orderSn,th){
       //不是详情页面，就不跳转了
       if(!th.data.is_detail) return false;

       var url='/packageF/pages/giftpack/giftpacklist/giftpacklist?isBuy=0&lbId='+id+'&orderSn='+orderSn;
       getApp().goto(url);
    },

    //------------处理门店---------------
    deal_pickup(e,th,os) {

        if (!th.data.sele_g) return false
        wx.hideLoading();

        //单总量超出10个的时候，同时门店有分类
        if (e.data.data.total > 10 && e.his_cate_num) {
            getApp().request.get("/api/weshop/storagecategory/page", {
                data: {
                    store_id: os.stoid,
                    pageSize: 1000,
                    orderField: "sort",
                    orderType: 'asc',
                },
                success: function (ee) {
                    if (ee.data.code == 0) {
                        var check_all_cate = 0;
                        if (ee.data.data && ee.data.data.pageData && ee.data.data.pageData.length > 0) {
                            for (let i in ee.data.data.pageData) {
                                let item = ee.data.data.pageData[i];
                                if (item.is_show == 1) {
                                    check_all_cate = 1;
                                    break
                                }
                            }
                        }
                        if (check_all_cate) {
                            var sto_cate = ee.data.data.pageData;
                            var sto_arr = e.data.data.pageData;
                            var newarr = new Array();
                            var qita = new Array();


                            var is_del_pk = 0;
                            //----要进行门店分组--------
                            for (var i = 0; i < sto_arr.length; i++) {
                                //找一下这个门店有没有在分类数组内
                                var find2 = 0, find2name = "", sort = 0;
                                is_del_pk = 0;
                                for (var m = 0; m < sto_cate.length; m++) {
                                    if (sto_arr[i].category_id == sto_cate[m].cat_id) {
                                        if (sto_cate[m].is_show != 1) {
                                            is_del_pk = 1;
                                            sto_arr.splice(i, 1);
                                            i--;
                                        } else {
                                            find2 = sto_cate[m].cat_id;
                                            find2name = sto_cate[m].cat_name;
                                            sort = sto_cate[m].sort;
                                            is_del_pk = 0;
                                        }
                                        break;
                                    }
                                }
                                if (is_del_pk) continue;

                                if (newarr.length > 0) {
                                    var find = 0;
                                    //如果有找到，那门店就在这个分组内，否则，分类就要排在其他
                                    if (find2 != 0) {
                                        for (var ii = 0; ii < newarr.length; ii++) {
                                            if (sto_arr[i].category_id == newarr[ii].cat_id) {
                                                newarr[ii].s_arr.push(sto_arr[i]);
                                                find = 1;
                                                break;
                                            }
                                        }
                                        if (find == 0) {
                                            var arr0 = new Array();
                                            arr0.push(sto_arr[i]);
                                            var item = {
                                                cat_id: find2,
                                                name: find2name,
                                                sort: sort,
                                                s_arr: arr0
                                            };
                                            newarr.push(item);
                                        }
                                    } else {
                                        qita.push(sto_arr[i]);
                                    }
                                } else {
                                    //如果有找到，那门店就在这个分组内，否则，分类就要排在其他
                                    if (find2 != 0) {
                                        var arr0 = new Array();
                                        arr0.push(sto_arr[i]);
                                        var item = {
                                            cat_id: find2,
                                            name: find2name,
                                            sort: sort,
                                            s_arr: arr0
                                        };
                                        newarr.push(item);
                                    } else {
                                        qita.push(sto_arr[i]);
                                    }
                                }
                            }

                            var def_arr = new Array();
                            //-- 开始就看10个门店 --
                            for (var k = 0; k < 10; k++) {
                                if (k == sto_arr.length) break;
                                def_arr.push(sto_arr[k]);
                            }

                            th.setData({
                                def_pickpu_list: def_arr,
                                pickpu_list: ee.data.data.pageData
                            });


                            //门店分类要排序下
                            function compare(property) {
                                return function (a, b) {
                                    var value1 = a[property];
                                    var value2 = b[property];
                                    return value1 - value2;
                                }
                            }

                            if (newarr.length > 0)
                                newarr.sort(compare("sort"));


                            //----安排其他的分类-----
                            if (qita.length > 0) {
                                var item = {
                                    cat_id: -1,
                                    name: "其他",
                                    s_arr: qita
                                };
                                newarr.push(item);
                            }

                            var sd = {
                                all_sto: newarr,
                                is_show_sto_cat: 1
                            }
                            if (!sto_arr || sto_arr.length <= 10) {
                                sd.is_show_sto_cat = -1;
                                sd.only_pk = sto_arr;
                            }
                            th.setData(sd);

                        } else {
                            th.setData({
                                is_show_sto_cat: -1,
                                only_pk: e.data.data.pageData
                            });
                            //-----如果没有默认门店，要取第一个门店作为默认店.此时没有门店分类的情况------
                            if (!th.data.def_pick_store) {
                                th.setData({
                                    def_pick_store: e.data.data.pageData[0],
                                    sto_sele_name: e.data.data.pageData[0].pickup_name,
                                    sto_sele_id: e.data.data.pageData[0].pickup_id,
                                    sto_sele_distr: e.data.data.pageData[0].distr_type
                                })
                            }
                        }
                    } else {
                        th.setData({
                            is_show_sto_cat: -1,
                            only_pk: e.data.data.pageData
                        });
                        //-----如果没有默认门店，要取第一个门店作为默认店.此时没有门店分类的情况------
                        if (!th.data.def_pick_store) {
                            th.setData({
                                def_pick_store: e.data.data.pageData[0],
                                sto_sele_name: e.data.data.pageData[0].pickup_name,
                                sto_sele_id: e.data.data.pageData[0].pickup_id,
                                sto_sele_distr: e.data.data.pageData[0].distr_type
                            })
                        }

                    }
                }
            });
        } else {
            th.setData({
                is_show_sto_cat: 0,
                only_pk: e.data.data.pageData
            });
            //-----如果没有默认门店，要取第一个门店作为默认店------
            if (!th.data.def_pick_store && th.data.bconfig && th.data.bconfig.is_sort_storage) {
                th.setData({
                    def_pick_store: e.data.data.pageData[0],
                    sto_sele_name: e.data.data.pageData[0].pickup_name,
                    sto_sele_id: e.data.data.pageData[0].pickup_id,
                    sto_sele_distr: e.data.data.pageData[0].distr_type
                })
            }
        }
    },

    check_def_pk:function (th) {
        th.data.def_pick_store.is_no_dis=0;
        var def_pick_store=th.data.def_pick_store
        var sele_g=th.data.sele_g;
        var arr=sele_g.pickup_list.split(',');

        var idx=arr.findIndex(function (e) {
            return e==def_pick_store.pickup_id;
        })

        if(idx==-1){
            def_pick_store.is_no_dis=1;
        }
        th.setData({def_pick_store})

    }




}