var Api = require('../../utils/api.js');
var app = getApp();
//他评量表
Page({
  data: {
    title: "录入信息",
    array: ['请选择', '小学', '初中', '高中（包括中专和普通高中）', '大学（包括高职、大专及本科）', '研究生及以上'],
    array1: ['请选择', '已婚', '未婚', '离异', '丧偶'],
    date: '1980-01-01',
    time: '请选择时间',
    items: [
      { name: '男', value: '男' },
      { name: '女', value: '女' },
    ],
    sex: '',
    username: '',
    username1: '',
    username2: '',
    index: 0,
    index1: 0,
    title1: "", // 答题
    content: '',
    otype: -2,
    lb_id: '',
    lb_tz: -1,
    tplb: -1,
    show: false,
    payInfo: true
  },
  onLoad: function (options) {
    var that = this;
    var info = wx.getStorageSync('tplbDetail');
    this.setData({
      tplb: 1
    })
    wx.setStorageSync('addFriend', true);
    console.log(info, 'info')
    this.loaditem(info);
    this.loadData();
    wx.setNavigationBarTitle({
      title: this.data.title
    });
  },
  loaditem(e) {
   var loadPhoto = setTimeout(function(){
     wx.showLoading({
       title: '加载中...',
     })
   },1000)
    // 咨询师的openID 和时间
    let that = this;
    var user = wx.getStorageSync('user');
    var data = {};
    var urlt = Api.HOST + 'wechatIteration.php?s=Measure/t_up_price';
    data.z_openid = e.z_openid;
    data.t_openid = e.t_openid;
    data.g_time = e.time;
    Api.fetchPost(urlt, data, (err, res) => {
      that.setData({
        lb_id: res[0],
      })
      var lb_descript = {
        id: res,
        desc: e
      }
      wx.setStorageSync("lb_descript", lb_descript);
      // var url = Api.HOST_xinli + 'xinli/index.php/Api/measure';
      // var url = Api.HOST + 'wechatIteration.php?s=Measure/measure_json_arr';
      var url = Api.HOST_xinli_lb + 'measure/' + res[0] + '.json';
      var data = {};
      data.measure_id = res[0];
      wx.request({
        method: 'GET',
        url: url,
        // data: data,
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success(res) {
          var res = res.data;
      // Api.fetchPost(url, data, (err, res) => {
        clearTimeout(loadPhoto)
        wx.hideLoading()
        that.setData({
          content: res[0].measure_desc,
          title1: res[0].measure_name,
          show: true
        })
        res[0].measure_id = data.measure_id;
        wx.setStorageSync('lb_swiper_con', res)
        if (res[0].type == 1) {
          wx.setStorageSync('itemData', res)
          that.setData({
            lb_tz: 1
          })
        } else {
          that.setData({
            lb_tz: 0
          })
          var obj = {
            res: res,
            talp: 1
          }
          wx.setStorageSync('liangbiaoCon', obj)
        }
        wx.setStorageSync('lb_tz', 1)
        }
      })
    })
  },
  /**
   * 自定义函数---加载数据
   * */
  loadData() {
    var that = this;
    // 获取授权  上传头像昵称  得到这个用户的信息
    var url = Api.HOST + 'wechatIteration.php?s=Home/getinfo';
    var data = {};
    var user = wx.getStorageSync('user');
    if (this.data.tplb != '1') {
      data.openid = user.openid;
    } else {
      var info = wx.getStorageSync('tplbDetail');
      data.openid = info.z_openid;
    }
    Api.fetchPost(url, data, (err, res) => {
      if (res.info) {
        if (res.info.cultural == '') {
          res.info.cultural = 0;
        } else {
          res.info.cultural = res.info.cultural;
        }
        if (res.info.birthday == null) {
          res.info.birthday = "请选择";
        } else {
          res.info.birthday = res.info.birthday;
        }
        that.setData({
          username: res.info.name,
          index: res.info.cultural,
          date: res.info.birthday,
          sex: res.info.xingbie,
          index1: res.info.hunfou,
          username1: res.info.ask_department,
          username2: res.info.profession ? res.info.profession:' '
        });
      }
    });
  },
  inputChange: function (e) {
    this.setData({
      username: e.detail.value,
    })
  },
  people: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },

  //性别
  radioChange: function (e) {
    this.setData({
      sex: e.detail.value,
    })
  },
  //日期
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 学历
  bindPickerChange: function (e) {
    var num = e.detail.value;
    this.setData({
      index: num
    })
  },
  // 单位名称
  inputChange1: function (e) {
    this.setData({
      username1: e.detail.value,
    })
  },
  //职业
  inputChange2: function (e) {
    this.setData({
      username2: e.detail.value,
    })
  },
  // 婚姻状况
  bindPickerChange1: function (e) {
    var num = e.detail.value;
    this.setData({
      index1: num
    })
  },
  radioChange1: function (e) {
    this.setData({
      marr: e.detail.value,
    })
  },
  submit: function () {
    var that = this;
    if (this.data.payInfo) {
     
      var user = wx.getStorageSync('user');
      
      var unitid = (user.unitid || user.unitid != 0) ? user.unitid : "";
      var urlt = Api.HOST + 'wechatIteration.php?s=Home/stinfo';
      var data = {};
      var itemData = wx.getStorageSync("itemData");
      data.unitid = unitid ? unitid : '';
      var info = wx.getStorageSync('tplbDetail');
      data.openid = info.z_openid;
      data.name = this.data.username;
      data.sex = this.data.sex;
      data.birthday = this.data.date;
      // data.hunfou = this.data.array1[this.data.select];  // 婚姻状况
      data.profession = this.data.username2||' '; // 职业
      // 新添加学历功能
      data.edu = this.data.index;
      // console.log(data,'data')
      if (data.name == '' || data.name == 'null' || data.sex == "null" || data.sex == "" || data.birthday == '' || data.birthday == 'null' || data.edu == 0) {
        // wx.hideLoading()
        clearTimeout(timer1)
        Api.alert('请完善信息');
        that.setData({ payInfo: true })
        return;
      }
      var timer1 = setTimeout(function () {
        // Api.alert("加载失败，请重试")
        wx.hideLoading()
        that.setData({ payInfo: true })
      }, 5000)
      wx.showLoading({
        title: '请稍后',
      })
      this.setData({ payInfo: false })
      Api.fetchPost(urlt, data, (err, res) => {
        wx.hideLoading()
        clearTimeout(timer1)
        if (res.ret == 1) {
          if (that.data.lb_tz == '1') {         
            wx.navigateTo({
              url: '/pages/dati01/dati01?tplb=1',
              success: function () {
                setTimeout(function () {
                  that.setData({ payInfo: true })
                }, 500)
              }
            })
          } else if (that.data.lb_tz == '0') {
            wx.navigateTo({
              url: '/pages/dati04/dati04?tplb=1',
              success: function(){
                setTimeout(function(){
                  that.setData({payInfo:true})
                },500)
              }
            })
          }

        } else {
          Api.alert(res.msg);
        }
      });
    }
  },
  onUnload() {
  },
  // 时间转换
  resTime: function (str) {
    var hour = str.split(":")[0];
    var minute = str.split(":")[1];
    var sec = str.split(":")[2];
    return (hour * 3600 + minute * 60 + sec);
  },
  fengei: function (str) {
    return str.split(',');
  },
  // 数组求和
  add: function (arr) {
    var num = 0;
    for (let i = 0; i < arr.length; i++) {
      num += Number(arr[i]);
    }
    return num;
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

})
