import {
    callShare4MiniProgram
} from '../util/share';
import apis from '../util/apis1.js';
let {
    setShare,
    getMember,
    guest,
    focusShop
} = apis;
import {
    getUrlQueryValueByKey,
    changeStates
} from '../util/tools.js';
import store from '../store/store.js'; //砍价客态

//会员分享（需要判断是否是会员）
const memberShare = (isTemplate, context, callBack) => {
    let actStatus = sessionStorage.getItem('actStatus');
    console.log(actStatus, 'actStatus');
    if (actStatus == 2) {
        context.$toast('活动已结束');
        return;
    }
    if (actStatus == 1) {
        context.$toast('活动尚未开始');
        return;
    }
    if (isTemplate == false) {
        if (actStatus == 3) {
            getMember(data => {
                if (data.data.status == 1) {
                    setShare(data => {
                        var cur_url = location.href;
                        var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
                        var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
                        if (!shareUserId) {
                            var invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
                        } else {
                            var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
                        }

                        console.log(invitionUrl, 'invitionUrl')
                        if (store.state.clientType == '01') {
                            var title = store.state.shareTitle;
                            var content = store.state.shareTitle;
                            var logoUrl = store.state.shareImage;
                            var mpIconUrl = store.state.shareImage;

                            var isHeader = false; //false 头部分享 true 按钮分享
                            var url = invitionUrl;
                            var shopid = store.state.shopId;
                            var userpin = store.state.userPin;
                            var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId

                            callShare4MiniProgram(
                                title,
                                content,
                                url,
                                logoUrl,
                                callBack => {
                                    console.log('测试分享');
                                },
                                isHeader,
                                mpIconUrl,
                                shopid,
                                userpin,
                                jdactivityid
                            );
                        }
                        callBack('success')
                    });
                } else {
                    console.log('请先开通会员');
                }
            });
        }
    } else {
        context.$toast('活动预览，仅供查看');
    }
}

//全员可分享（不需要判断是否是会员）
const allShare = (isTemplate, context, callBack) => {
    let actStatus = sessionStorage.getItem('actStatus');
    console.log(actStatus, 'actStatus');
    if (actStatus == 2) {
        context.$toast('活动已结束');
        return;
    }
    if (actStatus == 1) {
        context.$toast('活动尚未开始');
        return;
    }
    if (isTemplate == false) {
        if (actStatus == 3) {
            let uid = sessionStorage.getItem('uid');
            if(uid){
                var cur_url = location.href;
                console.log(cur_url, 'cur_url213123',store.state.shareImage,store.state.shareTitle)
                var invitionUrl = cur_url + '&uid=' + uid;
                if (store.state.clientType == '01' || store.state.clientType == '02') {
                    console.log("分享111111111111111111111")
                    var title = store.state.shareTitle;
                    var content = store.state.shareTitle;
                    var logoUrl = store.state.shareImage;
                    var mpIconUrl = store.state.shareImage;

                    var isHeader = false; //false 头部分享 true 按钮分享
                    var url = invitionUrl;
                    var shopid = store.state.shopId;
                    var userpin = store.state.userPin;
                    var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
                    callShare4MiniProgram(
                        title,
                        content,
                        url,
                        logoUrl,
                        callBack => {
                            console.log('测试分享');
                            store.commit("setFenXiang", true);
                        },
                        isHeader,
                        mpIconUrl,
                        shopid,
                        userpin,
                        jdactivityid
                    );
                    callBack('success')
                }

            }else{
                setShare(data => {
                    var cur_url = location.href;
                    console.log(cur_url, 'cur_url213123',store.state.shareImage,store.state.shareTitle)
                    var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
                    var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
    
                    if (!shareUserId) {
                        var invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
                    } else {
                        var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
                    }
    
    
                    if (store.state.clientType == '01' || store.state.clientType == '02') {
                        console.log("分享111111111111111111111")
                        var title = store.state.shareTitle;
                        var content = store.state.shareTitle;
                        var logoUrl = store.state.shareImage;
                        var mpIconUrl = store.state.shareImage;
    
                        var isHeader = false; //false 头部分享 true 按钮分享
                        var url = invitionUrl;
                        var shopid = store.state.shopId;
                        var userpin = store.state.userPin;
                        var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
                        callShare4MiniProgram(
                            title,
                            content,
                            url,
                            logoUrl,
                            callBack => {
                                console.log('测试分享');
                                store.commit("setFenXiang", true);
                            },
                            isHeader,
                            mpIconUrl,
                            shopid,
                            userpin,
                            jdactivityid
                        );
                        callBack('success')
                    }
                });

            }
          
        }
    } else {
        context.$toast('活动预览，仅供查看');
    }
}

