var e = getApp(), s = e.request,oo=e.globalData,os=oo.setting;
var  ut = require("../../../../utils/util.js");

Page({
    data: {
        url: e.globalData.setting.url,
        resourceUrl: e.globalData.setting.imghost,
        delivery: null,
        express: null,
        re_arr:null,//反转后的数组
        error: "快递信息异常",
        active:0,
        has_connaect:1,
        is_get:0
    },
    onLoad: function(e) {

        var th=this;
        //----获取系统参数-----
        getApp().getConfig2(function (ee) {
            var  is_connaect=1;

            if(ee.switch_list){
                var json_d = JSON.parse(ee.switch_list);
                if(json_d.weapp_customertype==0){
                    is_connaect=0;
                }
                th.setData({
                    store_config: ee,
                    sys_switch: json_d,
                    has_connaect:is_connaect
                })

            }else{
                th.setData({
                    has_connaect:0
                })
            }

            th.requestDelivery(e.order_id);
        })



    },
    requestDelivery: function(e) {
        var r = this;
        s.get("/api/order/deliverydoc/page", {
            data: { order_id: e,store_id:os.stoid,pageSize:100},
            success: function(e) {

                if(ut.ajax_ok(e)){
                    var list=e.data.data.pageData;

                    for (let i = 0; i < list.length; i++) {
                        list[i].all_gd_num=0;
                        var json=list[i].erp_ck_warelist;
                        if(json){
                            json=JSON.parse(json);
                            for (let j = 0; j < json.length; j++) {
                                list[i].all_gd_num+=json[j].qty;
                            }
                        }

                        list[i].erp_sender_city=r.sheng_lue(list[i].erp_sender_city,4);
                        list[i].erp_receiver_city=r.sheng_lue(list[i].erp_receiver_city,4);
                    }



                    r.setData({
                        delivery: list[0],
                        delivery_arr:list
                    }), r.requestExpress();
                }



            }
        });
    },
    requestExpress: function() {
        var s = this;
        var url="/api/weshop/order/wuliu/" + this.data.delivery.shipping_code + "/" + this.data.delivery.invoice_no + "/" + this.data.delivery.mobile;
        getApp().request.promiseGet(url, {}).then(e => {
            wx.hideLoading();
            s.setData({is_get:1}) 

            if(e.data.code==0 && e.data.data){
                var re_arr = e.data.data.Traces.reverse();
                s.setData({
                    express: e.data.data, re_arr: re_arr,
                });
            }

        }).catch(function(err) {
            wx.hideLoading();
            e.showWarning("请求失败");
        })

    },


    click_bao_btn(e){
         var index= parseInt(e.currentTarget.dataset.idx);
         var item=this.data.delivery_arr[index];
         this.setData({
             delivery: item,
             active:index,
             express:null,
             re_arr:[],
             is_get:0
         });
         wx.showLoading();
         this.requestExpress();
    },

    //打开客服操作菜单
    openCS() {
        let self=this;
        // 判断会员状态
        let user_info = getApp().globalData.userInfo;
        if (user_info == null || user_info.mobile == undefined || user_info.mobile == '' || user_info.mobile == null) {
            wx.navigateTo({
                url: '/packageE/pages/togoin/togoin',
            })
            return false;
        };
        // csType/在线客服设置：
        // 0关闭（如果有设置热线电话，则显示拨打热线电话）、
        // 1小程序客服、
        // 2企业微信客服
        if (this.data.sys_switch) {
            let csType = this.data.sys_switch.weapp_customertype;
            if (csType == 0) {
                // 拨打电话号码
                this.contactService();
            } else {
                this.getTel()
                    .then(() => {
                        self.setData({
                            hiddenCS: false,
                        });
                    });
            };
        };

    },

    //关闭客服操作菜单
    closeCS() {
        this.setData({
            hiddenCS: true,
        });
    },

    // 获取客服电话
    getTel() {
        var self=this;
        return new Promise((resolve, reject) => {
            getApp().getConfig(function (t) {
                if (t.store_tel == undefined) {
                    getApp().request.get("/api/weshop/store/get/" + os.stoid, {
                        isShowLoading: 1,
                        data: {},
                        success: function (rs) {
                            getApp().globalData.config = rs.data.data;
                            if (rs.data.data.store_tel == null && rs.data.data.store_tel == undefined) {
                                getApp().my_warnning("商家未设置电话", 0, th);
                                return false;
                            }
                            self.setData({
                                store_tel: rs.data.data.store_tel,
                            });
                            // s.confirmBox("请联系客服：" + rs.data.data.store_tel);
                        }
                    })
                } else {
                    self.setData({
                        store_tel: t.store_tel,
                    });
                    // s.confirmBox("请联系客服：" + t.store_tel);
                };
                resolve();
            });
        });
    },


    customer_chat: function () {
        getApp().customer_chat("");
    },

    //---------联系客服------------
    contactService: function () {
        getApp().com_call(this);
    },

    //---------联系微信客服------------
    con_weixin: function () {
        var url = this.data.sys_switch.weapp_customertype_url;
        var id = this.data.sys_switch.weapp_customertype_appid;
        wx.openCustomerServiceChat({
            extInfo: { url: url },
            corpId: id,
            success(res) { }
        })
    },

    // 内容复制到剪贴板
    copy() {
        // console.log('点击了复制按钮');
        wx.setClipboardData({
            data: this.data.delivery.invoice_no,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data);
                    }
                })
            }
        })
    },

    sheng_lue(txt,maxLength){
        if(!txt) return '';
        if (txt.length > maxLength) {
            txt = txt.substr(0, maxLength) + '...';
        }
        return txt
    }


});