const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
const request = require('../../utils/request.js');
Page({
  data: {
    // 轮播
    backgrounds: [{
        "pic": "/images/banner1.png"
      },
      {
        "pic": "/images/banner2.png"
      },
      {
        "pic": "/images/banner3.png"
      },
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    number: {},
    msgList: []
  },
  goCollect() {
    wx.navigateTo({
      url: '/pages/userBook/userBook',
    })
  },
  goChildren() {
    if (!wx.getStorageSync('openId')) {
      wx.login({
        success: res => {
          console.log("用户的code:" + res.code);
          wx.request({
            url: app.globalData.publicUrl + '/wx/getAppletOpenId',
            method: "GET",
            data: {
              cid: 'applet',
              code: res.code
            },
            success: res => {
              wx.setStorageSync('openId', res.data.data.openid)
              this.isBInd();
            }
          });
        }
      });
    } else {
      this.isBInd();
    }
  },
  // 判断子女是否绑定老人
  isBInd() {
    let id = wx.getStorageSync('openId');
    request.request('/wxinfo/visitIsExist', {
      openId: id
    }, 'GET').then((res) => {
      console.log(res)
      if (res.data.code == 1) {
        wx.navigateTo({
          url: '/pages/visitFind/visitFind',
        })
      } else if (res.data.code == 0) {
        wx.navigateTo({
          url: '/pages/bindOld/bindOld',
        })
      } else {
        wx.showModal({
          showCancel: false,
          content: res.data.message,
        })
      }
    })
  },
  goTongzhi() {
    tools.rzStatus().then((res) => {
      if (res.data.code == 0) {
        if (res.data.data) {
          wx.navigateTo({
            url: '/pages/notice/notice',
          })
        }
      } else {
        wx.clearStorageSync();
        wx.showModal({
          showCancel: false,
          content: '当前您未登录,请登录',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      }
    })
  },
  goPaiming() {
    tools.rzStatus().then((res) => {
      if (res.data.code == 0) {
        if (res.data.data) {
          tools.finishInfo().then((res) => {
            console.log(res.data.data.perfect, '我完善信息了吗 1代表完后 0代表未完成')
            console.log(res.data.data.comparison, '我人脸识别过了吗 1代表完后 0代表未完成')
            if (res.data.data.perfect == '0') {
              wx.redirectTo({
                url: '/pages/finishInfo/finishInfo',
              })
              return;
            }
            if (res.data.data.perfect == '1' && res.data.data.comparison == '0') {
              wx.redirectTo({
                url: '/pages/faceRecognition/faceRecognition',
              })
              return;
            }
            wx.navigateTo({
              url: '/pages/paiming/paiming',
            })
          })
        }
      } else {
        wx.clearStorageSync();
        wx.showModal({
          showCancel: false,
          content: '当前您未登录,请登录',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      }
    })
  },
  goSaoMa() {
    tools.rzStatus().then((res) => {
      if (res.data.code == 0) {
        if (res.data.data) {
          tools.finishInfo().then((res) => {
            console.log(res.data.data.perfect, '我完善信息了吗 1代表完后 0代表未完成')
            console.log(res.data.data.comparison, '我人脸识别过了吗 1代表完后 0代表未完成')
            if (res.data.data.perfect == '0') {
              wx.redirectTo({
                url: '/pages/finishInfo/finishInfo',
              })
              return;
            }
            if (res.data.data.perfect == '1' && res.data.data.comparison == '0') {
              wx.redirectTo({
                url: '/pages/faceRecognition/faceRecognition',
              })
              return;
            }
            wx.scanCode({
              onlyFromCamera: true,
              success: (res) => {
                let length = res.result.substring(res.result.lastIndexOf('/') + 1, res.result.length)
                wx.navigateTo({
                  url: '/pages/love/love?id=' + length,
                })
              },
              fail: () => {
                wx.showToast({
                  title: '扫码失败',
                  icon: 'none',
                  duration: 2000,
                })
              }
            })
          })
        }
      } else {
        wx.clearStorageSync();
        wx.showModal({
          showCancel: false,
          content: '当前您未登录,请登录',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      }
    })
  },
  depts() {
    request.request('/applet/homeStatistics', {
      appletsId: wx.getStorageSync('openId')
    }, 'GET').then((res) => {
      console.log(res)
      if (res.data.code == 0) {
        this.setData({
          number: res.data.data
        })
      }
    })
  },
  welcomeMessage() {
    this.setData({
      msgList: []
    })
    tools.rzStatus().then((res) => {
      console.log(res)
      if (res.data.code == 0) {
        if (res.data.data) {
          request.request('/appletNotice/listForAppletsFirst', {
            appletsId: wx.getStorageSync('openId')
          }, 'GET').then((res) => {
            console.log(res)
            if (res.data.code == 0) {
              if (res.data.data.length == 0) {
                this.data.msgList.push({
                  title: '暂无通知'
                });
                this.setData({
                  msgList: this.data.msgList
                })
              } else {
                let result = res.data.data.title;
                this.data.msgList.push({
                  title: result
                });
                this.setData({
                  msgList: this.data.msgList
                })
              }
            }
          })
        }
      } else {
        request.request('/welcomeMessage/list', {}, 'GET').then((res) => {
          if (res.data.code == 0) {
            if (res.data.data.length != 0) {
              let result = res.data.data[0].title
              this.data.msgList.push({
                title: result
              });
              this.data.msgList.push({
                title: result
              });
              this.setData({
                msgList: this.data.msgList
              })
            }
          }
        })
      }
    })
  },
  onShow() {
    this.depts();
    this.welcomeMessage();
  },
  onLoad: function () {
    const apps = getApp()
    apps.getAuthKey().then(() => {
      this.depts();
    })
  }
})