var app = getApp()
var that=null
var sopenFrame=null
var areas = null;
Page( {
  data: {
    loadingHidden:false,
    IFDefault:true,
    loginurl:app.globalData.apiurl+"/gl.aspx?g=0&tt=0000000",
    userid:"",
    userpwd:"",
    userpwd0:"",
    hkdjye:0,
    hkjdye:0,
    hkksye:0,
    productcount:0,
    productcount0:0,
    fhordercount0:0,
    fhordercount1:0,
    fhordercount2:0,
    kdordercount0:0,
    kdordercount1:0,
    kdordercount2:0,
    ztordercount0:0,
    ztordercount1:0,
    ztordercount2:0,
  },
  onLoad: function() {
      that = this
      // 检查用户权限
      var userInfodata = wx.getStorageSync("userInfo")
      if(userInfodata){
        var userInfo = JSON.parse(userInfodata)
        console.log("用户信息:", userInfo);
        console.log("sjuLevel0:", userInfo.sjuLevel0);
        // 检查是否是健康驿站用户
        if(userInfo.sjuLevel0 != 2){
          wx.showToast({
            title: "您不是健康驿站用户，无法查看此页面",
            icon: "none",
            duration: 3000
          })
          // 延迟返回上一页
          setTimeout(function(){
            wx.navigateBack()
          }, 2000)
          return
        }
      }
      that.rdata()
    },rdata: function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      if(!userId || !LoginId || !userInfodata){
        wx.navigateTo({  
          url: "../login/index"
        })
        return
      }
      
      var userInfo = JSON.parse(userInfodata)
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getmybusinessdata",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           // 添加用户类型参数，确保获取正确的数据
           userType: "business0"
        },
        method:"GET",
        dataType:"json", 
        success (res) { 
          console.log("mybusiness0 API返回数据:", res.data);
          if(res.data.status==1){
            // 检查返回的数据是否包含订单统计
            if(res.data.fhordercount0 === undefined){
              console.warn("API返回的数据缺少订单统计字段");
              wx.showToast({
                title: "数据获取不完整，请稍后重试",
                icon: "none",
                duration: 3000
              })
            }
            that.setData({
              userid:res.data.zh,
              userpwd:res.data.mm,
              hkdjye:res.data.userInfo ? res.data.userInfo.hkdjye : 0,
              hkjdye:res.data.userInfo ? res.data.userInfo.hkjdye : 0,
              hkksye:res.data.userInfo ? res.data.userInfo.hkksye : 0,
              productcount:res.data.productcount || 0,
              productcount0:res.data.productcount0 || 0,
              fhordercount0:res.data.fhordercount0 || 0,
              fhordercount1:res.data.fhordercount1 || 0,
              fhordercount2:res.data.fhordercount2 || 0,
              kdordercount0:res.data.kdordercount0 || 0,
              kdordercount1:res.data.kdordercount1 || 0,
              kdordercount2:res.data.kdordercount2 || 0,
              ztordercount0:res.data.ztordercount0 || 0,
              ztordercount1:res.data.ztordercount1 || 0,
              ztordercount2:res.data.ztordercount2 || 0,
              loadingHidden:true
             })
          } else {
            console.log("mybusiness0 API返回状态错误:", res.data);
            wx.showToast({
              title: res.data.msg || "数据获取失败",
              icon: "none",
              duration: 3000
            })
          }
        },
        fail (err) {
          console.error("mybusiness0 API请求失败:", err);
          wx.showToast({
            title: "网络请求失败，请检查网络连接",
            icon: "none",
            duration: 3000
          })
        }
        })
            
    },changeisDefault:function(e) {
      var ifdefault = e.detail.value
      that.setData({
        IFDefault:ifdefault
       })
    },oncopy0:function(e) {
      var font = e.currentTarget.dataset.id
      wx.setClipboardData({
        data: font,
        success: function (res) { 
          wx.showToast({
            title: "复制成功"
          })
        }
      })
    },onlog:function(e) {
      var id = e.currentTarget.dataset.id
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      var ifdl=true;
      if(!userId||!LoginId){
            ifdl=false;
            wx.navigateTo({  
              url: "../login/index"
             })
      }
      if(ifdl){
          var t = e.currentTarget.dataset.id
              wx.navigateTo({  
                  url: "../mybalance/index?t=" + t
              }) 
        }
    },onproduct:function(e) {
      var id = e.currentTarget.dataset.id
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      var ifdl=true;
      if(!userId||!LoginId){
            ifdl=false;
            wx.navigateTo({  
              url: "../login/index"
             })
      }
      if(ifdl){
          var t = e.currentTarget.dataset.id
              wx.navigateTo({  
                  url: "../myproducts/index?t=" + t
              }) 
        }
    },onorders:function(e) {
      var id = e.currentTarget.dataset.id
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      var ifdl=true;
      if(!userId||!LoginId){
            ifdl=false;
            wx.navigateTo({  
              url: "../login/index"
             })
      }
      if(ifdl){
          var index = e.currentTarget.dataset.index
          var id = e.currentTarget.dataset.id
              wx.navigateTo({  
                  url: "../myorder0/index?t=" + index + "&nav=" + id
              }) 
        }
    }
})