// 分享商品 店铺、活动
const allShareByurl = (urlPara, isTemplate, context, callBack) => {
        let actStatus = sessionStorage.getItem('actStatus');
        console.log(actStatus, 'actStatus');
        if (actStatus == 2) {
            context.$toast('活动已结束');
            return;
        }
        if (actStatus == 1) {

            context.$toast('活动尚未开始');
            return;
        }

        if (actStatus == 3) {

            setShare(data => {
                var cur_url = urlPara;
                var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
                var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
                // if (!shareUserId) {
                var invitionUrl = cur_url //aUrl + '&shareUserId=' + data.data.shareUserId;
                    // } else {
                    //     var invitionUrl = location.href + data.data.shareUserId;
                    // }
                if (store.state.clientType == '01') {
                    var title = store.state.shareTitle;
                    var content = store.state.shareTitle;
                    var logoUrl = store.state.shareImage;
                    var mpIconUrl = store.state.shareImage;

                    var isHeader = false; //false 头部分享 true 按钮分享
                    var url = invitionUrl;
                    var shopid = store.state.shopId;
                    var userpin = store.state.userPin;
                    var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId

                    callShare4MiniProgram(
                        title,
                        content,
                        url,
                        logoUrl,
                        callBack => {
                            console.log('测试分享');
                            store.commit("setFenXiang", true);
                        },
                        isHeader,
                        mpIconUrl,
                        shopid,
                        userpin,
                        jdactivityid
                    );
                    callBack('success')
                }
            });
        }

    }
    //关注店铺后分享
const focusShopShare = (context, isTemplate, paramsData, callBack) => {
        let actStatus = sessionStorage.getItem('actStatus');
        let isFollowShop = paramsData.isFollowShop
        if (actStatus == 2) {
            context.$toast('活动已结束');
            return;
        }
        if (actStatus == 1) {
            context.$toast('活动尚未开始');
            return;
        }
        if (isTemplate == false) {
            //需要关注店铺
            if (isFollowShop == 1) {
                focusShop((data) => {
                    console.log(data, '关注店铺')
                    if (data.resp_code != 0) {
                        context.$toast('关注店铺失败');
                        return;
                    }
                    setShare(data => {
                        var cur_url = location.href;
                        var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
                        var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
                        if (!shareUserId) {
                            var invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
                        } else {
                            var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
                        }
                        if (store.state.clientType == '01') {
                            var title = store.state.shareTitle;
                            var content = store.state.shareTitle;
                            var logoUrl = store.state.shareImage;
                            var mpIconUrl = store.state.shareImage;

                            var isHeader = false; //false 头部分享 true 按钮分享
                            var url = invitionUrl;
                            var shopid = store.state.shopId;
                            var userpin = store.state.userPin;
                            var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId

                            callShare4MiniProgram(
                                title,
                                content,
                                url,
                                logoUrl,
                                callBack => {
                                    console.log('测试分享');
                                },
                                isHeader,
                                mpIconUrl,
                                shopid,
                                userpin,
                                jdactivityid
                            );
                            callBack('success')
                        }
                    });
                })
            } else {
                //不需要关注店铺
                setShare(data => {

                    var cur_url = location.href;
                    var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
                    var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
                    // if (!shareUserId) {
                    if (!shareUserId) {
                        var invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
                    } else {
                        var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
                    }
                    // var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId;
                    // } else {
                    //     var invitionUrl = location.href + data.data.shareUserId;
                    // }
                    console.log(invitionUrl, "a")

                    console.log(store.state.clientType);
                    if (store.state.clientType == '01') {
                        var title = store.state.shareTitle;
                        var content = store.state.shareTitle;
                        var logoUrl = store.state.shareImage;
                        var mpIconUrl = store.state.shareImage;

                        var isHeader = false; //false 头部分享 true 按钮分享
                        var url = invitionUrl;
                        var shopid = store.state.shopId;
                        var userpin = store.state.userPin;
                        var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
                        console.log(title, content, url, logoUrl, isHeader, mpIconUrl, shopid, userpin, jdactivityid);
                        callShare4MiniProgram(
                            title,
                            content,
                            url,
                            logoUrl,
                            callBack => {
                                console.log('测试分享');
                            },
                            isHeader,
                            mpIconUrl,
                            shopid,
                            userpin,
                            jdactivityid
                        );
                        callBack('success')
                    }
                });
            }

        } else {
            context.$toast('活动预览，仅供查看');
        }

    }
    //中奖弹框点击分享按钮
