// pages/person/person.js
// pages/loginv2/loginv2.js
import JSEncrypt from '../../utils/jsencrypt.min.js'
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' +
  '2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='

const privateKey = 'MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8\n' +
  'mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9p\n' +
  'B6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue\n' +
  '/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZ\n' +
  'UBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6\n' +
  'vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha\n' +
  '4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3\n' +
  'tTbklZkD2A=='

// 加密
function encrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对需要加密的数据进行加密
}

// 解密
function decrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey)
  return encryptor.decrypt(txt)
}
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headPotraitPic: 'https://pic1.zhimg.com/80/v2-5f2e129e114b573ce25128ec0f519a46_720w.jpg',
    nameImgUrl: 'http://39.102.232.151:8030/cover/搜索icon.png',
    username: '小a',
    phone: '1888888888',
    email: '112233@qq.com',
    wechat: '',
    nickName: '',
    personIdentity: '',
    curChosenTab: '个人中心',
    isPullDown: false,
    staffList: [
    ],
    topAnimation: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const animation = wx.createAnimation({
      delay: 500,
      timingFunction: 'linear'
    })
    this.animation = animation
    // console.log(app.globalData, 'app.globalData')
    this.setData({
      personIdentity: app.globalData.curRole
    })
    this.setData({
      headPotraitPic: app.globalData.appBaseUrlV2 + '/avatar/' + app.globalData.userInfo.avatarName,
      username: app.globalData.userInfo.username ? app.globalData.userInfo.username : '',
      phone: app.globalData.userInfo.phone ? app.globalData.userInfo.phone : '',
      email: app.globalData.userInfo.email,
      wechat: app.globalData.userInfo.wechat ? app.globalData.userInfo.wechat : '',
      nickName: app.globalData.userInfo.nickName ? app.globalData.userInfo.nickName : ''
    })
    console.log(app.globalData.userInfo, 'app.globalData.userInfo')
    if (app.globalData.curRole === '企业管理' && (!(app.globalData.companyId === -1 || app.globalData.companyId === null))) {
      wx.request({
        url: app.globalData.appBaseUrlV2 + '/api1/thUser/findSignUpCompanyStaff',
        method: 'GET',
        data: {
          sort: 'verify_status,asc'
        },
        header: {
          Authorization: app.globalData.Authorization
        },
        success: (res) => {
          console.log(res, 'staff list staff list staff list')
          if (res.data && res.data.content) {
            this.setData({
              staffList: res.data.content
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '个人中心'
    })
    wx.setNavigationBarColor({
      backgroundColor: '#090D9C',
      frontColor: '#ffffff',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 跳转到个人中心
   */
  jumptoperson() {
    if (this.data.curChosenTab !== '个人中心') {
      this.setData({
        curChosenTab: '个人中心'
      })
    }
  },
  /**
   * 跳转到企业员工管理
   */
  jumptomanage() {
    if (this.data.curChosenTab !== '企业管理') {
      this.setData({
        curChosenTab: '企业管理'
      })
    }
  },
  pullDownRefresh() {
    wx.request({
      url: app.globalData.appBaseUrlV2 + '/api1/thUser/findSignUpCompanyStaff',
      method: 'GET',
      data: {
        sort: 'verify_status,asc'
      },
      header: {
        Authorization: app.globalData.Authorization
      },
      success: (res) => {
        console.log(res, 'staff list staff list staff list')
        if (res.data && res.data.content) {
          this.setData({
            staffList: res.data.content
          })
        }
      },
      complete: () => {
        this.setData({
          isPullDown: false
        })
      }
    })
  },
  verifyStaff(event) {
    if (event.currentTarget && event.currentTarget.dataset) {
      console.log(event.currentTarget.dataset.id, 'event.currentTarget.dateset.id')
      console.log(app.globalData.Authorization, 'app.globalData.Authorization')
      wx.request({
        url: app.globalData.appBaseUrlV2 + '/api1/thUser/verify',
        method: 'PUT',
        header: {
          Authorization: app.globalData.Authorization
        },
        data: {
          userId: event.currentTarget.dataset.id,
          verifyCase: true
        },
        success: (res) => {
          console.log(res, 'verify staff res ')
          if (res.statusCode === 204) {
            this.initStaffs()
          }
        },
        complete: () => {}
      })
    }
  },
  initStaffs() {
    wx.request({
      url: app.globalData.appBaseUrlV2 + '/api1/thUser/findSignUpCompanyStaff',
      method: 'GET',
      data: {
        sort: 'verify_status,asc'
      },
      header: {
        Authorization: app.globalData.Authorization
      },
      success: (res) => {
        console.log(res,'aaaaa')
        if (res.data && res.data.content) {
          this.setData({
            staffList: res.data.content
          })
        }
      },
      complete: () => {
      }
    })
  },
  deleteStaff(event) {
    if (event.currentTarget && event.currentTarget.dataset) {
      wx.showModal({
        title: '提示',
        content: '此操作将永久删除该用户，是否继续?',
        success: (res) => {
          if (res.confirm) {
            wx.request({
              url: app.globalData.appBaseUrlV2 + '/api1/thUser/delete',
              method: 'DELETE',
              data: [event.currentTarget.dataset.id],
              header: {
                Authorization: app.globalData.Authorization
              },
              success: (res) => {
                if (res.statusCode === 204) {
                  this.initStaffs()
                }
              },
              complete: () => {
              }
            })
          }
        }
      })
    }
  },
  searchStaff(event) {
        
    wx.showLoading({
      title: '正在加载',
    })
    if(event.detail.value===''||event.detail.value===null){
      this.setData({
        objv: {
          sort: 'verify_status,asc',
      
        }
      })
     
    
    }else{
      this.setData({
        objv:{
          sort: 'verify_status,asc',
          nameKeyword: event.detail.value
        }
      })
       
      
    }
    console.log(this.data.objv)
    wx.request({
      url: app.globalData.appBaseUrlV2 + '/api1/thUser/findSignUpCompanyStaff',
      method: 'GET',
      data: this.data.objv,
      header: {
        Authorization: app.globalData.Authorization
      },
      success: (res) => {
        if (res.data && res.data.content) {
          this.setData({
            staffList: res.data.content
          })
        }
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  changeUserPortrait(event) {
    // event
    wx.chooseImage({
      count: 1,
      success: (res) => {
        console.log(res, 'chooseImage chooseImage')
        if (res.tempFilePaths.length && res.tempFilePaths.length > 0) {
          wx.uploadFile({
            filePath: res.tempFilePaths[0],
            name: 'file',
            url: app.globalData.appBaseUrlV2 + '/api1/thUser/updateAvatar',
            header: {
              Authorization: app.globalData.Authorization
            },
            success: (res) => {
              console.log(res, 'upload avatar')
              if (res.statusCode === 200) {
                //https://safe.c-icc.cn:8035/avatar
                this.setData({
                  headPotraitPic: app.globalData.appBaseUrlV2 + '/avatar/' + JSON.parse(res.data).avatar
                })
              }
            },
            header: {
              Authorization: app.globalData.Authorization
            }
          })
        }
      },
      fail: () => {},
      complete: () => {}
    })
  },
  changeNickName(event) {
    // event 
    wx.showModal({
      title: '输入修改后的姓名',
      editable: true,
      success(res) {
        console.log(res, 'changeNickName changeNickName')
        if (res.confirm) {
          //wx.request
        }
      }
    })
  },
  updatePassword(event) {
    console.log(event, 'event')
    console.log(this.animation, 'heheanimation')
    this.animation.bottom(0).step()
    this.setData({
      topAnimation: this.animation.export()
    })
  },
  cancelUpdatePassword() {
    this.animation.bottom('-100%').step()
    this.setData({
      topAnimation: this.animation.export()
    })
  },
  confirmUpdatePassword() {
    if (!(this.updateNewPassword && this.updateRepeatNewPassword && this.updateOldPassword)) {
      wx.showToast({
        title: '请输入密码',
        icon: 'error'
      })
      return
    }
    if (this.updateNewPassword !== this.updateRepeatNewPassword) {
      wx.showToast({
        title: '密码不一致',
        icon: 'error'
      })
      return
    }
    wx.request({
      url: app.globalData.appBaseUrlV2 + '/api1/thUser/updatePass',
      method: "POST",
      header: {
        Authorization: app.globalData.Authorization
      },
      data: {
        newPass: encrypt(this.updateNewPassword),
        oldPass: encrypt(this.updateOldPassword)
      },
      success: (res) => {
        console.log(res, 'updateNewPassword')
      },
      fail: () => {},
      complete: () => {
        this.animation.bottom('-100%').step()
        this.setData({
          topAnimation: this.animation.export()
        })
      }
    })
  },
  inputOldPassword(event) {
    this.updateOldPassword = event.detail.value
  },
  inputNewPassword(event) {
    this.updateNewPassword = event.detail.value
  },
  inputRepeatNewPassword(event) {
    this.updateRepeatNewPassword = event.detail.value
  },
  relogin(event) {
    app.globalData.appCounty='威海经济技术开发区';
    app.globalData.appBaseUrlAll='https://whjqaqscxt.weihai.cn';
    app.globalData.appBaseUrl='https://whjqaqscxt.weihai.cn';
    app.globalData.appBaseUrlV2='https://whjqaqscxt.weihai.cn';
    app.globalData.appBaseUrlV3='https://whjqaqscxt.weihai.cn';
   
    // app.globalData.appBaseUrlAll='https://whjqaqscxt.weihai.cn';
    // app.globalData.appBaseUrl='https://whjqaqscxt.weihai.cn';
    // app.globalData.appBaseUrlV2='https://whjqaqscxt.weihai.cn';
    // app.globalData.appBaseUrlV3='https://whjqaqscxt.weihai.cn';
   
    wx.navigateTo({
      url: '/pages/loginv2/loginv2',
      success: () => {}
    })
  }
})