const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
const config = require("../../config/config");
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options, "跳转地址栏");
    wx.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];
        } 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];
        }
      }
    }

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

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

  /**
   * 检查用户的状态
   */
  userStatus: function (options) {
    var that = this;
    if (options && (options.couponChannelId || options.orderId || options.id)) {
      that.checkuserstatus(options);
    } else {
      that.checkuserstatus();
    }
  },
  /**
   * 获取手机信息
   */
  // updateUserInfo() {
  //   wx.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;

    Http.get({
      url: config.api.checkUserStatus,
      data: {
        token: app.globalData.token
      }
    })
      .then(res => {
        // res = JSON.parse('{"code":11004,"message":"用户昵称未授权，请跳转到用户昵称授权页！","data":{}}')
        //参与拼团
        if (options && options.orderGroupId) {
          wx.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;
            wx.redirectTo({
              url: `/pages/spellGroup/mySpellGroup/index?couponChannelId=${options.couponChannelId}&couponId=${data.couponId}`
            })
          })
        } else if (options && options.couponChannelId && !options.spellGroup && app.globalData.type != 'sd') {
          // 转赠判断
          if (options.cuserId) {
            wx.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 {
            // 跳转普通券/消费卡/限时秒杀/砍价详情
            wx.redirectTo({
              url: `/pages/coupon/detail/index?couponChannelId=${options.couponChannelId}`,
            })
          }
        }
        // 拼团详情页(只有通过扫码进入才可能进入拼团详情页面，只有这一个入口)
        else if ((options && options.couponChannelId && app.globalData.type == 'sd') || (options && options.couponChannelId && options.spellGroup)) {
          wx.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') {
          wx.redirectTo({
            url: `/pages/radetail/index?id=${options.id}`,
          })
        }
        //活动签到
        else if (options && options.id && app.globalData.type == 'si') {
          wx.redirectTo({
            url: `/pages/acSignIn/index?id=${options.id}`,
          })
        }
        //问卷答题
        else if (options && options.id && app.globalData.type == 'wj') {
          wx.redirectTo({
            url: `/pages/questionnaire/questionnaire?id=${options.id}`,
          })
        }
        // 宣传页详情
        else if (options && options.bt && options.id && app.globalData.type == 'bd') {
          if (options.bt == '2') {
            // 自由图文
            wx.redirectTo({
              url: `/pages/freeBannerDetail/index?id=${options.id}`,
            })
          } else {
            // 标准格式
            wx.redirectTo({
              url: `/pages/bannerdetail/index?id=${options.id}`,
            })
          }
        }
        // 专题
        else if (options && options.id && app.globalData.type == 'td') {
          wx.redirectTo({
            url: `/pages/topicDetail/index?id=${options.id}`
          });
        }
        // 门店详情
        else if (options && options.id && app.globalData.type == 'md') {
          wx.redirectTo({
            url: `/pages/index/searchbar/detail/index?id=${options.id}`
          });
        }
        // 参与砍价
        else if (options && options.orderId) {
          wx.redirectTo({
            url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${options.orderId}&from=${"discount"}`,
          })
        }
        // 我的
        else if (app.globalData.type == 'uc') {
          wx.switchTab({
            url: '/index/user'
          });
        }
        // 积分
        else if (app.globalData.type == 'jf') {
          wx.redirectTo({
            url: '/pages/integralmall/index'
          });
        }
        // 首页
        else if (app.globalData.type == 'in') {
          wx.switchTab({
            url: app.globalData.goHomeUrl
          });
        }
        // 停车
        else if (app.globalData.type == 'pc') {
          // 停车
          wx.switchTab({
            url: '/index/passCar'
          });
        }
        // 我的券包
        else if (app.globalData.type == 'mc') {
          // 我的券包
          wx.redirectTo({
            url: '/pages/couponorder/index/index'
          });
        }
        // 我的订单
        else if (app.globalData.type == 'mo') {
          // 我的订单
          wx.redirectTo({
            url: '/pages/order/index/index?id=all'
          });
        }
        // 我的卡包
        else if (app.globalData.type == 'ca') {
          // 我的卡包
          wx.redirectTo({
            url: '/pages/cardorder/index/index'
          });
        }
        // 特享礼遇
        else if (app.globalData.type == 'sc') {
          // 特享礼遇
          wx.redirectTo({
            url: '/pages/specialcourtesy/specialcourtesy'
          });
        }
        // 限时秒杀
        else if (app.globalData.type == 'rb') {
          // 限时秒杀
          wx.redirectTo({
            url: '/pages/rushToBuy/index'
          })
        }
        // 砍价专场
        else if (app.globalData.type == 'bl') {
          // 砍价专场
          wx.redirectTo({
            url: '/pages/bargain/bargain'
          })
        }
        // 我的砍价
        else if (app.globalData.type == 'mb') {
          // 我的砍价
          wx.redirectTo({
            url: '/pages/bargain/bargain?from=myhtml'
          })
        }
        // 拼团专场
        else if (app.globalData.type == 'sl') {
          // 拼团专场
          wx.redirectTo({
            url: '/pages/spellGroup/spellGroup'
          })
        }
        // 我的拼团
        else if (app.globalData.type == 'ms') {
          // 我的拼团
          wx.redirectTo({
            url: '/pages/spellGroup/spellGroup?from=myhtml'
          })
        }
        // 消费卡
        else if (app.globalData.type == 'dc') {
          // 消费卡
          wx.redirectTo({
            url: '/pages/discountCardList/discountCardList'
          })
        }
        // 门店
        else if (app.globalData.type == 'ml') {
          wx.switchTab({
            url: '/index/searchbar'
          })
        }
        // 兑换
        else if (app.globalData.type == 'ec') {
          wx.redirectTo({
            url: '/pages3/exchange/exchange'
          })
        }
        // 通过分享进入的门店详情
        else if (options && options.id && options.frommd == 'md') {
          wx.redirectTo({
            url: `/pages/index/searchbar/detail/index?id=${options.id}`
          });
        } else {
          // 主页
          wx.switchTab({
            url: app.globalData.goHomeUrl
          })
        }
      })
      .catch(err => {
        console.log(err)
        if (err.code == 11004) {
          // 用户昵称未授权
          if (options && options.orderGroupId) {
            wx.redirectTo({
              url: `/pages/getuserinfo/index?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) {
            wx.redirectTo({
              url: `/pages/getuserinfo/index?couponChannelId=${options.couponChannelId}`
            })
          } else if (options && options.couponChannelId && !options.cuserId && options.spellGroup) {
            wx.redirectTo({
              url: `/pages/getuserinfo/index?couponChannelId=${options.couponChannelId}&spellGroup=${options.spellGroup}`
            })
          } else if (options && options.couponChannelId && options.cuserId) {
            /**
             * 转赠判断
             */
            wx.redirectTo({
              url: `/pages/getuserinfo/index?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) {
            wx.redirectTo({
              url: `/pages/getuserinfo/index?orderId=${options.orderId}&from='${"discount"}`
            })
          }
          // 通过分享进入的门店详情
          else if (options && options.id && options.frommd == 'md') {
            wx.redirectTo({
              url: `/pages/getuserinfo/index?id=${options.id}&frommd=md`
            });
          } // 门店详情
          else if (options && options.id && app.globalData.type == 'md') {
            wx.redirectTo({
              url: `/pages/index/searchbar/detail/index?id=${options.id}`
            });
          }
          // 答题问卷 
          else if (options && options.id && app.globalData.type == 'wj') {
            wx.redirectTo({
              url: `/pages/questionnaire/questionnaire?id=${options.id}`,
            })
          }
          // 报名活动页面
          else if (options && options.id && app.globalData.type == 'ra') {
            wx.redirectTo({
              url: `/pages/radetail/index?id=${options.id}`,
            })
          } else if (app.globalData.type == 'uc') {
            wx.switchTab({
              url: '/index/user'
            });
          }
          // 积分
          else if (app.globalData.type == 'jf') {
            wx.redirectTo({
              url: '/pages/integralmall/index'
            });
          }
          // 专题
          else if (options && options.id && app.globalData.type == 'td') {
            wx.redirectTo({
              url: `/pages/topicDetail/index?id=${options.id}`
            });
          }
          // 首页
          else if (app.globalData.type == 'in') {
            wx.switchTab({
              url: app.globalData.goHomeUrl
            });
          }
          // 停车
          else if (app.globalData.type == 'pc') {
            // 停车
            wx.switchTab({
              url: '/index/passCar'
            });
          }
          // 我的券包
          else if (app.globalData.type == 'mc') {
            // 我的券包
            wx.redirectTo({
              url: '/pages/couponorder/index/index'
            });
          }
          // 我的订单
          else if (app.globalData.type == 'mo') {
            // 我的订单
            wx.redirectTo({
              url: '/pages/order/index/index?id=all'
            });
          }
          // 我的卡包
          else if (app.globalData.type == 'ca') {
            // 我的卡包
            wx.redirectTo({
              url: '/pages/cardorder/index/index'
            });
          }
          // 特享礼遇
          else if (app.globalData.type == 'sc') {
            // 特享礼遇
            wx.redirectTo({
              url: '/pages/specialcourtesy/specialcourtesy'
            });
          }
          // 限时秒杀
          else if (app.globalData.type == 'rb') {
            // 限时秒杀
            wx.redirectTo({
              url: '/pages/rushToBuy/index'
            })
          }
          // 砍价专场
          else if (app.globalData.type == 'bl') {
            // 砍价专场
            wx.redirectTo({
              url: '/pages/bargain/bargain'
            })
          }
          // 我的砍价
          else if (app.globalData.type == 'mb') {
            // 我的砍价
            wx.redirectTo({
              url: '/pages/bargain/bargain?from=myhtml'
            })
          }
          // 拼团专场
          else if (app.globalData.type == 'sl') {
            // 拼团专场
            wx.redirectTo({
              url: '/pages/spellGroup/spellGroup'
            })
          }
          // 我的拼团
          else if (app.globalData.type == 'ms') {
            wx.redirectTo({
              url: '/pages/spellGroup/spellGroup?from=myhtml'
            })
          }
          // 消费卡
          else if (app.globalData.type == 'dc') {
            // 消费卡
            wx.redirectTo({
              url: '/pages/discountCardList/discountCardList'
            })
          }
          // 兑换
          else if (app.globalData.type == 'ec') {
            wx.redirectTo({
              url: '/pages3/exchange/exchange'
            })
          }
          // 门店
          else if (app.globalData.type == 'ml') {
            wx.switchTab({
              url: '/index/searchbar'
            })
          } else {
            wx.switchTab({
              url: app.globalData.goHomeUrl,
            })
          }
        }
      })
  },

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