const app = getApp();
const request = require('../../utils/request.js');
const tools = require('../../utils/tools.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {
    arr: [{
      name: '',
      phone: '',
      userid: '',
      password: ''
    }],
    flag: false,
    readonly: false
  },
  onNameChange(e) {
    console.log(e)
    this.setData({
      ['arr[' + e.target.dataset.index + '].name']: e.detail
    })
  },
  ontelChange(e) {
    this.setData({
      ['arr[' + e.target.dataset.index + '].phone']: e.detail
    })
  },
  onUseridChange(e) {
    this.setData({
      ['arr[' + e.target.dataset.index + '].userid']: e.detail
    })
  },
  onPasswordChange(e) {
    this.setData({
      ['arr[' + e.target.dataset.index + '].password']: e.detail
    })
  },
  add() {
    if (this.data.arr.length > 2) {
      wx.showToast({
        title: '最多3个哦',
        duration: 2000, //显示时长
        mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
        // icon:'warn', //图标，支持"success"、"loading"  
      })
      return;
    }
    let arr = this.data.arr;
    arr.push({
      name: '',
      phone: '',
      userid: '',
      password: ''
    });
    this.setData({
      arr
    })
  },
  async getGuardianInfo() {
    let options = {
      url: 'resource/miniapp/user/getJhrList',
      data: {
        openId: wx.getStorageSync('openid'),
        type: app.globalData.type
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code == 200) {
      this.setData({
        arr: res.data.data.length > 0 ? res.data.data : [{
          name: '',
          phone: '',
          userid: '',
          password: ''
        }]
      })
    } else {
      Toast.fail(res.data.msg);
    }
  },
  async save(e) {
    if (e.target.dataset.item.name || e.target.dataset.item.phone || e.target.dataset.item.userid || e.target.dataset.item.password) {
      let jhrList = [];
      jhrList.push(e.target.dataset.item)
      let url = (e.target.dataset.item.id && this.data.arr[0].id) ? 'resource/miniapp/user/updateJhrInfo' : 'resource/miniapp/user/addJhrInfo';
      let editObj = {
        jhrList
      };
      let addObj = {
        jhrList,
        openId: wx.getStorageSync('openid'),
        type: app.globalData.type
      };
      console.log(e.target.dataset.item.id);
      console.log(this.data.arr[0].id);
      console.log((e.target.dataset.item.id || this.data.arr[0].id))
      let data = (e.target.dataset.item.id && this.data.arr[0].id) ? editObj : addObj;
      let options = {
        url,
        data,
        method: 'post'
      }
      let res = await request.query2(options);
      if (res.data.code === 200) {
        wx.showModal({
          title: '提示',
          content: '保存成功',
          showCancel: false,
          success: (res) => {
            if (res.confirm) {
              this.getGuardianInfo();
            }
          }
        })
      } else {
        Toast.fail(res.data.msg);
      }
    } else {
      Toast.fail('请至少填写一项信息');
    }
  },
  async delete(e) {
    if (!e.target.dataset.item.id) {
      this.data.arr.splice(e.target.dataset.index, 1);
      this.setData({
        arr: this.data.arr.length === 0 ? [{
          name: '',
          phone: '',
          userid: '',
          password: ''
        }] : this.data.arr
      })
      return false;
    }
    let options = {
      url: 'resource/miniapp/user/deleteJhrInfo',
      data: {
        jhrId: e.target.dataset.item.id
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      wx.showModal({
        title: '提示',
        content: '删除成功',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            this.getGuardianInfo();
          }
        }
      })
    } else {
      Toast.fail(res.data.msg);
    }
  },
  onShow: function () {
    if (!wx.getStorageSync('userId')) {
      wx.showModal({
        title: '提示',
        content: '请扫描二维码进入小程序',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    tools.isLogin().then((res) => {
      this.getGuardianInfo();
      this.setData({
        readonly: app.globalData.type === 'jhdx' ? false : true
      })
    }, (error) => {
      console.log(error);
    })
  }
})