const util = require("../../utils/util.js");
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '1988-03-12',
    flag: 1,
    flagsex: 0,
    items: [
      { name: 1, value: '男', checked: false },
      { name: 2, value: '女', checked: false },
    ],
    username: "",
    array: ['上班族', '学生', '企业高管', '个体户', '自由职业', '其他'],
    array1: ['附近住户', '距离2km', '距离3km', '更远'],
    index: 0,
    index1: 0,
    showPage:false
  },
  /**
   * 获得生日
   */
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      flag: 2
    })
  },
  /**
   * 职业
   */
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生活半径
   */
  bindPickerChange1: function (e) {
    this.setData({
      index1: e.detail.value
    })
  },
  address: function () {
    let that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.name + '(' + res.address + ')',
          addressStr: JSON.stringify(res)
        })
      },
      fail: function (error) {
        console.log(error)
      },
      complete: function (data) {
      }
    })
  },
  formSubmit: function (e) {
    let that = this;
    /**
     * sex
     * 0 保密
     * 1 男
     * 2 女
     */
    if (that.data.flagsex == 0) {
      var sex = 0;
    } else {
      var sex = that.data.sex;
    }
    if (that.data.addressStr) {
      var address = that.data.addressStr;
    }
    else {
      var address = null;
    }
    if (e.detail.value.username) {
      var username = e.detail.value.username;
    }
    else if (that.data.username) {
      var username = that.data.username;
    }
    else {
      var username = null;
    }
    if (that.data.flag == 2 && that.data.date) {
      var birthdate = new Date(that.data.date).getTime();
    } else {
      var birthdate = null;
    }
    if (username == null || address == null || sex == 0 || birthdate == null) {
      wx.showModal({
        title: '提示',
        content: '请输入完整的用户信息',
        showCancel: false
      })
    } else {
      Http.post({
        url: config.api.updateInfo,
        data: {
          sex: sex,
          address: address,
          name: username,
          birthdate: birthdate,
        }
      })
      .then(res => {
        wx.showModal({
          title: '提示',
          content: '修改成功',
          showCancel: false,
          success: function (res) {
            wx.switchTab({
              url: '/pages/user/index',
            })
          }
        })
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
    }
  },
  radioChange: function (e) {
    this.setData({
      sex: e.detail.value,
      flagsex: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    Http.get({
      url: config.api.getScore,
      data: {}
    })
    .then(res => {
      if(res.code == 200){
        that.setData({
          showPage:true
        })
      }
      console.log(res.data.address);
      var reg = RegExp(/address/);
      if (res.data.address.match(reg)) {
        that.setData({
          address: JSON.parse(res.data.address).address + JSON.parse(res.data.address).name,
          addressStr: JSON.parse(res.data.address).address + JSON.parse(res.data.address).name
        })    
      }else{
        that.setData({
          address: res.data.address,
          addressStr: res.data.address
        })
      }
      if (res.data.name) {
        that.setData({
          username: res.data.name
        })
      }
      if (res.data.sex) {
        if (res.data.sex == 1) {
          var checked = 'items[' + 0 + '].checked'
          that.setData({
            [checked]: true,
            flagsex: 1
          })
        } else if (res.data.sex == 2) {
          var checked = 'items[' + 1 + '].checked'
          that.setData({
            [checked]: true,
            flagsex: 1
          })
        }
        that.setData({
          sex: res.data.sex
        })
      }
      if (res.data.birthdate) {
        that.setData({
          date: util.fmtDate(parseInt(res.data.birthdate)),
          flag: 2
        })
      }
    })
    .catch(error=>{
      wx.showModal({
        title: '提示',
        content: error.errMsg,
        showCancel:false
      })
    })
  }
})