import {
  imgServerUrl
} from '../../config/config.js'
import {
  selectInfo
} from '../../services/index.js'
import {
  showToast
} from '../../utils/tips.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServerUrl: imgServerUrl,
    eduList: ["请选择", "小学", "中学", "高中", "大专", "本科", "研究生", "博士", "博士后"],
    items: [{
      name: '0',
      value: '0'
    },
    {
      name: '1',
      value: '1'
    },
    {
      name: '2',
      value: '2'
    },
    ],
    userList: [],
    userRealName: [], //用户姓名加*数组
    userIdCardNumber: [], //用户身份证号加*数组
    select_index: null, //选择的用户信息索引
    currentUser: {}, //当前用户
    show_message: true,
    show_confirm: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userList = wx.getStorageSync('userList');
    var currentUser = wx.getStorageSync('currentUser');

    var userRealName = [];
    var userIdCardNumber = [];
    for (var i = 0; i < userList.length; i++) {
      var year;
      var realName;
      var idCardNumber;
      var realName1 = '';
      var idCardNumber1 = '';
      //年龄换算
      if (userList[i].hpResume != null) {
        year = userList[i].hpResume.birthYear;
        if (year) {
          var date = new Date;
          var year1 = date.getFullYear();
          year = year1 - year + 1;
        }
        //头像为空处理
        if (!userList[i].hpResume.headImg) {
          userList[i].hpResume.headImg = `${imgServerUrl}/images/avatar/man.png`;
        }
        userList[i].hpResume.birthYear = year;
      }
      if (userList[i].hpUser != null) {
        //真实姓名加*处理
        realName = userList[i].hpUser.realName;
        if (realName) {
          for (var j = 0; j < realName.length; j++) {
            if (j == 0) {
              realName1 = '*';
            } else {
              realName1 = realName1 + realName[j];
            }
          }
        }
        userRealName.push(realName1);

        //身份证号加*处理
        idCardNumber = userList[i].hpUser.idCardNumber;
        if (idCardNumber) {
          for (var j = 0; j < idCardNumber.length; j++) {
            if (j > 5 && j < 14) {
              idCardNumber1 = idCardNumber1 + '*';
            } else {
              idCardNumber1 = idCardNumber1 + idCardNumber[j];
            }
          }
        }
        userIdCardNumber.push(idCardNumber1);
      }
    }
    this.setData({
      userList: userList,
      userRealName: userRealName,
      userIdCardNumber: userIdCardNumber,
      currentUser: currentUser
    })
    // wx.showModal({
    //   title: '提示',
    //   content: '系统升级，一个用户只能保留唯一的用户信息，请选择您要保留的个人信息。',
    //   showCancel: false,
    //   confirmText: "好的",
    //   confirmColor: '#0177FF',
    // })
  },

  onShow: function () {

  },
  privacyCancel(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  closeMessage() {
    this.setData({
      show_message: false,
      show_confirm: false
    })
  },
  showConfirm() {
    this.setData({
      show_confirm: true
    })
  },
  goResume(e) {
    wx.setStorageSync("userResume", e.currentTarget.dataset.resume)
    wx.navigateTo({
      url: '../jinali-info/index',
    })
  },
  radioChange: function (e) {
    this.setData({
      select_index: e.detail.value
    })
  },
  submit(e) {
    var select_index = this.data.select_index;
    if (!select_index) {
      return false;
    }
    var currentUser_id = this.data.currentUser.id;//当前用户id
    var userId = this.data.userList[select_index].hpUser.id;
    var userIds = '';
    var phone = this.data.userList[select_index].hpUser.phone;
    var userList = [];
    for (var i = 0; i < this.data.userList.length; i++) {
      if (i != select_index) {
        userList.push(this.data.userList[i]);
      }
    }
    for (var i = 0; i < userList.length; i++) {
      if (i == 0) {
        userIds = userList[i].hpUser.id;
      } else {
        userIds = ',' + userIds;
      }
    }
    let paramsObj = {
      user_id: userId,
      phone: phone,
      userIds: userIds,
      login_type: 0, //0:小程序；1：微信授权；2：手机号
      currentUser_id: currentUser_id
    }
    Object.assign(paramsObj);
    selectInfo(paramsObj).then(data => {
      wx.setStorageSync("hpuser", data.data.hpUser);
      wx.setStorageSync("hpMember", data.data.hpMember);
      wx.setStorageSync("user_id", data.data.hpUser.id);
      wx.setStorageSync("member_id", data.data.hpMember.id);
      wx.setStorageSync("user_token", data.data.hpUser.userToken);
      wx.setStorageSync("phone", data.data.hpUser.phone);
      this.setData({
        show_confirm: false
      })
      showToast("合并成功");
      setTimeout(function () {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }, 500)
    })
  }
})