const navigationBarHeight = (getApp().statusBarHeight + 60) + 'px'
const util = require("../../utils/util.js");
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
const imgurl = require("../../utils/imgurl");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    childArr:[],
    navigationBarHeight,
    chevronUrl: imgurl.chevron.url,
    date: '1988-03-12',
    dateEnd:'',
    flag: 1,
    flagsex: 0,
    height:"",
    weight:"",
    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;
    tt.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) { 
    console.log(e.detail,"formSubmit")
    let that = this;
    // console.log(that.data.childArr)
    /**
     * 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) {
      tt.showToast({
        title: '请输入完整的用户信息',
        icon:"none"
      })
    } else {
      let tmeparr = []
      tmeparr = JSON.parse(JSON.stringify(that.data.childArr)) 
      console.log(that.data.childArr)
      tmeparr.map(item=>{
        item.birthdate = new Date(item.birthdate).getTime()
      })
      console.log(that.data.childArr)
      Http.post({
        url: config.api.updateInfo,
        data: {
          sex: sex,
          address: address,
          name: username,
          birthdate: birthdate,
          childrenList: tmeparr,
          height: e.detail.value.height ? e.detail.value.height : that.data.height,
          weight: e.detail.value.weight ? e.detail.value.weight : that.data.weight,
        }
      })
      .then(res => {
        tt.showToast({
          title: "信息提交成功，将在3分钟内生效",
          icon: 'none',
          duration: 2000,
          mask: false
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        })
      })
    }
  },
  radioChange: function (e) {
    this.setData({
      sex: e.detail.value,
      flagsex: 1
    })
  },
  childSex(e){
    let index = e.currentTarget.dataset.index
    let tmpeObj = this.data.childArr
    tmpeObj[index].sex = e.detail.value
    this.setData({
      childArr: tmpeObj
    })
    console.log(tmpeObj)
  },
  chidBirthday(e){
    let index = e.currentTarget.dataset.index
    let tmpeObj = this.data.childArr
    tmpeObj[index].birthdate = e.detail.value
    this.setData({
      childArr: tmpeObj
    })
  },
  chidName(e){
    let index = e.currentTarget.dataset.index
    let tmpeObj = this.data.childArr
    tmpeObj[index].name = e.detail.value
    this.setData({
      childArr: tmpeObj
    })
  },
  childDel(e){
    let index = e.currentTarget.dataset.index
    let tmpeObj = this.data.childArr
    tmpeObj.splice(index,1)
    this.setData({
      childArr: tmpeObj
    })
  },
  childAdd(){
    let tmpeObj = this.data.childArr
    tmpeObj.unshift({ sex:1})
    this.setData({
      childArr: tmpeObj
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let tmpeDateEnd = util.formatTime(new Date(), "yyyy-MM-dd") 
    let that = this;
    that.setData({
      dateEnd: tmpeDateEnd
    })
    Http.get({
      url: config.api.getScore,
      data: {},
    })
    .then(res => {
      console.log(res);
      if(res.code == 200){
        if (res.data.weight){
          this.setData({
            weight: res.data.weight
          })
        }
        if (res.data.height) {
          this.setData({
            height: res.data.height
          })
        }
        
        if (res.data.childrenList){
          let tmpeArr = res.data.childrenList
          tmpeArr.map(item => {
            item.birthdate = util.fmtDate(parseInt(item.birthdate))
            that.setData({
              childArr: tmpeArr
            })
          })
        }
        that.setData({
          showPage:true,
        })
      }
      var reg = RegExp(/address/);
      if (res&&res.data&&res.data.address&&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=>{
      tt.showToast({
        title: error.errMsg,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  }
})