var t = require("../../../../utils/util.js");
var os = getApp().globalData.setting;
const {
    qrcode
} = require('../../../../utils/index.js');
const rq = require("../../../../utils/request.js");
Page({
    data: {
        imghots: os.imghost,
        guide: null,
        pick_up: null,
        error_arr: {
            '81012': '缺失可见范围',
            '81013': 'UserID、部门ID、标签ID全部非法或无权限',
            '40031': '不合法的UserID列表',
            '0032': '不合法的UserID列表长度',
            '40058': '不合法的参数',
            '40063': '参数为空',
            '40094': '不合法的URL',
            '40096': '不合法的外部联系人userid',
            '40099': '外部联系人的数量已达上限',
            '40100': '此用户的外部联系人已经在转移流程中',
            '40125': '无效的openuserid参数',
            '41001': '缺少access_token参数',
            '41009': '缺少userid参数',
            '41010': '缺少url参数',
            '41035': '缺少外部联系人userid参数',
            "41037": '缺少「联系我」type参数',
            "41038": '缺少「联系我」scene参数',
            "41039": '无效的「联系我」type参数',
            "41040": '无效的「联系我」scene参数',
            "41041": '「联系我」使用人数超过限制',
            "41042": '无效的「联系我」style参数',
            "41043": '缺少「联系我」config_id参数',
            "41044": '无效的「联系我」config_id参数',
            "41045": 'API添加「联系我」达到数量上限',
            "42001": 'access_token已过期',
            '43009': '企业未验证主体',
            '45009': '接口调用超过限制',
            '45022': '应用name参数长度不符合系统限制',
            '45024': '帐号数量超过上限',
            '45034': 'url必须有协议头',
            '46004': '指定的用户不存在',
            '48001': 'API功能未授权',
            '48002': 'API接口无权限调用',
            '48003': '不合法的suite_id',
            '48004': '授权关系无效',
            '48005': 'API接口已废弃',
            '48006': '接口权限被收回',
            '60102': 'UserID已存在',
            '670010': '负责人数量超出限制',
            '81014': '标签添加成员，单次添加user或party过多',
            '82001': '	指定的成员/部门/标签全部为空',
            '84072': '应用未配置客服',
            '84073': '客服userid不在应用配置的客服列表中',
            '84074': '没有外部联系人权限',
            '84075': '不合法或过期的authcode',
            '84076': '缺失authcode',
            '84083': '非服务人员',
            '84084': '非跟进用户',
            '84085': '应用已下架',
            '84144': '确认者的gid不在当前服务商的通讯录中',
            '84145': '确认者gid所在的企业数不能超过5个',
            '84146': '确认者gid最多只能在3个企业下确认合同',
            '86001': '参数 chatid 不合法',
            '86003': '参数 chatid 不存在',
            '86004': '参数 群名不合法',
            '86005': '参数 群主不合法',
            '86006': '群成员数过多或过少',
            '86007': '不合法的群成员',
        },
        skip_verify:1,

        is_qy_wx:1,
        qr_code:'',
        phone:'',

        err_img:os.imghots + "/miniapp/images/default_g_img.gif"
    },

    onLoad: function (t) {
        var th=this;
        this.data.phone=getApp().globalData.userInfo.mobile;
        qrcode('qrcode', "1505955959595", 480, 480, this);
        //th.requestRecommend();
        /*--- 获取一下，会员要不要线上美顾 ---*/

        getApp().getConfig(()=>{
            rq.get("/api/weshop/users/getAndUpdateUser/" + os.stoid + "/" + getApp().globalData.user_id, {
                success: async function (src) {
                    if (src.data.code == 0) {
                        var u_data = src.data.data;
                        if (!u_data.staffId) {
                            getApp().confirmBox('该会员无专属导购');
                            return false;
                        }
                        //-- 导购的临时变量 --
                        let tp_guide = null;
                        let tp_pick = null;
                        //获取会员在手店后台的
                        await getApp().get_guide_from_staff(u_data.staffId, function (guide) {
                            if (!guide) {
                                getApp().confirmBox('该会员的专属导购未在手店后台导购列表中');
                                return false;
                            }
                            tp_guide = guide;
                        });

                        if(!tp_guide.weapp_codeimg){
                            var code_rq={};
                            code_rq['accdb'] = getApp().globalData.config.erpid;
                            code_rq['staffId'] = encodeURIComponent(tp_guide['erp_staff_id']);
                            await getApp().request.promiseGet("/api/weshop/MdUser/getStaffWeAppEwm", {
                                data: code_rq,
                            }).then(res => {
                                if(res.data.code==0){
                                    tp_guide.weapp_codeimg=res.data.data.weapp_codeimg;
                                }
                            })

                        }

                        tp_guide.staffName = u_data.staffName;
                        tp_guide.staffId = u_data.staffId;
                        tp_guide.staffTel = u_data.staffTel || tp_guide.mobile;
                        if(!tp_guide.staffTel){
                            tp_guide.staffTel=tp_guide.bind_user_mobile;
                        }
                        await getApp().request.promiseGet("/api/weshop/pickup/get/" + os.stoid + "/" + tp_guide.pickup_id, {}).then(res => {
                            if(res.data.code==0 && res.data.data){
                                tp_pick=res.data.data;
                            }
                        })
                        th.setData({
                            guide: tp_guide,
                            pick_up: tp_pick,
                        })


                        th.getopenuserid(tp_guide.id);

                    }
                }
            })
        })


    },

    //打电话
    call:function(e){
       var type=e.currentTarget.dataset.type;
       switch (type+''){
           case "1":
               wx.makePhoneCall({
                   phoneNumber: this.data.guide.staffTel,
               })
               break;
           case "2":
               wx.makePhoneCall({
                   phoneNumber: this.data.pick_up.pickup_phone,
               })
               break;
       }
    },

    change_code(){
        let is_qy_wx=!this.data.is_qy_wx;
        this.setData({
            is_qy_wx
        })
    },

    //获取qywxStaffcode 的信息
    getopenuserid(guide_id) {
        let url = "/api/weshop/qyWeChat/qywxStaffcode/getStaffcode";
        getApp().request.promisePost(url, {
            is_json:1,
            data: {
                store_id:os.stoid,
                guide_id:guide_id
            }
        }).then(async res => {
            console.log(res);
            if (res.data.code == 0) {
                // let resdata=JSON.parse(res.data.data);
                let openuserid = res.data.data.openuserid;
                let skip_verify = res.data.data.skip_verify;
                let guide_id =res.data.data.guide_id
                getApp().globalData.guide_id=guide_id

                this.setData({
                    openuserid,
                    skip_verify
                })

                this.befgetqy()

            }
        })
    },
    async getqy() {
        // wx.showLoading({
        //   title: '加载中',
        //   mask:true
        // })
        getApp().getConfig(async (e)=>{

            var conf=e;

            let openuserid = this.data.openuserid
            let skip_verify = this.data.skip_verify ? true : false
            let res = await getApp().request.promisePost(`/apib/weshop/admin/qyWeChat/qyWeChatCorpInfo/postAdminQyApi?accdb=${conf.erpid}&url=externalcontact/add_contact_way&qyid=4`, {
                data: {
                    // accdb: getApp().globalData.config.erpid,
                    // ApiName: 'api.pos.shop.advancesum.seek',
                    // url:'externalcontact/add_contact_way',
                    user: openuserid,
                    type: 1,
                    scene: 2,
                    style: 1,
                    state: '手店_' + this.data.phone,
                    skip_verify
                    // is_temp:true,
                    // store_id: getApp().globalData.setting.stoid,
                    // usercode: "admin",
                    // vipid: this.data.userinfo.erpvipid,
                    // oddsum: "0",
                    // advancesum: cartList.order_amount,
                    // storageid: storageid,
                    // listform
                },
                is_json: 1
            })
            wx.hideLoading()
            if (res.data && res.data.errcode == 0) {
                this.setData({
                    qr_code: res.data.qr_code
                })
                this.saveqy(res.data.config_id, res.data.qr_code)
            } else {
                let errcode = res.data.errcode
                let msg = errcode
                if (this.data.error_arr[errcode]) {
                    msg += this.data.error_arr[errcode]
                }
                wx.showModal({
                    title: '提示',
                    content: '获取失败,错误码:' + msg,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            }
            console.log('获取企业二维码');
            console.log(res);

        })


    },

    async befgetqy() {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        let openuserid = this.data.openuserid
        let res = await getApp().request.promiseGet(`/api/weshop/qyWeChat/qywxConfigList/getMobileInfo/${os.stoid}/${openuserid}/${this.data.phone}`, { data: {} })
        console.log('获取数据库表');
        console.log(res);
        if (res.data && res.data.code == 0) {
            this.setData({
                qr_code: res.data.data.qrcode
            })
            wx.hideLoading()
        } else {
            this.getqy()
        }

    },

    async saveqy(config_id, qrcode) {
        let openuserid = this.data.openuserid
        let mobile = this.data.phone
        let addtime = ((Date.now()) / 1000).toFixed(0)
        let req_data = {
            store_id: getApp().globalData.setting.stoid,
            openuserid,
            config_id,
            qrcode,
            mobile,
            addtime
        }
        let res = await getApp().request.promisePost(`/api/weshop/qyWeChat/qywxConfigList/save`, {
            data: req_data,
            // is_json: 1
        })
        // wx.hideLoading()
    },

    bind_bnerr2(e){
        this.setData({
            qr_code:this.data.imghots + "/miniapp/images/default_g_img.gif"
        })
    }


});
