const wxService = require('./wxService')
const app = getApp()
const wx2my = require('../wx2my');
const Behavior = require('../Behavior');

function currentLinkRouter(event, _this) {
  const ev = event.currentTarget.dataset;
  let curItemType = ev.item
  const userInfo = wx2my.getStorageSync('_baseUserInfo') && wx2my.getStorageSync('_baseUserInfo').data || null
 
  // link row
  if (curItemType.link) {
    let currentClickType = app.globalData.commonFunc.getLink(curItemType.link.type, curItemType)
    console.log('wwwwwwww', curItemType.link.type)
    console.log('wwwwwwuserInfoww',wx2my.getStorageSync('_baseUserInfo'),userInfo)
    console.log('currentClickType', currentClickType)

    // 触发父组件更新页面 外部小程序
    if (curItemType.link.type == 1) {
      // 触发父组件更新页面 外部小程序
      _this.setData({
        isAuthorization: false
      })
      return false
    } else if (!userInfo) {
      // userInfo 不存在 授权弹框
      _this.setData({
        isAuthorization: true
      })
      return false
    }

    if (curItemType.link.type == 2) {
      // 触发父组件更新页面
       console.log('2', currentClickType.url)
      wxService.nextTick(() => {
        console.log('2', currentClickType.url)
        wxService.router(`/subPackage/page/pages/subPage/subPage`).search({pageId: currentClickType.url})
        // _this.triggerEvent('updatePage', { pageId: currentClickType.url })
      })
    } else if (curItemType.link.type == 3) {
      // 预览
      wx.previewImage({
        current: `${app.globalData.imageUrl}/${currentClickType.imageUrl}`, // 当前显示图片的http链接
        urls: [`${app.globalData.imageUrl}/${currentClickType.link.url}`]  // 需要预览的图片http链接列表
      })
    } else if (curItemType.link.type == 4) {
      wxService.router(`${currentClickType}`)
    } else if (curItemType.link.type == 5) {
      if (currentClickType == 1) {
        wxService.openCard()
      } else if (currentClickType == 2) {
        wxService.openCard()
      }
    } else if (curItemType.link.type == 6) {
      wxService.router(`${currentClickType}`)
    } else if (curItemType.link.type == 7) {
      // 集点详情
      wxService.router(`${currentClickType}`)
    }
  } else if (curItemType.row) {
    let rowObj = typeof (curItemType.row) == "string" ? JSON.parse(curItemType.row) : curItemType.row
    let currentClickRow = app.globalData.commonFunc.getLinkRow(rowObj.type, rowObj)

    if (rowObj.type == 1) { }
    else if (rowObj.type == 2) { }
    else if (rowObj.type == 3) { }
    else if (rowObj.type == 4) { }
    else if (rowObj.type == 5) { }
    else if (rowObj.type == 6) { }
    else if (rowObj.type == 7) { }
    else if (rowObj.type == 8) {
      // 商品
      wxService.router(`/pages/productDetail/productDetail?id=${currentClickRow}`)
    }
    else if (rowObj.type == 9) {
      // 分类
      wxService.router(`/subPackage/page/pages/categoryPro/categoryPro?${currentClickRow}`)
    }
  }
}

module.exports = {
  currentLinkRouter: currentLinkRouter
}