const app = getApp();
const util = require("../../../util/util.js")
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    ctx: false,
    hidden: true,
    showcamara: false,
    type:'card',
    info:{
      card_code:"",
      name:""
    }
  },
  onLoad: function (options) {
    let info = this.data.info;
    app.getMakerInfo(  res =>{
      Object.assign( info, res )
      this.setData( {info })
    })
  },
  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]
    })
  } ,
  takeBand(){
    this.setData( {showcamara:true, type:'band'})
  },
  identify( err, {url} ){
    if( err !=0 || !url ) {
      util.showMsg("图片上传失败")
      return
    }
    let info = this.data.info
    let method='/maker/addMakerBand';
    let showcamara= false
    util.http( method, {url}, (err,res)=>{
      if( err!= 0 ) return;
      console.log("http",err, res)
      Object.assign( info, res)
      this.setData({info,showcamara})
    })
  },
  chooseImg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'], 
      success: function (res) {
        util.uploadFile( res.tempFilePaths[0], that.identify )
      }
    })
  },
  takePhoto() {
    var that = this
    var ctx = this.data.ctx
    that.setData({ hidden: false })
    ctx.takePhoto({
      quality: 'low',
      success: (res) => {
        var tempImagePath = res.tempImagePath
        util.uploadFile( tempImagePath, that.identify )
      },
      fail: function (res) {
        cosole.log('take error', res)
        that.setData({ hidden: true })
        app.showMsg('拍照失败')
      }
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  }
})