// pages/membersinfo/index.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    birthdate: '',
    flag: 1,
    name:'',
    phone:'',
    sex:'',
    address:'',
    copenid:'',
    items: [
      { name: 1, value: '男', checked: false },
      { name: 2, value: '女', checked: false },
    ],
    subappid:'',
    phoneFlag:false,
  },
  bindDateChange: function (e) {
    this.setData({
      birthdate: e.detail.value,
      flag: 2
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, 2222, util)
    let _this=this;
    this.setData({
      copenid: options.copenid,
      subappid: options.subappid
    })
    let params={
      userId: wx.getStorageSync('bUserId'),
      openid: options.copenid,
      appId: options.subappid
    }
    Http.post({
      url: config.api.findUserInfo,
      data: params
    })
      .then(res => {
        let data=res.data;
        if (res.data){
          let sexOption = _this.data.items
          sexOption.map((item,index)=>{
            if (item.name == data.sex) {
              item.checked=true;
            }
          })
          if (data.phone){
            this.setData({
              phone: data.phone,
              phoneFlag:true
            })
          }
          this.setData({
            birthdate: data.birthdate? util.formatDate02(data.birthdate):"请输入",
            name: data.name,
            sex: data.sex,
            address: data.address,
            items:sexOption,
            flag:2
          })
        }
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  formSubmit(e){
    if (e.detail.value.name==''){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return
    }
    if (this.data.phone == '' && e.detail.value.phone == '') {
      wx.showToast({
        title: '请输入会员手机号',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return
    }
    if (e.detail.value.address=='') {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return
    }
    if (!(/^1[3456789]\d{9}$/.test(e.detail.value.phone))){
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return
    }
    let params={
      appId: this.data.subappid,
      openid: this.data.copenid,
      name:e.detail.value.name,
      sex:e.detail.value.sex,
      address:e.detail.value.address,
      userId: wx.getStorageSync("bUserId") ? wx.getStorageSync("bUserId") : app.globalData.bUserId,
      phone: e.detail.value.phone,
    }
    if (this.data.birthdate != '' && this.data.birthdate != "请输入") {
      params.birthdate = this.data.birthdate + ' 00:00:00'
    } else {
      params.birthdate = ''
    }
    if (params.birthdate == '') {
      wx.showToast({
        title: '请输入生日',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return
    }
    wx.showLoading({
      title: '正在保存!',
    })
    Http.post({
      url: config.api.cMemberAdd,
      data: params
    })
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          title: '录入成功，2秒后跳转首页',
          icon: 'none',
          duration: 2000,
          success:function(){
            wx.reLaunch({
              url: '../main/main',
            })
          }
        });
      })
      .catch(err => {
        wx.hideLoading()
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  }
})