// pages/webview/index.js
const api = require('../../config/api.js');
const util = require('../../utils/util.js');
import socketApi from '../../socket'

const handleScenes = ['t-9EtLj36', 'tSrGwEa0n', 'wrsPDAyy9']


const URL = 'https://www.4dkankan.com/mallscene.html?m='
Page({

  ...socketApi,
  /**
   * 页面的初始数据
   */
  data: {
    handleScenes,
    sendShare: false,
    loadUrl: false,
    goodsCount: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    rnd:'',
    temp:'',
    live_base_url: 'rtmp://120.24.85.77:1935/hls',
    peopleCount: 5,
    surplus: 5
    // url: 'https://www.4dkankan.com/showProPC.html?m=vroMUXdx&origin=4Dplaza&sid=1046051&room_id=1596785144930&user_id=44'
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    getApp().orderBuyCbs(() => {
      try {
        this.socketSendMessage('clientSyncAction', {
          type: 'referServerData'
        })
      } catch(e) {
        console.log('发送失败', e)
        let url = this.data.webviewUrl
        this.setData({webviewUrl: ''})
        setTimeout(() => {
          this.setData({webviewUrl: url})
        }, 100)
      }
    })

    getApp().checkNetStatu();

    
    // setTimeout(() => this.login(), 3000)
    this.setData({
      join: false,
      imgServer: util.imgServer,
      showGuide: false,
      showCommodity: false,
      showInvitation: false,
      showEnd: false,
      showAlert: false,
      sendShare: false,
      showPacked: false,
      showIsEnd: false,
      showCoupon: false,
      showContact: false,
      showCommodityCtrl: false,
      recordStatus: 0,
      commoditys: [],
      coupons: [],
      userAuth: false,
      count: 0,
      goodsList: [],
      page: 1,
      size: 1000
    })
    
    // console.log(options)
    // options.id = '1046098'
    this.options = options
    if (this.options.roomId) {
      this.data.join = true
    }

    if (this.data.join) {
      this.role = 'customer'
    } else {
      this.role = 'leader'
    }

    if (options.many !== void 0) {
      this.data.many = options.many
    }

    socketApi.onShow.call(this)
    this.urlPj = '&brandId=' + this.options.id
  },

  async init() {
    let options = this.options

    let {url: base, obj: m } = await new Promise(r => this.getBrandDetail(options.id, (url, obj)  => r({url, obj})))

    this.setData({sceneNumber: m})
    this.setData({isGreve: !!~this.data.handleScenes.indexOf(this.data.sceneNumber)})
    this.getBrand(options.id, m)
    this.mcode = m
    let socketOptions = await this.socketStart({sceneId: this.mcode, roomId: options.roomId})
    // options.join = true
    let url = this.getUrl(base, socketOptions, this.data.join) + this.urlPj

    this.base = base

    this.setData({urlTemp: url.split('').join(' ')})
    this.setData({ url, loadUrl: true, socketOptions, reload: true, hideWebView: false })
    this.recorderManager = wx.getRecorderManager()

    this.brandId = options.id
    this.joinUrl()
    // this.mic()
  },

  hideInvitation() {
    this.setData({ showInvitation: false })
    this.exit()
  },

  hideGuide() {
    this.setData({ showGuide: false })
  },

  hideEnd() {
    this.setData({ showEnd: false })
  },

  hideAlert() {
    this.setData({ showAlert: false })
  },

  // exit() {
  //   this.stopCall()
  //   this.socketStop && this.socketStop()
  //   wx.navigateBack()
  // },
  
  hidePacked() {
    this.setData({ showPacked: false })
  },
  
  hideIsEnd() {
    this.setData({ showIsEnd: false })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    getApp().updateCardCount()

    
    
    // this.options.join = true
    // this.getCartList()
    
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');

    if (!(userInfo && userInfo.userId && token)) {
      return;
      // return wx.navigateTo({
      //   url: '/pages/auth/btnAuth/btnAuth',
      // })
      if (this.options.join) {
        getApp().setLoginProps(false)
      } else {
        this.socketStop && this.socketStop()
        this.init()
      }
    } else if (this.token !== wx.getStorageSync('token')) {
      if (this.data.join) {
        let res = await util.request(api.UserInfo)
        if (res.errno === 401) {
          return getApp().setLoginProps(false)
        }
      }
      this.token = wx.getStorageSync('token')
      if (this.socketStop) {
        this.socketStop()
        this.setData({
          hideWebView: true,
          webviewUrl: null
        })
        setTimeout(() => {
          this.setData({ hideWebView: false })
          this.init()
        }, 500)
      } else {
        this.init()
      }
    } else if (!this.socketSendMessage){
      
    } else {
      socketApi.onShow.call(this)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    socketApi.onHide.call(this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    getApp().onPullDownRefresh()
  },

  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})