import {
  imgServerUrl
} from '../../config/config.js'
import {
  getCenterInfo,
  getResume,
  getMoneyAccountDetail,
  myPoint
} from '../../services/index.js'
import {
  showToast
} from '../../utils/tips.js'
var app = getApp()

Page({
  data: {
    imgServerUrl: imgServerUrl,
    userInfo: {},
    noPhone: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    percent: 0,
    userId: '',
    memberId: '',
    userToken: '',
    isLogin: false, //默认：未登录
    approveStatus: 0,
    hpMember: {},
    userResume: {},
    hpUserResumeId: '',
    visible: false,
    message_title: '薪资查询',
    actions: [{
      name: '稍后再说',
      color: '#327AE1',
    },
    {
      name: '立即认证',
      color: '#327AE1',
      fontWeight: 500
    },

    ],
  },
  onLoad: function (options) {

  },
  onShow: function () {
    //获取用户user_id和member_id,判断是否处于登录态
    var userId = wx.getStorageSync("user_id");
    var userToken = wx.getStorageSync("user_token");
    var memberId = wx.getStorageSync("member_id");
    var userInfo = wx.getStorageSync("userInfo");
    var hpMember = wx.getStorageSync('hpMember');
    var phone = wx.getStorageSync("phone");
    this.setData({
      hpUserResumeId: '',
      userResume: {},
      percent: 0
    })
    var isLogin = false;
    if (userId && memberId) {
      isLogin = true;
      //必传参数放于app全局变量内
      app.globalData.userId = userId;
      app.globalData.userToken = userToken;
      app.globalData.memberId = memberId;
      if (userInfo) {
        app.globalData.userInfo = userInfo;
      }
      this.start();
    }
    this.setData({
      avatar: (app.globalData.userInfo && app.globalData.userInfo.avatarUrl) ? app.globalData.userInfo.avatarUrl : `${imgServerUrl}/images/avatar/man.png`,
      isLogin: isLogin,
      userInfo: userInfo,
      hpMember: hpMember,
      phone: phone
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  start() {
    this.fetchData();
    this.getUserResume();
    // this.getWallt();
    this.getSign();
  },
  // 获取钱包信息
  getWallt() {
    getMoneyAccountDetail({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
    }).then(res => {
      if (res.errcode === 0) {
        this.setData({
          moneyBalance: res.data.MpMemberAccountAPI.moneyBalance.toFixed(2) + '  元'
        })
      }
    })
  },
  // 获取积分信息
  getSign() {
    myPoint({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
    }).then(res => {
      if (res.errcode === 0) {
        this.setData({
          pointsBalance: res.data.mpMemberAccount.pointsBalance + ' 分',
          moneyBalance: res.data.mpMemberAccount.moneyBalance.toFixed(2) + '  元'
        })
      }
    })
  },
  //获取用户简历详情
  getUserResume() {
    let paramsObj = {
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getResume(paramsObj).then(data => {
      if (data.data.userResume) {
        var percent = 0;
        if (data.data.userResume.realName && data.data.userResume.birthYear && data.data.userResume.gender && data.data.userResume.highestQualification) {
          percent = 40 + percent;
        }
        if (data.data.userResume.expectationPosition && data.data.userResume.expectationAddress && data.data.userResume.expectationSalary) {
          percent = 40 + percent;
        }
        if (data.data.userResume.hpResumeWorkExperienceList.length > 0) {
          percent = 20 + percent;
        }
        this.setData({
          hpUserResumeId: data.data.userResume.id,
          userResume: data.data.userResume,
          percent: percent.toString()
          // percent: '25'
        })
      }
    })
  },
  //获取用户认证状态
  fetchData() {
    let paramsObj = {
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getCenterInfo(paramsObj).then(data => {
      let {
        approveStatus
      } = data.data
      this.setData({
        approveStatus, // 认证状态（0：未认证 1：等待认证 2：未通过 3：已认证） ,
      })
    })
  },
  // 去首页
  toPosition() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 简历
  tojianli() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    if (this.data.hpUserResumeId) {
      wx.navigateTo({
        url: '../jianli/index?hpUserResumeId=' + this.data.hpUserResumeId,
      })
    } else {
      wx.navigateTo({
        url: '../user-info/user-info',
      })
    }
  },
  //去认证页面
  toAuth() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    let approveState = this.data.approveStatus
    if (approveState == 0) {
      //身份认证
      wx.navigateTo({
        url: '../identification/index',
      })
    } else if (approveState == 3) {
      showToast("已认证")
      //个人信息页面
      // wx.navigateTo({
      //   url: '../user-form/index',
      // })
    } else if (approveState == 2) {
      //认证不通过
      wx.navigateTo({
        url: '../result/index?type=auth&status=0',
      })
    } else if (approveState == 1) {
      //待审核
      wx.navigateTo({
        url: '../result/index?type=auth&status=2',
      })
    }
  },
  checkStatus() {
    let approveState = this.data.approveStatus
    if (!wx.getStorageSync("user_id") || !wx.getStorageSync("user_token") || !wx.getStorageSync("member_id")) {
      wx.showModal({
        title: '提示',
        content: '你还未登录，请先登录小程序',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            // wx.navigateBack({
            //   delta: 1,
            // })
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    if (approveState == 0) {
      wx.showModal({
        title: '提示',
        content: '你还未认证，请先进行身份认证',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            //身份认证
            wx.navigateTo({
              url: '../identification/index',
            })
          }
        }
      })
      return false
    } else if (approveState == 2) {
      //认证不通过
      wx.navigateTo({
        url: '../result/index?type=auth&status=0',
      })
      return false;
    } else if (approveState == 1) {
      //待审核
      wx.navigateTo({
        url: '../result/index?type=auth&status=2',
      })
      return false
    }
    return true
  },
  apply() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    //我的申请
    wx.navigateTo({
      url: '../apply-list/index',
    })
  },
  collection() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    //我的收藏
    wx.navigateTo({
      url: '../collection-list/index',
    })
  },
  interview() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    //面试邀请
    wx.navigateTo({
      url: '../interview/index',
    })
  },
  setup() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    //设置
    wx.navigateTo({
      url: '../setting/pages/set/index',
    })
  },
  toPayRoll(e) {
    if (!this.data.isLogin) {
      wx.showModal({
        title: '提示',
        content: '你还未登录，请先登录小程序',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            // wx.navigateBack({
            //   delta: 1,
            // })
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    if (this.data.approveStatus === 0) {
      this.setData({
        message_title: e.currentTarget.dataset.title,
        visible: true
      })
      return
    }
    const {
      url,
      title
    } = e.currentTarget.dataset;
    if (wx.getStorageSync('user_id') && wx.getStorageSync('member_id') && this.checkStatus()) {
      wx.navigateTo({
        url: `/pages/check-iphone/index?phone=${this.data.phone}&&title=${title}&&url=${url}`
      })
    }
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  toDetail(e) {
    const {
      url
    } = e.currentTarget.dataset;

    if (wx.getStorageSync('user_id') && wx.getStorageSync('member_id')) {
      wx.navigateTo({
        url,
        fail: () => {
          wx.switchTab({
            url,
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未登录，请先登录小程序',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            // wx.navigateBack({
            //   delta: 1,
            // })
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
    }
  },
  handel(e) {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    const {
      index
    } = e.detail;
    this.setData({
      visible: false
    })
    if (index === 1) {
      this.toAuth();
    }
  }
})