var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    user:{
      header_img:'../../../images/header_img02.gif'
    },
    user_format:{},
    is_show:0
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function(){
      self.getIndex();
    });
  },
  onShow:function(){
    this.setData({
      is_show:1
    })
  },
  getIndex: function (code) {
    var self = this;
    var data = '';
    var action = 'c=Member&a=index';
    var method = 'GET';
    if (code=='save'){
      var action = 'c=Member&a=saveuser';
      var method = 'POST';
      var data = self.data.user;
      app.showLoading('加载中...');
    }
    
    app.ajaxRequest(action, method, data, function (ret) {
      if (ret.status == 1) {
        if (ret.code=='save'){
          app.HCtoast('操作成功！');
          setTimeout(function () {
            app.HCnavigateBack();
          }, 2000);
          return;
        }
        var user = self.data.user; var user_format = self.data.user_format;
        if (ret.info.header_img){
          user.header_img = ret.info.header_img;
        }
        if (ret.info.real_name) {
          user.real_name = ret.info.real_name;
        }
        if (ret.info.idcard) {
          user.IDcard = ret.info.idcard;
        }
        if (ret.info.email) {
          user.email = ret.info.email;
        }
        if (ret.info.birthday){
          user.birthday = ret.info.birthday;
        }
        if (ret.info.six_array) {
          user.six_array = ret.info.six_array;
        }
        if (ret.info.constellationData) {
          user.constellationData = ret.info.constellationData;
        }
        if (ret.info.six_name){
          user_format.six_name = ret.info.six_name;
        }
        if (ret.info.constellation_name) {
          user_format.constellation_name = ret.info.constellation_name;
        }
        self.setData({
          user: user,
          user_format: user_format
        });
      }else{
        app.HCerror('提示', ret.msg, false); return;
      }
    }, true);
  },
  bindPickerChange:function(e){
    var six_array=this.data.user.six_array;
    var user_format = this.data.user_format;
    if (six_array){
      user_format.sex= e.detail.value;
      user_format.six_name = six_array[e.detail.value];
      this.setData({
        user_format: user_format
      });
    } 
  },
  bindConstellationChange:function(e){
    var constellation_array = this.data.user.constellationData;
    var user_format = this.data.user_format;
    if (constellation_array){
      user_format.constellation = constellation_array[e.detail.value];
      user_format.constellation_name= constellation_array[e.detail.value];
      this.setData({
        user_format: user_format
      });
    } 
  },
  uploadimg:function(){
    var self = this;
    var user = self.data.user;
    app.HCaddimg(function(ret,res){
      if(ret.status==1){
        // user.header_img = res.tempFilePaths[0];
        // user.header_img_save = ret.data;
        user.header_img = ret.data;
        self.setData({
          user: user
        });
      }
    });
  },
  formSubmit:function(event){
    var value = event.detail.value;
    var user = this.data.user;
    var user_format = this.data.user_format;
    if (user_format.sex && user_format.six_name){
      user.sex = user_format.sex;
      user.six_name = user_format.six_name;
    }
    if (user_format.constellation && user_format.constellation_name) {
      user.constellation = user_format.constellation;
      user.constellation_name = user_format.constellation_name;
    }
    if (value.real_name){
      user.real_name = value.real_name;
    }
    if (value.IDcard) {
      user.IDcard = value.IDcard;
    } 
    if (value.birthday) {
      user.birthday = value.birthday;
    } 
    if (value.email) {
      if (!$.isEmail(value.email)){
        app.HCerror('提示','邮箱格式不正确',false);return;
      }
      user.email = value.email;
    }
    this.getIndex('save');
  },
  goToMaintain: function (event) {
    app.HCnavigateTo('../maintain/maintain');
  }
})