// pages/home/home.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var isregistered 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        "adUrl": "../../img/home_banner1.png"
      },
      {
        "adUrl": "../../img/home_banner2.png"
      },
      {
        "adUrl": "../../img/home_banner.png"
      }
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    homedata:{},
    noticeContent:'这是公告',
    baseUrl: baseUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    baseutils.getComOpenid() //获取openid
    // BASEURL.comLog('home','hme')
    // baseutils.getAccessToken()//获取Token
  },

  getAddata:function(){
    var that = this
    var data = {  }
    var url = baseUrl + BASEURL.getUrl.getAd
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data.success){
        that.setData({
          homedata: res.data.data,
          noticeContent: res.data.data.announcement[0].noticeContent
        })
        // console.log('homedata', that.data.homedata)

        // if (res.data.data.advertisingImg.length > 0){
        //   that.setData({
        //     imgUrls: res.data.data.advertisingImg
        //   })

        //   console.log('imgUrls', that.data.imgUrls)
        // }
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },


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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    isregistered = wx.getStorageSync('isregistered')
    var wxnickName = wx.getStorageSync('wxnickName')
    console.log('wxnickName', wxnickName)
    if (!wxnickName){
      wx.showModal({
        title: '提示',
        content: '您当前尚未进行微信授权登录，是否立即前往?',
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine',
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    } else if (!isregistered){
      wx.showModal({
        title: '提示',
        content: '您当前尚未与企业关联绑定，是否立即去绑定?',
        success: function (res) {
          if (res.confirm) {
          wx.navigateTo({
            url: '../bindphone/bindphone',
          })
          } else if (res.cancel) {
            return
          }
        }
      })
    }else{
      baseutils.getPersonlEmp()
      baseutils.judgeisReg()
    }
    that.getAddata()   //获取home广告信息
  },
  newsItemClick:function(event){
    var that = this
    var cid = event.currentTarget.dataset.newsid
    var title = event.currentTarget.dataset.newstitle
    // cid = 128
    var url = baseUrl + '/integral/blog/open/post/' + cid
    console.log('newsItemClick', url)
    wx.navigateTo({
      url: '../WebView/WebView?getUrl=' + url + '&getTitle=' + title
    })
  },
  sendTemMsg:function(e){
    console.log('e1', e)
    var data = {
      keyword1: {
        value: '余额充值',
        color: "#4a4a4a"
      },
      keyword2: {
        value: '_花自飘零水自流',
        color: "#9b9b9b"
      },
      keyword3: {
        value: new Date().getDate(),
        color: "#9b9b9b"
      },
      keyword4: {
        value: "999",
        color: "#9b9b9b"
      },
      keyword5: {
        value: "18130506086",
        color: "red"
      },
      keyword6: {
        value: "4003182001201611291d140743105",
        color: "red"
      },
      keyword7: {
        value: "9999.00元",
        color: "red"
      },
      keyword8: {
        value: "阿里腾",
        color: "red"
      },
      keyword9: {
        value: "80201708281553340810",
        color: "red"
      },
      keyword10: {
        value: "微信支付",
        color: "red"
      }
    }
    let form_id = e.detail.formId;
    let template_id = 'ycnFda6tQoZlEvufrqQCR3QdvNgB5UpvR84O5FfUeU8' 

    var d = {
      token: wx.getStorageSync('access_token'),
      invoicesId: 61,
      touser: wx.getStorageSync('openid'),
      template_id: template_id,//这个是1、申请的模板消息id，  
      page: '/pages/home/home',
      formid: form_id,
      data:JSON.stringify(data),
      color: '#ccc',
      emphasis_keyword: 'keyword1.DATA'
    } 

    var url = baseUrl + BASEURL.getUrl.sendMessage
    netUtil.requestLoading(url, d, '正在加载数据...', 'POST', function (res) {

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})