const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
const config = require("../../config/config");
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("我是中转页面")
    let that = this;
    console.log(options);
    tt.showToast({
      title: '加载中...',
      icon: "loading",
      duration: 2000
    })

    /**
     * 根据接收到的参数的值，判断跳转到哪个页面
     */
    if (options && options.type) {
      app.globalData.type = options.type;
    }
    if (decodeURIComponent(options.scene) == "undefined") {
      that.setData({
        scene: 0
      })
      app.globalData.scene = 0;
    } else {
      that.setData({
        scene: decodeURIComponent(options.scene)
      });
      app.globalData.scene = decodeURIComponent(options.scene)
      that.setData({
        newArr: this.data.scene.split(':')
      })
      //// 大屏-二维码-start
      if (this.data.newArr[0] == 'JC') {
        // 跳转券详情
        options.couponChannelId = this.data.newArr[1];
      } else if (this.data.newArr[0] == 'JG') {
        // 跳转拼团详情
        options.couponChannelId = this.data.newArr[1];
        options.path = 'daping'
      } else if (this.data.newArr[0] == 't') {
        // 普通二维码
        options.type = this.data.newArr[1];
        app.globalData.type = options.type;
        if (options.type == 'cd') {
          options.couponChannelId = this.data.newArr[2];
          options.tenantId = this.data.newArr[3];
        } else if (options.type == 'bd') {
          options.bt = this.data.newArr[2];
          options.id = this.data.newArr[3];
        } else if (options.type == 'gm') {
          options.id = this.data.newArr[2];
        } else if (options.type == 'sd') {
          options.couponChannelId = this.data.newArr[2];
        } else if (options.type == 'td') {
          // 专题活动页
          options.id = this.data.newArr[2];
        } else if (options.type == 'md') {
          // 门店详情
          options.id = this.data.newArr[2];
        } else if (options.type == 'ra') {
          //活动详情
          options.id = this.data.newArr[2];
        } else if (options.type == 'si') {
          //活动签到
          options.id = this.data.newArr[2];
        } else if (options.type == 'wj') {
          options.id = this.data.newArr[2];
        } else if (options.type == 'ts') {
          options.id = this.data.newArr[2];
        } else if (options.type == 'ts') {
          options.id = this.data.newArr[2];
        }
      }
    }

    tt.setStorageSync('options', JSON.stringify(options))
    tt.setStorageSync('imgurl', imgurl);

    if (app.globalData.token) {

      that.getCongig()
      this.updateUserInfo();
      if (options.couponChannelId || options.orderId || options.id) {
        that.userStatus(options);
      } else {
        that.userStatus();
      }
    } else {
      // 由于 token 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.tokenCallback = token => {
        that.getCongig()
        this.updateUserInfo();
        if (options.couponChannelId || options.orderId || options.id) {

          that.userStatus(options);
        } else {
          that.userStatus();
        }
      }
    }
  },
  getCongig() { //获取小程序是否开通授权手机号
    Http.get({
      url: config.api.getConfig,
      data: {
        key: "douyinPhoneStatus"
      }
    }).then(res => {
      app.globalData.ifCongPh = res.data ? res.data : 1
    }).catch(err => {
      app.globalData.ifCongPh = 1
    })
  },

  /**
   * 检查用户的状态
   */
  userStatus: function (options) {

    var that = this;
    if (options && (options.couponChannelId || options.orderId || options.id)) {
      that.checkuserstatus(options);
    } else {
      that.checkuserstatus();
    }
  },
  /**
   * 获取手机信息
   */
  updateUserInfo() {
    tt.getSystemInfo({
      success: function (res) {
        Http.post({
          url: config.api.updateUserInfo,
          data: {
            systemInfo: JSON.stringify(res)
          }
        }).then(res => {
          console.log(res)
        })
      }
    })
  },
  checkuserstatus(options) {
    let that = this;
    console.log("判断是否是商品详情");

    if ((options && options.couponChannelId && !options.spellGroup && app.globalData.type != 'sd')) {
      if (options.cuserId) {
        tt.redirectTo({
          url: `/pages/coupon/detail/index?couponChannelId=${options.couponChannelId}&cuserId=${options.cuserId}&coverImg=${options.coverImg}&userName=${options.userName}&avatarUrl=${options.avatarUrl}&couponOrderId=${options.couponOrderId}&updateDate=${options.updateDate}&mallTenantId=${options.tenantId}`,
        })
      } else {
        // 跳转普通券/消费卡/限时秒杀/砍价详情
        tt.redirectTo({
          url: `/pages/coupon/detail/index?couponChannelId=${options.couponChannelId}&mallTenantId=${options.tenantId}`,
        })
      }
    } else {
      Http.get({
          url: config.api.checkUserStatus,
          data: {
            token: app.globalData.token
          }
        })
        .then(res => {
          // res = JSON.parse('{"code":11004,"message":"用户昵称未授权，请跳转到用户昵称授权页！","data":{}}')
          //参与拼团
          if (options && options.orderGroupId) {
            tt.redirectTo({
              url: `/pages/joinFrDpell/index?couponId=${options.couponId}&orderGroupId=${options.orderGroupId}&couponChannelId=${options.couponChannelId}&orderId=${options.orderId}&avatarUrl=${options.avatarUrl}&nickName=${options.nickName}`
            })
          }
          //来自大屏的跳转拼团券详情
          else if (options && options.couponChannelId && options.path == 'daping') {
            /**
             * 主要是为了拿couponId
             */
            Http.get({
              url: config.api.couponDetail,
              data: {
                couponChannelId: options.couponChannelId
              }
            }).then(res => {
              let data = res.data;
              tt.redirectTo({
                url: `/pages/spellGroup/mySpellGroup/index?couponChannelId=${options.couponChannelId}&couponId=${data.couponId}`
              })
            })
          } else if (options && options.couponChannelId && !options.spellGroup && app.globalData.type != 'sd') {
            console.log(3333333333333333)
            // 转赠判断
            if (options.cuserId) {
              tt.redirectTo({
                url: `/pages/coupon/detail/index?couponChannelId=${options.couponChannelId}&cuserId=${options.cuserId}&coverImg=${options.coverImg}&userName=${options.userName}&avatarUrl=${options.avatarUrl}&couponOrderId=${options.couponOrderId}&updateDate=${options.updateDate}`,
              })
            } else {
              // 跳转普通券/消费卡/限时秒杀/砍价详情
              tt.redirectTo({
                url: `/pages/coupon/detail/index?couponChannelId=${options.couponChannelId}`,
              })
            }
          }
          // 拼团详情页(只有通过扫码进入才可能进入拼团详情页面，只有这一个入口)
          else if ((options && options.couponChannelId && app.globalData.type == 'sd') || (options && options.couponChannelId && options.spellGroup)) {
            tt.redirectTo({
              url: `/pages/spellGroup/mySpellGroup/index?couponChannelId=${options.couponChannelId}`,
            })
          }
          // 游戏页面
          else if (options && options.id && app.globalData.type == 'gm') {
            that.getGameOne(app.globalData.token, options.id)
          }
          // 报名活动页面
          else if (options && options.id && app.globalData.type == 'ra') {
            tt.redirectTo({
              url: `/pages/radetail/index?id=${options.id}`,
            })
          }
          //活动签到
          else if (options && options.id && app.globalData.type == 'si') {
            tt.redirectTo({
              url: `/pages/acSignIn/index?id=${options.id}`,
            })
          }
          //答题问卷
          else if (options && options.id && app.globalData.type == 'wj') {
            tt.redirectTo({
              url: `/pages/questionnaire/questionnaire?id=${options.id}`,
            })
          }
          //投诉建议
          else if (app.globalData.type == 'ts') {
            tt.redirectTo({
              url: `/pages/complaint/complaint`,
            })
          }
          // 宣传页详情
          else if (options && options.bt && options.id && app.globalData.type == 'bd') {
            if (options.bt == '2') {
              // 自由图文
              tt.redirectTo({
                url: `/pages/freeBannerDetail/index?id=${options.id}`,
              })
            } else {
              // 标准格式
              tt.redirectTo({
                url: `/pages/bannerdetail/index?id=${options.id}`,
              })
            }
          }
          // 专题
          else if (options && options.id && app.globalData.type == 'td') {
            tt.redirectTo({
              url: `/pages/topicDetail/index?id=${options.id}`
            });
          }
          // 门店详情
          else if (options && options.id && app.globalData.type == 'md') {
            tt.redirectTo({
              url: `/pages/index/searchbar/detail/index?id=${options.id}`
            });
          }
          // 参与砍价
          else if (options && options.orderId) {
            tt.redirectTo({
              url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${options.orderId}&from=${"discount"}&composeOrderType=${options.composeOrderType}`,
            })
          }
          // 我的
          else if (app.globalData.type == 'uc') {
            tt.navigateTo({
              url: '/index/user'
            });
          }
          // 积分
          else if (app.globalData.type == 'jf') {
            tt.redirectTo({
              url: '/pages/integralmall/index'
            });
          }
          // 首页
          else if (app.globalData.type == 'in') {
            tt.reLaunch({
              url: `/index/index`
            });
          }
          // 停车
          else if (app.globalData.type == 'pc') {
            // 停车
            tt.navigateTo({
              url: '/index/passCar'
            });
          }
          // 我的券包
          else if (app.globalData.type == 'mc') {
            // 我的券包
            tt.redirectTo({
              url: '/pages/couponorder/index/index'
            });
          }
          // 我的订单
          else if (app.globalData.type == 'mo') {
            // 我的订单
            tt.redirectTo({
              url: '/pages/order/index/index?id=all'
            });
          }
          // 我的卡包
          else if (app.globalData.type == 'ca') {
            // 我的卡包
            tt.redirectTo({
              url: '/pages/cardorder/index/index'
            });
          }
          // 特享礼遇
          else if (app.globalData.type == 'sc') {
            // 特享礼遇
            tt.redirectTo({
              url: '/pages/specialcourtesy/specialcourtesy'
            });
          }
          // 限时秒杀
          else if (app.globalData.type == 'rb') {
            // 限时秒杀
            tt.redirectTo({
              url: '/pages/rushToBuy/index'
            })
          }
          // 砍价专场
          else if (app.globalData.type == 'bl') {
            // 砍价专场
            tt.redirectTo({
              url: '/pages/bargain/bargain'
            })
          }
          // 我的砍价
          else if (app.globalData.type == 'mb') {
            // 我的砍价
            tt.redirectTo({
              url: '/pages/bargain/bargain?from=myhtml'
            })
          }
          // 拼团专场
          else if (app.globalData.type == 'sl') {
            // 拼团专场
            tt.redirectTo({
              url: '/pages/spellGroup/spellGroup'
            })
          }
          // 我的拼团
          else if (app.globalData.type == 'ms') {
            // 我的拼团
            tt.redirectTo({
              url: '/pages/spellGroup/spellGroup?from=myhtml'
            })
          }
          // 消费卡
          else if (app.globalData.type == 'dc') {
            // 消费卡
            tt.redirectTo({
              url: '/pages/discountCardList/discountCardList'
            })
          }
          //直播
          else if (app.globalData.type == 'zb') {
            tt.redirectTo({
              url: `/pages2/live/livelist/index`
            });
          }
          //活动日历
          else if (app.globalData.type == 'arl') {
            tt.redirectTo({
              url: `/pages/dateLsit/dateLsit`
            });
          }
          //每日签到
          else if (app.globalData.type == 'qd') {
            tt.redirectTo({
              url: `/pages/activityCalendar/activityCalendar`
            });
          }
          // 门店
          else if (app.globalData.type == 'ml') {
            tt.navigateTo({
              url: '/index/searchbar'
            })
          }
          // 兑换
          else if (app.globalData.type == 'ec') {
            tt.redirectTo({
              url: '/pages/exchange/exchange'
            })
          }
          // 通过分享进入的门店详情
          else if (options && options.id && options.frommd == 'md' && app.globalData.type == 'md') {
            tt.redirectTo({
              url: `/pages/index/searchbar/detail/index?id=${options.id}`
            });
          } else {
            // 主页
            tt.reLaunch({
              url: '/index/index'
            })
          }
        })
        .catch(err => {
          tt.showToast({
            title: err,
            icon: "loading",
            duration: 2000
          })
          if (err.code == 11004) {
            // 用户昵称未授权
            if (options && options.orderGroupId) {
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?couponId=${options.couponId}&orderGroupId=${options.orderGroupId}&couponChannelId=${options.couponChannelId}&orderId=${options.orderId}&avatarUrl=${options.avatarUrl}&nickName=${options.nickName}`
              })
            }
            /**
             * spellGroup
             * 区分是通过拼团详情分享过来的还是从普通券分享过来的
             * 注意：拼团券和普通券是两个页面
             */
            if (options && options.couponChannelId && !options.cuserId && !options.spellGroup) {
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?couponChannelId=${options.couponChannelId}`
              })
            } else if (options && options.couponChannelId && !options.cuserId && options.spellGroup) {
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?couponChannelId=${options.couponChannelId}&spellGroup=${options.spellGroup}`
              })
            } else if (options && options.couponChannelId && options.cuserId) {
              /**
               * 转赠判断
               */
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?couponChannelId=${options.couponChannelId}&cuserId=${options.cuserId}&coverImg=${options.coverImg}&userName=${options.userName}&avatarUrl=${options.avatarUrl}&couponOrderId=${options.couponOrderId}&updateDate=${options.updateDate}`
              })
            } else if (options && options.orderId) {
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?orderId=${options.orderId}&from='${"discount"}`
              })
            }
            // 报名活动页面
            else if (options && options.id && app.globalData.type == 'ra') {
              tt.redirectTo({
                url: `/pages/radetail/index?id=${options.id}`,
              })
            }
            // 通过分享进入的门店详情
            else if (options && options.id && options.frommd == 'md' && app.globalData.type == 'md') {
              tt.redirectTo({
                url: `/pages/getuserinfo/getuserinfo?id=${options.id}&frommd=md`
              });
            } // 门店详情
            else if (options && options.id && app.globalData.type == 'md') {
              tt.redirectTo({
                url: `/pages/index/searchbar/detail/index?id=${options.id}`
              });
            } else if (app.globalData.type == 'uc') {
              tt.navigateTo({
                url: '/index/user'
              });
            }
            // 积分
            else if (app.globalData.type == 'jf') {
              tt.redirectTo({
                url: '/pages/integralmall/index'
              });
            }
            // 首页
            else if (app.globalData.type == 'in') {
              tt.reLaunch({
                url: '/index/index'
              });
            }
            // 停车
            else if (app.globalData.type == 'pc') {
              // 停车
              tt.navigateTo({
                url: '/index/passCar'
              });
            }
            // 我的券包
            else if (app.globalData.type == 'mc') {
              // 我的券包
              tt.redirectTo({
                url: '/pages/couponorder/index/index'
              });
            }
            // 我的订单
            else if (app.globalData.type == 'mo') {
              // 我的订单
              tt.redirectTo({
                url: '/pages/order/index/index?id=all'
              });
            }
            // 我的卡包
            else if (app.globalData.type == 'ca') {
              // 我的卡包
              tt.redirectTo({
                url: '/pages/cardorder/index/index'
              });
            }
            // 特享礼遇
            else if (app.globalData.type == 'sc') {
              // 特享礼遇
              tt.redirectTo({
                url: '/pages/specialcourtesy/specialcourtesy'
              });
            }
            // 限时秒杀
            else if (app.globalData.type == 'rb') {
              // 限时秒杀
              tt.redirectTo({
                url: '/pages/rushToBuy/index'
              })
            }
            // 砍价专场
            else if (app.globalData.type == 'bl') {
              // 砍价专场
              tt.redirectTo({
                url: '/pages/bargain/bargain'
              })
            }
            // 我的砍价
            else if (app.globalData.type == 'mb') {
              // 我的砍价
              tt.redirectTo({
                url: '/pages/bargain/bargain?from=myhtml'
              })
            }
            // 拼团专场
            else if (app.globalData.type == 'sl') {
              // 拼团专场
              tt.redirectTo({
                url: '/pages/spellGroup/spellGroup'
              })
            }
            // 我的拼团
            else if (app.globalData.type == 'ms') {
              tt.redirectTo({
                url: '/pages/spellGroup/spellGroup?from=myhtml'
              })
            }
            // 消费卡
            else if (app.globalData.type == 'dc') {
              // 消费卡
              tt.redirectTo({
                url: '/pages/discountCardList/discountCardList'
              })
            }
            // 兑换
            else if (app.globalData.type == 'ec') {
              tt.redirectTo({
                url: '/pages/exchange/exchange'
              })
            }
            // 门店
            else if (app.globalData.type == 'ml') {
              tt.navigateTo({
                url: '/index/searchbar'
              })
            } else {
              tt.reLaunch({
                url: '/index/index',
              })
            }
          }
        })
    }


  },

  getGameOne: function (token, id) {
    let _this = this;
    Http.get({
        url: config.api.getOneGame,
        data: {
          token: token,
          id: id
        }
      }).then(res => {
        tt.redirectTo({
          url: '/pages/game/index?url=' + res.data.url + "&id=" + res.data.id + "&gameId=" + res.data.gameId,
        })
      })
      .catch(err => {
        tt.showModal({
          title: '提示',
          content: err.message,
          showCancel: false,
          success: function (res) {
            // 如果游戏下架或者找不到，重启首页
            if (res.confirm) {
              tt.reLaunch({
                url: '/pages/index/index',
              })
            }
          }
        })
        this.alphaClick();
      })
  },
})