// index.js
// 获取应用实例
const app = getApp()
var utils = require('../../utils/util.js')
Page({
  data: {
    index: 1,
    noticeList: [], //开班通告
    answerList: [], //报考问答
    userIcon: '../../images/icons/ic_user_default.png',
    nickName: '点击授权',
    // show: false,
    phone: '', //要绑定的手机号
    categoryId: '87a51bb1-451b-4b49-903b-4d847ba9c730',
  },
  onLoad() {
    this.getUserInfo()
    this.getNoticeList()
    this.getAnswerList()
    this.doLogin()
  },
  //获取最新公告
  getNoticeList() {
    var _self = this
    app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
        categoryId: _self.data.categoryId,
        page: '1',
        rows: '6'
      }, {},
      function (code, data) {
        if (code == 200) {
          for (var i = 0; i < data.rows.length; i++) {
            var item = data.rows[i]
            if (item.coverPhotos.length > 0) {
              var photos = item.coverPhotos.split(',')
              data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
            } else {
              data.rows[i].showPhoto = '../../images/icons/notice_default.png'
            }
          }
          _self.setData({
            noticeList: data.rows
          })
        }
      },
      function (code, err) {
        console.log(err)
      })
  },
  //报考问答
  getAnswerList() {
    var _self = this
    app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
        categoryId: '494bf3cc-cab6-4710-a0b2-d84f4591c992',
        page: '1',
        rows: '6'
      }, {},
      function (code, data) {
        if (code == 200) {
          for (var i = 0; i < data.rows.length; i++) {
            var item = data.rows[i]
            if (item.coverPhotos.length > 0) {
              var photos = item.coverPhotos.split(',')
              data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
            } else {
              data.rows[i].showPhoto = '../../images/icons/answer_default.jpeg'
            }
          }
          _self.setData({
            answerList: data.rows
          })
        }
      },
      function (code, err) {
        console.log(err)
      })
  },
  //获取个人信息
  getUserInfo() {
    let _self = this
    try {
      var name = wx.getStorageSync('nickName')
      var icon = wx.getStorageSync('userIcon')
      if (name) {
        _self.setData({
          nickName: name
        })
      }
      if (icon) {
        _self.setData({
          userIcon: icon
        })
        // _self.checkBindPhone()
      } else {
        wx.showModal({
          title: '提示',
          content: '小程序需要您微信头像以及昵称用于展示,请授权.',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              _self.getUserProfile(3)
            }
          }
        })
      }
    } catch (e) {

    }
  },
  showMineInfo(e) {
    try {
      var icon = wx.getStorageSync('userIcon')
      if (icon) {
        wx.navigateTo({
          url: './mineinfo',
        })
      } else {
        this.getUserProfile(e)
      }
    } catch (err) {

    }
  },
  //弹框关闭
  onClose(e) {
    this.setData({
      show: false
    })
  },
  //进行手机号绑定 
  doBindPhone(e) {
    var _self = this
    if (_self.checkPhonelegal()) {
      app.globalData.bindPhone = _self.data.phone
      wx.setStorage({
        data: _self.data.phone,
        key: 'bindPhone',
      })
      _self.setData({
        show: false
      })
    }
  },
  checkPhonelegal() {
    var _self = this
    if (!utils.isMobile(_self.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    } else {
      return true
    }
  },
  //绑定手机号输入监听
  bindPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //显示更多数据
  showMoreList() {
    var self = this
    if (this.data.index == 1 || this.data.index == 3) {
      wx.navigateTo({
        url: '../notices/noticeslist?index=' + self.data.index,
      })
    } else {
      wx.navigateTo({
        url: '../answer/answerlist',
      })
    }
  },
  //新闻切换
  tabSelect(e) {
    this.setData({
      index: e.currentTarget.dataset.id
    })
    if (e.currentTarget.dataset.id != '2') {
      if (e.currentTarget.dataset.id == '1') {
        this.setData({
          categoryId: '87a51bb1-451b-4b49-903b-4d847ba9c730'
        })
      } else if (e.currentTarget.dataset.id == '3') {
        this.setData({
          categoryId: '92737534-c2ef-487c-b763-4d1fb597388f'
        })
      }
      this.getNoticeList()
    }
  },
  //页面跳转
  choosePage(e) {
    var pagePath = ''
    switch (e.currentTarget.dataset.page) {
      case '1': //培训机构查询
        pagePath = '../institutions/institutionslist'
        break
      case '2': //成绩查询
        pagePath = '../results/resultslist'
        break
      case '3': //准考证查询
        pagePath = '../card/cardlist'
        break
      case '4': //考试网点查询
        pagePath = '../branches/brancheslist'
        break
      case '5': //培训报名
        pagePath = '../sign/sign'
        break
      case '6': //报名情况
        pagePath = '../signStatus/signStatus'
        break
    }
    wx.navigateTo({
      url: pagePath,
    })
    // if (e.currentTarget.dataset.page == '2' || e.currentTarget.dataset.page == '3') {
    //   if (app.globalData.bindPhone != '' && app.globalData.bindPhone.length > 0) {
       
    //   } else {
    //     // this.checkBindPhone()
    //   }
    // } else {
    //   wx.navigateTo({
    //     url: pagePath,
    //   })
    // }
  }, // 获取个人信息
  getUserProfile(e) {
    let _self = this
    wx.getUserProfile({
      desc: '获取微信头像以及昵称用于展示',
      success: (res) => {
        wx.setStorage({
          data: res.userInfo.nickName,
          key: 'nickName',
        })
        wx.setStorage({
          data: res.userInfo.avatarUrl,
          key: 'userIcon',
        })
        _self.setData({
          nickName: res.userInfo.nickName,
          userIcon: res.userInfo.avatarUrl
        })
        // _self.checkBindPhone()
      },
      fail(err) {
        //判断是否绑定手机号
        wx.showToast({
          title: '获取个人信息失败',
          icon: 'error',
          success() {
            _self.checkBindPhone()
          }
        })
      }
    })
  },
  //判断是否绑定手机号
  // checkBindPhone() {
  //   try {
  //     var _self = this
  //     var bindPhone = wx.getStorageSync('bindPhone')
  //     if (bindPhone == '' || bindPhone.length <= 0) {
  //       _self.setData({
  //         show: true
  //       })
  //     }
  //   } catch (error) {

  //   }
  // },
  showNoticeDetail(e) {
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../notices/noticedetail?id=' + item.contentId,
    })
  },

  showAnswerDetail(e) {
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../answer/answerdetail?id=' + item.contentId,
    })
  },
  // 登录
  doLogin: function () {
    wx.login({
      success: function (res) {
        app.restAjax.post(app.restAjax.path(app.apis.login, [app.baseUrls.testUrl]), {
          jsCode: res.code
        }, {},
        function (code, data) {
          var token = data.data
          wx.setStorage({
            key: 'token',
            data: token
          })
        },
        function (code, err) {
          console.log(err)
        })
      }
    })
  }
})