// pages/mine/mine.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isopen: 1, // 账户余额是否显示
    isMyPro: false,// 是否是小程序拥有者登录判断
    iosDialog: false,
    iosDialogTobe: false,
    isLoading: true, // 成为代理loading效果
    isLogin: app.globalData.isLogin,
    // user:app.globalData.user,
    serviceInfo: {},
    map: { num10: "0", num20: "0", num25: "0", num30: "0", num40: "0", num999: "0", user999: "0", user40: "0", user48: "0", user50: "0", billStatus10Salary: "0", billStatus30Salary: "0" },
    motto: "Hello World",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
    canIUseGetUserProfile: false,
    // 如需尝试获取用户信息可改为false
    underReviewNum: -1,
    underReviewList: [],
    searchParam: {},
    recordBillType: "",
    configInfo: {},
    balance: '0.00'
    // agencyStatus: 0, // 是否是代理判断
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    setTimeout(() => {
      console.log(app.globalData.loginUserInfo);
      this.setData({
        isLogin: app.globalData.isLogin,
        hasUserInfo: app.globalData.hasUserInfo,
        // agencyStatus: app.globalData.loginUserInfo.agencyStatus,
        userInfo: app.globalData.loginUserInfo,
        serviceInfo: app.globalData.serviceInfo,
        corpUserFlag: app.globalData.loginUserInfo.corpUserFlag,
      });
    }, 100);
    this.getProInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },
  openEyes: function () {
    this.setData({
      showMoney: false
    })
  },
  closeEyes: function () {
    this.setData({
      showMoney: true
    })
  },
  /**
  * 获取当前小程序信息
  */
  getProInfo () {
    let that = this
    wx.request({
      url: app.globalData.ip + '/yishoudan/weixin/config/getConfig', // 分类列表获取接口
      header: app.globalData.headers,
      method: "GET",
      success: function (res) {
        console.log(res);
        console.log(app.globalData.loginUserInfo);
        if (res.data.status == 200) {
          if (res.data.data.config.agencyId == app.globalData.loginUserInfo.agencyId) {
            that.setData({
              isMyPro: true
            })
          } else {
            that.setData({
              isMyPro: false
            })
          }
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {
    console.log("isshow");
    let that = this;
    wx.setStorageSync("comeFromPage", "me");
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
        isShow: true
      });
    }
    wx.setStorageSync('BILLFROM', 'mine')

    // wx.showTabBar({
    //   success(e) {},
    // });
    // if (!app.globalData.isLogin) {
    //   wx.setStorageSync('comeFromPage', "me")
    //   wx.redirectTo({
    // 		url: "/pages/login/index",
    // 	});
    // } else {
    // 	wx.switchTab({
    // 		url: "/pages/mine/index",
    // 	});
    // }
    // let serviceInfo =  wx.getStorageSync('ServiceInfo')
    setTimeout(() => {
      if (app.globalData.isLogin) {
        console.log(that.data.userInfo);
        that.setData({
          isLogin: app.globalData.isLogin,
          hasUserInfo: app.globalData.hasUserInfo,
          // agencyStatus: app.globalData.loginUserInfo.agencyStatus,
          userInfo: app.globalData.loginUserInfo,
          serviceInfo: app.globalData.serviceInfo,
          corpUserFlag: app.globalData.loginUserInfo.corpUserFlag,
        });
        that.getDataNum();
      } else {
        // 由于 userLogin 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        // app.userLoginCallback = (res) => {
        setTimeout(() => {
          console.log(that.data.userInfo);
          that.setData({
            isLogin: app.globalData.isLogin,
            hasUserInfo: app.globalData.hasUserInfo,
            userInfo: app.globalData.loginUserInfo,
            // agencyStatus: app.globalData.agencyStatus,
            serviceInfo: app.globalData.serviceInfo,
          });
        }, 100);
        // };
        that.getDataNum();
      }
      console.log(that.data.serviceInfo);
      // this.watch();
    }, 100);
    this.getConfigInfo();

  },
  watch () {
    // var obj = app.globalData;
    // this.setData({
    // 	isLogin: obj.isLogin,
    // });
  },
  onTabItemTap (item) {
    console.log(item.index);
    console.log(item.pagePath);
    console.log(item.text);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log(123);

    this.setData({
      iosDialog: false,
      iosDialogTobe: false,
    });
    wx.onAppRoute({
      success (item) {
        console.log(item);
      },
      fail: function (item) {
        console.log(item);
      },
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // var that = this;
    // console.log(that.data.isLogin)
    // if(!that.data.isLogin){
    //   wx.setStorageSync('comeFromPage', "me")
    //   wx.redirectTo({
    // 		url: "/pages/login/index",
    // 	});
    // }
  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage1: function () { },
  showbalance (e) {
    this.setData({
      isopen: e.mark.open,
    });
  },
  showdialog () {
    this.setData({
      iosDialog: true,
    });
  },
  showTobe () {
    this.setData({
      iosDialogTobe: true,
    });
  },
  closeDialog () {
    this.setData({
      iosDialog: false,
      iosDialogTobe: false,
    });
  },
  login () {
    // this.setData({
    //   isLogin:true
    // })

    app.globalData.isLogin = true;
    wx.reLaunch({
      url: "/pages/mine/index",
    });
  },
  /**
 * 获取商家配置信息
 *
 *
 */
  getConfigInfo () {
    let that = this;
    wx.request({
      url: app.globalData.ip + "/yishoudan/agency/config/get", // 分类列表获取接口
      header: app.globalData.headers,
      method: "GET",
      success: function (res) {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            configInfo: res.data.data.config,
          });
          wx.setStorageSync('configInfo', res.data.data.config)
          console.log(that.data.configInfo);
        }
      },
    });
  },
  makePhoneCall () {
    console.log(1);
    var that = this;
    wx.makePhoneCall({
      // number: that.data.jobDetail.assistantUserTel + ""
      phoneNumber: that.data.serviceInfo.workPhone || "0371-6611 3723",
    });
  },
  /**
   * 前往工单页面
   *
   *
   */
  goList () {
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: "/pages/login/index",
      });
    } else {
      wx.navigateTo({
        url: "../myBill/index?status=0",
      });
    }
  },
  /**
   * 获取各个数量
   *
   *
   */
  getDataNum () {
    let that = this;
    wx.request({
      url: app.globalData.ip + "/statistics/home",
      header: app.globalData.headers,
      success (res) {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            map: res.data.data,
          });
        }
      },
    });
  },
  /**
   * 二维码加载laoding隐藏
   *
   *
   */
  imageLoad () {
    this.setData({
      isLoading: false,
    });
  },
  toTodayData () {
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: "/pages/login/index",
      });
    } else {
      wx.navigateTo({
        url: "/pages/todayData/index",
      });
    }
  },
  modalMove () {
    return false;
  },
});
