// pages/home/home.js
var app = getApp();
var baseUrl = app.globalData.baseUrl;
Page({
  self_id:null,
  /**
   * 页面的初始数据
   */
  data: {
    load_finish: false,
    text_tip: "自然人信息不完善,立即完善",
    taxlist: ['']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onLoad')
    if (app.globalData.OSSUrl.length == 0) {
      app.configOssUrl()
    }

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    console.log('onShow')
    this.getTaxList()
  },

  getTaxList: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    var Authorization = app.globalData.Authorization;
    console.log('Authorization', Authorization)
    if (!Authorization || Authorization.length < 10) {
      return
    }
    wx.request({
      url: baseUrl + 'persontax/v1/personal-taxes',
      method: "GET",
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": Authorization
      },
      data: {
        'all': true
      },
      success: function (res) {
        console.log('succ', res)
        if (res.statusCode == 200) {
          that.handleData(res.data);
        } else {
          // wx.showModal({
          //   title: res.data.message,
          //   content: '',
          //   showCancel: false,
          //   success: function (res) {
          //     if (res.confirm) {
          //       console.log('用户点击确定')
          //       wx.redirectTo({
          //         url: '../../getPhone/getPhone',
          //       })
          //     } else if (res.cancel) {
          //       console.log('用户点击取消')
          //     }
          //   }
          // })
        }
      },
      fail(res) {
        console.log(res.data)
      },
      complete() {
        wx.hideLoading()
        that.setData({
          load_finish: true
        })
      }
    })
  },

  handleData: function (data) {
    if (data.items && data.items.length > 0) {
      var datalist = []

      for (var i = 0; i < data.items.length; i++) {
        data.items[i].newmobile = this.formatMobile(data.items[i].mobile)
        data.items[i].newidnum = this.formatIdNum(data.items[i].id_card_no)
        data.items[i].relative = data.items[i].family_ties
        if (data.items[i].family_ties=="本人"){
          this.self_id = data.items[i].id
        }
        // if (i == 0) {
        //   data.items[i].relative = "本人"
        //   this.self_id = data.items[i].id
        // }else{
        //   data.items[i].relative = data.items[i].family_ties
        // }
        datalist.push(data.items[i])
      }
      this.setData({
        taxlist: datalist
      })
      this.getStatus(datalist)
    }
    console.log('handleData', this.data.taxlist)
  },

  formatMobile: function (mobile) {
    if (mobile && mobile > 7) {
      return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    } else {
      return mobile
    }
  },

  formatIdNum: function (idnum) {
    if (idnum && idnum.length >= 6 && idnum.length < 10) {
      return idnum.substring(0, 2) + '****' + idnum.substring(idnum.length - 2, idnum.length);
    } else if (idnum.length >= 10) {
      return idnum.substring(0, 4) + '****' + idnum.substring(idnum.length - 4, idnum.length);
    } else {
      return idnum
    }
  },

  getStatus: function (data) {
    console.log('data', data)
    if (data[0].family_ties.length < 1 && data[0].name.length > 0 && data[0].id_card_type.length > 0 && data[0].id_card_no.length > 0 && data[0].mobile.length > 0) {
      this.setData({
        text_tip: '去查看'
      })
    } else {
      this.setData({
        text_tip: '自然人信息不完善,立即完善'
      })
    }
  },

  goadd: function (e) {
    console.log(e)
    wx.navigateTo({
      url: 'addinfo/addinfo',
    })
  },

  goedit: function (e) {
    console.log(e)
    if (this.self_id != e.currentTarget.id){
      wx.navigateTo({
        url: 'addinfo/addinfo?id=' + e.currentTarget.id,
      })
    }else {
      wx.navigateTo({
        url: 'editinfo/editinfo?id=' + e.currentTarget.id,
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})