const tools = require('../../utils/tools.js');
const app = require('../../utils/util.js');
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    avatar:"",
    loadvisible:false,
    visible:false,

    jobArr: [],
    jobtext: '请选择',
    kaihutext: '请选择',
    dytext: '请选择',
    wgztext: '请选择',
    ldztext: '请选择',
    dyztext: '请选择',
    gygtext: '请选择',
    kaihuList: [{
        name: '小学'
      },
      {
        name: '初中'
      },
      {
        name: '高中(职高、中专)'
      },
      {
        name: '大专(高职)'
      }, {
        name: '本科'
      }, {
        name: '硕士研究生'
      }, {
        name: '博士研究生'
      },
    ],
    kaihuindex: 0,
    nameIsTrue: false, //名字是否填写
    cultureIsTrue: false, //文化是否填写
    graduationIsTrue: false, //毕业学校
    majorIsTrue: false, //所有专业
    postIsTrue: false, //现任职务
    renzhishijianIsTrue: false,
    phoneIsTrue: false, //电话
    peixunshijianTrue: false, // 培训时间
    nameMessage: '',
    tenure: '', //在线时间
    peixunTime: '', //培训时间
    headmasterText: '请选择任职时间', // 
    peixunText: '请选择培训时间',
    radio: '1',
    username: 'asdsadsad',
    info: {},
    parameter: [{
      id: 1,
      name: '男',
      value: '0',
      checked: false
    }, {
      id: 2,
      name: '女',
      checked: false,
      value: '1',
    }],
    dyArr: ['否', '是'],
    wgzArr: ['否', '是'],
    ldzArr: ['否', '是'],
    dyzArr: ['否', '是'],
    gygArr: ['否', '是'],
    renzhiDate: '',
    postshow: false,
    postshow1: false,
    errorIsTrue: false,
    // currentDate: new Date().getTime(),
    // currentDate1: new Date().getTime(),
    // minDate: new Date().getTime,
    // formatter(type, value) {
    //   if (type === 'year') {
    //     return `${value}年`;
    //   } else if (type === 'month') {
    //     return `${value}月`;
    //   }
    //   return value;
    // },
    currentDate: new Date().getTime(),
    // minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    // maxDate: new Date().getTime(),
    minDate: new Date(1949, 10, 1).getTime(),
  },
  // ontimeInput(e){
  //   this.setData({
  //     currentDate: e.detail,
  //   });
  // },

  bindGetUserInfo (e) {
    var that=this;
    console.log(e.detail.userInfo)
    console.log(typeof e.detail.userInfo)
    if(typeof e.detail.userInfo!='undefined'){
      that.setData({avatar:e.detail.userInfo.avatarUrl});
    }

    that.setData({visible:true});
    console.log(that.data.avatar);
    
  },

  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  jobChange(e) {
    console.log(e)
    this.setData({
      zwIndex: e.detail.value,
      jobtext: this.data.jobArr[e.detail.value].dictLabel
    })
  },
  dyChange(e) {
    console.log(e)
    this.setData({
      dyIndex: e.detail.value,
      dytext: this.data.dyArr[e.detail.value]
    })
  },
  // 网格长
  wgzChange(e) {
    console.log(e)
    this.setData({
      wgzIndex: e.detail.value,
      wgztext: this.data.wgzArr[e.detail.value]
    })
  },
  // 楼栋长
  ldzChange(e) {
    console.log(e)
    this.setData({
      ldzIndex: e.detail.value,
      ldztext: this.data.ldzArr[e.detail.value]
    })
  },
  // 单元长
  dyzChange(e) {
    console.log(e)
    this.setData({
      dyzIndex: e.detail.value,
      dyztext: this.data.dyzArr[e.detail.value]
    })
  },
  // 公益岗
  gygChange(e) {
    console.log(e)
    this.setData({
      gygIndex: e.detail.value,
      gygtext: this.data.gygArr[e.detail.value]
    })
  },
  share() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      postshow: false
    });
  },
  onClose1() {
    this.setData({
      postshow1: false
    });
  },
  // onInput(event) {
  //   this.setData({
  //     currentDate: event.detail,
  //   });
  // },
  onInput1(event) {
    this.setData({
      currentDate1: event.detail,
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()-1}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  kaihuListChange(e) {
    console.log(e, "8888888888888")
    this.setData({
      kaihuindex: e.detail.value,
      kaihutext: this.data.kaihuList[e.detail.value].name,
    })
  },
  share1() {
    this.setData({
      postshow: true
    });
  },
  share() {
    this.setData({
      postshow1: true
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()-1}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    let timeValue = this.timeFormat(new Date(event.detail), "yyyy-MM-dd");
    this.setData({
      tenure: timeValue,
      postshow: false,
      headmasterText: ''
    });
  },
  onConfirm1(event) {
    let timeValue = this.timeFormat(new Date(event.detail), "yyyy-MM-dd");
    this.setData({
      peixunTime: timeValue,
      postshow1: false,
      peixunText: ''
    });
  },
  parameterTap: function (e) {
    let that = this
    let this_checked = e.currentTarget.dataset.id
    let parameterList = this.data.parameter //获取Json数组
    for (let i = 0; i < parameterList.length; i++) {
      if (parameterList[i].id == this_checked) {
        parameterList[i].checked = true; //当前点击的位置为true即选中
      } else {
        parameterList[i].checked = false; //其他的位置为false
      }
    }
    that.setData({
      parameter: parameterList
    })
  },
  formSubmit(e) {
    e.detail.value.culture = this.data.kaihuList[this.data.kaihuindex].name;
    e.detail.value.studentDuty = isNaN(this.data.zwIndex) ? '' : this.data.jobArr[this.data.zwIndex].dictValue
    e.detail.value.tenure = this.data.tenure;
    e.detail.value.sessionKey = this.data.sessionKey;
    let aa = e.detail.value
    console.log(this.data.avatar);
    if (aa.culture && aa.school && aa.studentDuty && aa.tenure && aa.studentDept && aa.studentPhone) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/student/upStudent',
        method: "post",
        data: {
          studentDept: aa.studentDept,
          culture: aa.culture,
          school: aa.school,
          studentDuty: aa.studentDuty,
          tenure: aa.tenure,
          studentPhone: aa.studentPhone,
          sessionKey: this.data.sessionKey,
          mcajor: aa.mcajor,
          gridLeader: this.data.wgzIndex, //网格长
          buildingLeader: this.data.ldzIndex, //楼栋长
          unitLeader: this.data.dyzIndex, //单元长
          welfarePosition: this.data.gygIndex, //公益岗位
          partyMember: this.data.dyIndex, //党员
          studentHead:this.data.avatar//微信头像
        },
        success: () => {
          wx.showModal({
            showCancel: false,
            content: '报到成功',
            success() {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        },
        fail: (res) => {
          console.log(res)
          wx.showModal({
            showCancel: false,
            content: '报到失败',
            success() {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      })
    } else {
      wx.showModal({
        showCancel: false,
        content: '请确认是否还有其他必填项没有填写'
      })
    }
  },
  timeFormat(date, fmt) {
    let o = {
      "M+": date.getMonth() + 1, //月份   
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时   
      "m+": date.getMinutes(), //分   
      "s+": date.getSeconds(), //秒   
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
      "S": date.getMilliseconds() //毫秒   
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  getjob(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/position',
      method: "get",
      data: {
        sessionKey
      },
      success: (res) => {
        console.log(res, '我是获取到的数组')
        let Arr = [];
        res.data.data.forEach(item => {
          let newdata = {};
          newdata.dictLabel = item.dictLabel;
          newdata.dictValue = item.dictValue;
          Arr.push(newdata);
        });
        this.setData({
          jobArr: Arr
        })
        this.getBanner(sessionKey);
        console.log(this.data.jobArr, '我是新组成的数组')
      },
      fail: () => {
        wx.hideLoading();
      }
    })
  },
  onReady: function () {},







  async onLoad() {
    console.log(this.data.loadvisible);
    var that=this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const sessionKey = await tools.checkSessionAndLogin();
    console.log(sessionKey, "000000");
    this.setData({
      sessionKey: sessionKey
    })
    this.getjob(sessionKey)



    // 查看是否授权
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              that.setData({visible:true,loadvisible:true,avatar:res.userInfo.avatar});
              console.log(res.userInfo)
              console.log(that.data.loadvisible);
            }
          })
        }else{
          // 如果没有授权，表单不可见
          that.setData({loadvisible:true,visible:false});
        }
      },
      fail(){
        //接口调用失败需要返回重试
        wx.showModal({
          showCancel: false,
          content: '网络错误，请稍后再试',
          success() {
            wx.navigateBack({
              delta: 1
            })
          }
        })

      }
    })
  },
  getBanner(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKeyEcho',
      method: "post",
      data: {
        sessionKey: sessionKey,
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          this.data.parameter.forEach(item => {
            if (item.value == res.data.data.studentSex) {
              item.checked = true;
            }
          })
          this.setData({
            info: res.data.data,
            parameter: this.data.parameter,
            studentDept: res.data.data.studentDept,
            zwIndex: Number(res.data.data.studentDuty) - 1,
            // jobtext: res.data.data.studentDuty,
            kaihutext: res.data.data.culture,
            mcajor: res.data.data.mcajor,
            tenure: res.data.data.tenure,
            studentPhone: res.data.data.studentPhone,
            wgzIndex: res.data.data.gridLeader, //网格长
            ldzIndex: res.data.data.buildingLeader, //楼栋长
            dyzIndex: res.data.data.unitLeader, //单元长
            gygIndex: res.data.data.welfarePosition, //公益岗位
            dyIndex: res.data.data.partyMember //党员
          })
          wx.hideLoading();
        }
      },
      fail: () => {
        wx.hideLoading();
      }
    })
  }
})