const app = getApp();
const util = require("../../../utils/util.js")
Page({
  data: {
    ctx: false,
    hidden: true,
    showcamara: false,
    type:'card',
    phone:'',
    studyUser :{
      cardId:"352202198709031536",
      cardImg:"",
      nickname:"吕君喜",
      phone:'18850724428',
      openid:'',
      identify:0,
      session_key:'',
      cardBackImg:""
    }
  },
  onShow: function () {
    app.getUserInfo( studyUser =>{
      if( !studyUser.token ){
        studyUser = wx.getStorageSync('@identify')||{}
      }
      this.setData({studyUser})
    })
  },
  wxLogin(){
    let that = this
    wx.login({
      success(wxres) {
        if( !wxres.code){
          util.showMsg("登入失败")
        }else{
          that.doLoginByCode( wxres.code )
        }
      }
    })
  },
  doLoginByCode( code ){
    app.formPost( "Auth.WxLoginByCode", {code}).then( (res)=>{
      if( res.code == 200){
        this.setData( {userInfo: res.data})
        app.setUserInfo( res.data )
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  doIdentify( e ){
    let that = this
    wx.login({
      success(wxres) {
        if (wxres.code) {
          that.doRegister( wxres.code, e.detail.value )
        }
      }
    })
  },
  doRegister( code, info ){
    let param = {
      code: code,
      phone:   info.phone,
      cardId:  info.cardId,
      nickname: info.nickname,
    }
    app.formPost( 'Auth.Register', param).then( res =>{
      if( res.code == 200 ){
        let studyUser= res.data
        app.setUserInfo( studyUser )
        util.showSuccess("实名认证成功")
        this.setData({studyUser})
      }else{
        util.showMsg( res.msg  )
      }
    })
  },
  onReady: function (res) {
    var ctx = wx.createCameraContext('myCamera')
    this.setData({ ctx: ctx })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, 
      urls: [current]
    })
  } ,
  takeCard(){
    this.setData( {showcamara:true, type:'card'})
  },
  takeCardBack(){
    this.setData( {showcamara:true, type:'cardBack'})
  },
  identify( err, {url} ){
    if( err != 200 || !url ) {
      util.showMsg("图片上传失败")
      return
    }
    let type = this.data.type;
    util.showBusy("识别图片中")
    let studyUser = this.data.studyUser
    let showcamara= false
    app.formPost('Auth.wxUploadImg', {url, type}).then(res => {
      if( res.code == 200 ){
        util.showBusy("上传成功")
        if ( type =="card"){
          studyUser.cardImg = url
          studyUser.address = res.data.Address
          studyUser.nickname = res.data.Name
          studyUser.cardId = res.data.IDNumber
          studyUser.gender = res.data.Gender
        }else{
          studyUser.cardBackImg = url
        }
        wx.setStorageSync('@identify', studyUser)
        this.setData({studyUser,showcamara})
      }
    })
  },
  chooseImg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'], 
      success: function (res) {
        that.setData( {showcamara:false})
        util.showSuccess("图片上传中")
        util.uploadFile( res.tempFilePaths[0], that.identify )
      },
      fail:(res)=>{
        console.log( res)
        util.showMsg("图片选择失败")
      }
    })
  },
  takePhoto() {
    var that = this
    var ctx = this.data.ctx
    ctx.takePhoto({
      quality: 'low',
      success: (res) => {
        app.message("图片上传中")
        that.setData( {showcamara:false})
        var tempImagePath = res.tempImagePath
        util.uploadFile( tempImagePath, that.identify )
      },
      fail:  (res)=> {
        app.message("图片上传中", "error")
      }
    })
  },
  onConfirm( e ){
    let {phone} = e.detail.value
    let info = this.data.info
    let {card_img, card_back_img} = this.data.info
    if( !card_img ) return util.showMsg( "身份证正面有误");
    if( !card_back_img ) return util.showMsg( "身份证背面有误");
    if( !phone || phone.length != 11 ) return util.showMsg( "手机号码有误");
    util.http( '/maker/identifyMaker', {phone}, (err,res)=>{
      if( err!= 0 ) return;
      info.identify = 1
      app.identifyUser( )
      this.setData({info})
    })

  },
  goBack() {
    this.setData({showcamara:false})
  }

})