// pages/add_parent/add_parent.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.baseImgUrl,
    isAddParents:false,
    role:'',
    isRole:false,
    hiddenModel: false,
    roleList: [],
    parentList:[
      {
        str:'爸爸',
        status:''
      },
      {
        str: '妈妈',
        status: ''
      },
      {
        str: '爷爷',
        status: ''
      },
      {
        str: '奶奶',
        status: ''
      },
      {
        str: '外公',
        status: ''
      },
      {
        str: '外婆',
        status: ''
      },
      {
        str: '其他',
        status: ''
      },
    ]
  },

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

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getParents();
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  /**
   * 家长列表
   */
  getParents:function(){
    const { vipId } = this.data;
    const postData = { fn: this.getParents, param: { ...arguments } }
    app.post('/api/parents/parents/V2/parentsRoleList', { vipId}, 0, postData).then(res => {
      const { roleList } = res.data;
      this.setData({ roleList})
    })
  },
  /**
   * 展示角色
   */
  clickShow:function(){
    const { isRole}=this.data;
    this.setData({ isRole:!isRole})
  },
  /**
   * 选择角色
   */
  selectRole:function(e){
    const { parentList, role, isRole}=this.data;
    const {idx,str}=e.currentTarget.dataset;
    // const {role } = e.currentTarget.dataset
    console.log(e)
    var temp={};
    for (var i in parentList) {
      temp[`parentList[${i}].status`] = '';
    } 
    temp[`parentList[${idx}].status`] = parentList[idx].status == '' ?'active':''; 
    this.setData(temp)
    this.setData({ role: str,isRole:!isRole})
  }, 
  /**
   * 跳转添加家长
   */
  skipParents:function(){
    const { isAddParents}=this.data;
    this.setData({ isAddParents: !isAddParents})
  },
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false })
  },
  showModel:function(e){
    const { id } = e.currentTarget.dataset
    this.setData({ hiddenModel: true, roleUserId:id})
  }, 
  /**
   * 解绑家长
   */
  checkCtl:function(){
    const { roleUserId, vipId}=this.data;
    var that=this;
    const postData = { fn: this.getParents, param: { ...arguments } }
    app.post('/api/parents/parents/V2/untieParentsRole', { roleUserId, vipId }, 0, postData).then(res => {
      that.setData({ hiddenModel: false })
      that.getParents();
      wx.showToast({
        title: '解绑成功',
        icon: 'none',
        duration: 1000
      })
    })
  },
  /**
   * input
   */
  inputCtl: function (e) {
    const mobileExp = /^1[0-9]{10}$/;
    const { temp = {}, type } = e.currentTarget.dataset
    const { value } = e.detail
    temp[type] = value;
    const { phone, parents } = temp;
    if (type == 'phone') {
        this.setData({ phone })
        } else if (type == 'parents') {
          return this.setData({ parents })
        } 
    
  },
  /**
   * 添加家长
   */
  saveParents:function(){
    const mobileExp = /^1[0-9]{10}$/;
    const postData = { fn: this.saveParents, param: { ...arguments } }
    var _that = this;
    const { phone, parents, role, vipId, isAddParents} = this.data;
    console.log(phone, parents, role, vipId);
    if (!phone) {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none'
      })
      return
    } else if (!mobileExp.test(phone)) {
      wx.showToast({
        title: '手机号码位数不对',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!role) {
      wx.showToast({
        title: '请选择家长角色',
        icon: 'none'
      })
      return
    }
    if (!parents) {
      wx.showToast({
        title: '请输入家长姓名',
        icon: 'none'
      })
      return
    }
    app.post('/api/parents/parents/V2/addParentsRole', { 
      vipId,
      phone,
      roleName:role,
      name: parents
      }, 0, postData).then(res => {
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          duration: 1000
        })
        _that.setData({ isAddParents: !isAddParents });
        _that.getParents();
        // setTimeout((function callback() {
        //   wx.navigateBack({});
        // }).bind(this), 2000);
    })
  }
})