// pages/agent/index.js
import {
  saveAgent
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    contact: '',
    year: '',
    sex: ["请选择", "男", "女"],
    sex_index: 0,
    imgServerUrl: imgServerUrl,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNodePos();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户user_id和member_id,判断是否处于登录态
    var userId = wx.getStorageSync("user_id");
    var userToken = wx.getStorageSync("user_token");
    var memberId = wx.getStorageSync("member_id");
    var isLogin = false;
    if (userId && memberId) {
      isLogin = true;
      app.globalData.userId = userId;
      app.globalData.userToken = userToken;
      app.globalData.memberId = memberId;
    }
    this.setData({
      isLogin: isLogin
    })
  },
  imageLoad() {
    var that = this
    var query = wx.createSelectorQuery()
    query.select('.agent1').boundingClientRect()
    query.select('.agent2').boundingClientRect()
    query.select('.agent3').boundingClientRect()
    query.select('.agent4').boundingClientRect()
    query.exec(function (res) {
      if (res[0]) {
        that.setData({
          swiperH: res[0].height,
          swiperH2: res[1].height,
          swiperH3: res[2].height,
          swiperH4: res[3].height
        })
      }
    })
  },
  //获取位置
  getNodePos() {
    var query = wx.createSelectorQuery()
    query.select('#agentJoin').boundingClientRect()
    var that = this;
    query.exec(function (res) {
      that.setData({
        basePos: res[0].top + 500,
      })
    })
  },

  baoming(e) {
    let top = e.currentTarget.dataset.top
    wx.pageScrollTo({
      scrollTop: top,
      duration: 0
    })
  },
  changeName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  changeContact: function (e) {
    this.setData({
      contact: e.detail.value
    })
  },
  //改变性别
  bindSexChange(e) {
    let {
      value
    } = e.detail
    this.setData({
      sex_index: value
    })
  },
  changeYear: function (e) {
    this.setData({
      year: e.detail.value
    })
  },
  submit() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    var name = this.data.name;
    var contact = this.data.contact;
    var sex = this.data.sex_index;
    var year = this.data.year;
    if (!name) {
      showToast("请填写姓名")
      return;
    }
    if (contact == "" || contact.length != 11) {
      showToast("请输入11位手机号")
      return;
    }
    if (sex == 0) {
      showToast("请选择性别")
      return;
    }
    if (!year) {
      showToast("请填写年龄")
      return;
    }
    var regNum = new RegExp('[0-9]', 'g');
    var rsNum = regNum.exec(year);
    if (!rsNum) {
      showToast("年龄请输入正确的数字")
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定提交吗?',
      success: function (res) {
        if (res.confirm) {
          let paramsObj = {
            user_id: app.globalData.userId,
            user_token: app.globalData.userToken,
            member_id: app.globalData.memberId,
            name: name,
            phone: contact,
            gender: sex,
            age: year
          }
          Object.assign(paramsObj);
          saveAgent(paramsObj).then(data => {
            showToast('提交成功', 'success')
            setTimeout(function () {
              wx.navigateBack();
            }, 1000)
          })
        } else if (res.cancel) {

        }
      }
    })
  }
})