import {
  callShare4MiniProgram,
  callShare4MiniProgramDz
} from '../util/share';
import apis from '../util/apis1.js';
let {
  setShare,
  getMember,
  guest,
  focusShop,
  shareTask,
} = 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) {
      setShare(data => {
        var cur_url = sessionStorage.getItem('activityUrl');//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=' + data.data.shareUserId;
        } else {
          var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
        }
        console.log(invitionUrl, 'invitionUrlinvitionUrl')


        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 allShareDz = (isTemplate, context, callBack) => {
  let actStatus = sessionStorage.getItem('actStatus');
  if (actStatus == 2) {
    context.$toast('活动已结束');
    return;
  }
  if (actStatus == 1) {
    context.$toast('活动尚未开始');
    return;
  }
  console.log(isTemplate == false, actStatus == 3, 'actStatus == 3')
  if (isTemplate == false) {
    if (actStatus == 3) {
      setShare(data => {
        var cur_url = sessionStorage.getItem('activityUrl');//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, 'invitionUrlinvitionUrl')


        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; //false 头部分享 true 按钮分享
          var url = invitionUrl;
          var shopid = store.state.shopId;
          var userpin = store.state.userPin;
          var jdactivityid = store.state.jdActivityId; //'1356071125718585346'//store.state.activityId
            callShare4MiniProgramDz(
            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 allShareByurlNew = (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 = sessionStorage.getItem('activityUrl'); //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 = sessionStorage.getItem('activityUrl'); //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 allShareGoods = (isTemplate, params, context, callBack) => {
  let actStatus = sessionStorage.getItem('actStatus');
  console.log(isTemplate, context, callBack, 'actStatus');
  if (actStatus == 2) {
    context.$toast('活动已结束');
    return;
  }
  if (actStatus == 1) {
    context.$toast('活动尚未开始');
    return;
  }
  console.log(isTemplate == false, actStatus == 3, 'actStatus == 3')
  if (isTemplate == false) {
    if (actStatus == 3) {
      setShare(data => {
        console.log(data, 'dataaaaaaaaaaaaa')
        var cur_url = sessionStorage.getItem('activityUrl');//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=' + data.data.shareUserId + '&prizeInfoId=' + params.prizeInfoId;
        } else {
          var invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId + '&prizeInfoId=' + params.prizeInfoId;// location.href + data.data.shareUserId;
        }
        console.log(invitionUrl, 'invitionUrlinvitionUrl')


        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 toDoShare = (item,isTemplate, context, callBack) => {
  let actStatus = sessionStorage.getItem('actStatus');
  console.log(item,isTemplate, context, callBack, 'actStatus');
  if (actStatus == 2) {
    context.$toast('活动已结束');
    return;
  }
  if (actStatus == 1) {
    context.$toast('活动尚未开始');
    return;
  }
  if (isTemplate == false) {
    if (actStatus == 3) {
      setShare(data => {
        var cur_url = sessionStorage.getItem('activityUrl');//location.href;
        var shareUserId = getUrlQueryValueByKey('shareUserId', cur_url);
        var aUrl = cur_url.substring(0, cur_url.indexOf('shareUserId'));
        var invitionUrl = ''
        if(item.taskType == 9){
           // 分享指定商品
           invitionUrl = 'https://item.m.jd.com/product/'+
              item.skuInfoVO[0].skuId +
          '.html?wjfrom=ISV_2180A2AFA5B2452F9061C01C053C4756_WX&wjwxpubid=wxd9fd3549c5fdb283&wjunionid=&wjopenid=';
        }else if(item.taskType == 10 || item.taskType == 11){
          //分享指定店铺 //分享指定会场 Todo
           invitionUrl = 'https://shop.m.jd.com/?shopId=' + item.shopId;
        }else{
          if (!shareUserId) {
            invitionUrl = cur_url + '&shareUserId=' + data.data.shareUserId;
          } else {
           invitionUrl = aUrl + '&shareUserId=' + data.data.shareUserId // location.href + data.data.shareUserId;
          }
        }
        console.log(invitionUrl,"invitionUrlinvitionUrl");
        
       if(data.resp_code == 0){
        shareTask(item.taskId,'',res=>{
          context.publicData.$emit('fansPrizeComponent', true)
          if(res.resp_code == 0){
            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; //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('活动预览，仅供查看');
  }
}


export {
  memberShare,
  allShare,
  allShareByurl,
  prizeShare,
  focusShopShare,
  teamShare,
  allShareGoods,
  allShareByurlNew,
  toDoShare,
  allShareDz
};