const prizeShare = (isTemplate, context, callBack) => {
    let actStatus = sessionStorage.getItem('actStatus');
    console.log(actStatus, 'actStatus');
    if (actStatus == 2) {
        context.$toast('活动已结束');
        return;
    }
    if (actStatus == 1) {
        context.$toast('活动尚未开始');
        return;
    }
    if (store.state.clientType == '01') {
        var title = store.state.shareTitle;
        var content = store.state.shareTitle;
        var logoUrl = store.state.shareImage;
        var mpIconUrl = store.state.shareImage;

        var isHeader = false; //true 头部分享 false 按钮分享
        var url = location.href;
        var shopid = store.state.shopId;
        var userpin = store.state.userPin;
        var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId

        callShare4MiniProgram(
            title,
            content,
            url,
            logoUrl,
            callBack => {
                console.log('测试分享');
            },
            isHeader,
            mpIconUrl,
            shopid,
            userpin,
            jdactivityid
        );
        callBack('success')
    }
}

//中奖弹框点击分享按钮
const teamShare = (isTemplate, teamId, context, callBack) => {
    let actStatus = sessionStorage.getItem('actStatus');
    console.log(actStatus, 'actStatus');
    if (actStatus == 2) {
        context.$toast('活动已结束');
        return;
    }
    if (actStatus == 1) {
        context.$toast('活动尚未开始');
        return;
    }
    if (isTemplate) {
        this.$toast("活动预览，仅供查看");
        return;
    }
    if (actStatus == 3) {
        setShare(data => {
            var cur_url = location.href;
            console.log(cur_url, 'cur_url213123')
                // 获取连接里的shareuserid
            var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);

            // 提出纯连接
            var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));

            if (!shareUserId) {
                var invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
            } else {
                var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
            }

            invitionUrl = invitionUrl + '&teamId=' + teamId;

            console.log(invitionUrl, 'invitionUrl')
            if (store.state.clientType == '01') {
                var title = store.state.shareTitle;
                var content = store.state.shareTitle;
                var logoUrl = store.state.shareImage;
                var mpIconUrl = store.state.shareImage;

                var isHeader = false; //false 头部分享 true 按钮分享
                var url = invitionUrl;
                var shopid = store.state.shopId;
                var userpin = store.state.userPin;
                var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId

                callShare4MiniProgram(
                    title,
                    content,
                    url,
                    logoUrl,
                    callBack => {
                        console.log('测试分享');
                    },
                    isHeader,
                    mpIconUrl,
                    shopid,
                    userpin,
                    jdactivityid
                );
                callBack('success')
            }
        });
    }
}

//定制全员可分享（不需要判断是否是会员）
const dzallShare = (uid, callBack) => {
    var cur_url = location.href;
    console.log(cur_url, 'cur_url213123')
    var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
    var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));

    if (!shareUserId) {
        var invitionUrl = cur_url + '&shareUserId=' + uid;
    } else {
        var invitionUrl = aUrl + '&shareUserId=' + uid // location.href + uid;
    }
    if (store.state.clientType == '01' || store.state.clientType == '02') {
        var shareImage = 'https://img10.360buyimg.com/imgzone/jfs/t1/143792/32/31831/44646/63abb0b0Fd2101d2b/0dfb447c655f7a24.jpg'
        console.log(invitionUrl,"分享111111111111111111111")
        var title = "助我赢羽西正装星品！";
        var content = "助我赢羽西正装星品！";
        var logoUrl = shareImage;
        var mpIconUrl = shareImage;

        var isHeader = false; //true 头部分享 false 按钮分享
        var url = invitionUrl;
        var shopid = store.state.shopId;
        var userpin = store.state.userPin;
        var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
        
        callShare4MiniProgram(
            title,
            content,
            url,
            logoUrl,
            callBack => {
                console.log('测试分享');
                store.commit("setFenXiang", true);
            },
            isHeader,
            mpIconUrl,
            shopid,
            userpin,
            jdactivityid
        );
        callBack('success')
    }
}
//ysl定制全员可分享（不需要判断是否是会员）
const yslDzallShare = (uid, callBack) => {
    var cur_url = location.href + '&type=2';
    var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
    var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));

    if (!shareUserId) {
        var invitionUrl = cur_url + '&shareUserId=' + uid + '&type=2';
    } else {
        var invitionUrl = aUrl + '&shareUserId=' + uid + '&type=2'; // location.href + uid;
    }
    if (store.state.clientType == '01' || store.state.clientType == '02') {
        var title = store.state.shareTitle;
        var content = store.state.shareTitle;
        var logoUrl = store.state.shareImage;
        var mpIconUrl = store.state.shareImage;

        var isHeader = false; //true 头部分享 false 按钮分享
        var url = invitionUrl;
        var shopid = store.state.shopId;
        var userpin = store.state.userPin;
        var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
        
        callShare4MiniProgram(
            title,
            content,
            url,
            logoUrl,
            callBack => {
                console.log('测试分享');
                store.commit("setFenXiang", true);
            },
            isHeader,
            mpIconUrl,
            shopid,
            userpin,
            jdactivityid
        );
        callBack('success')
    }
}

export {
    memberShare,
    allShare,
    allShareByurl,
    prizeShare,
    focusShopShare,
    teamShare,
    dzallShare,
    yslDzallShare
};