// pages/contract/add/add.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
import SignaturePad from '../../../utils/signature_pad';
let signaturePad = {};
let pix = 7;
let penColor = 'black';
let lineWidth = 0.6;

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    id:null,
    autosize:{ maxHeight: 100, minHeight: 50 },
    form:{
      voteResult: '1'
    },
    penColor: 'black',
    lineWidth: 0.6,
    isEmpty: true,
    baseItem:"",
    item:{},
    url:"",
    isShow:false
  },
  back:function(){
    wx.navigateBack({
      delta: 2
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({id:options.id})
    this.getUserInfo();
    this.getBaseUrl()
    //获取收入合同状态
    
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  onChange(event) {
    console.log(event.detail);
    this.setData({
      [event.currentTarget.dataset.value]: event.detail,
    });
  },
  /* 获取用户信息*/
  getUserInfo(){
    UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          this.setData({item:res.user})
          // if(this.data.item.electronicSignature==null||this.data.item.electronicSignature==""){
          //   this.setData({isShow:false})
            var ctx = wx.createCanvasContext('handWriting');
            const data = {
                devicePixelRatio: pix,
            };
            signaturePad = new SignaturePad(ctx, data);
          // }else{
          //     this.setData({isShow:true})
          // }
        }
      }
    })
  },
getBaseUrl:function(){
    let params={
        configKey: "system.attachment.url"}
    UTIL.httpRequest(API.URL_GET_SYSCONFIG,params,{
        success: (res) => {
          if (res.code == API.SUCCESS_CODE) {
            console.log(res.rows[0].configValue+this.data.item.electronicSignature)
            this.setData({url:res.rows[0].configValue+this.data.item.electronicSignature})
          }
        }
      })  
},
uploadScaleStart(e) {
    const item = {
        penColor: penColor,
        lineWidth: lineWidth
    };
    signaturePad._handleTouchStart(e, item);
},
uploadScaleMove(e) {
    signaturePad._handleTouchMove(e);
},
uploadScaleEnd: function(e) {
    signaturePad._handleTouchEnd(e);
    const isEmpty = signaturePad.isEmpty();
    this.setData({
        isEmpty: isEmpty
    })
},
retDraw: function() {
    this.setData({isShow:false})
    var ctx = wx.createCanvasContext('handWriting');
        const data = {
            devicePixelRatio: pix,
        };
        signaturePad = new SignaturePad(ctx, data);
    signaturePad.clear();
    const isEmpty = signaturePad.isEmpty();
    this.setData({
        isEmpty: isEmpty
    })
},
getSysInfo: function() {
    var that = this
    wx.getSystemInfo({
        success: function(res) {
            pix = res.pixelRatio
            that.setData({
                width: res.windowWidth * pix,
                height: res.windowHeight * pix
            })
        }
    })
},
//保存canvas图像
subCanvas: function() {
    let _this = this
    if (this.data.isEmpty) {
      wx.showToast({
        title: '请签字后提交',
        duration: 2000,
        icon:"none"
      })
        return false
    }
    wx.canvasToTempFilePath({
        canvasId: 'handWriting',
        success: function(res) {
          _this.data.form.voteId = _this.data.id;
          _this.data.form.file = res.tempFilePath;
          _this.data.form.voteBy = _this.data.item.userName;
          
          wx.uploadFile({
            url: wx.getStorageSync('dressCode')+API.votedetailAdd, //服务器地址               
            filePath:res.tempFilePath,//本地照片地址                
            name: 'file',
            formData: _this.data.form,
            header: { 
                'Content-Type': 'multipart/form-data',
                'Authorization':'Bearer '+getApp().globalData.userInfo.token,  //如果需要token的话要传
            },
            success (res){
                let dd = JSON.parse(res.data)
              if(dd.code==200){
                  wx.showToast({
                    title: dd.msg,
                    duration: 2000,
                    icon:"success"
                  }) 
                  setTimeout(() => {
                      _this.back()
                  }, 2000);
                }else{
                  wx.showToast({
                    title: dd.msg,
                    duration: 2000,
                    icon:"none"
                  })
                }
            }
          })
      },
      fail: function(res) {
          console.log(res)
      }
    })
},
// 保存电子签名
saveESign : function(item){
    UTIL.httpRequest(API.URL_POST_USEREDIT,item,{
        success: (res) => {
          if (res.code == API.SUCCESS_CODE) {
            wx.showToast({
                title: "保存成功！",
                duration: 2000,
                icon:"success"
              })
          }
        }
      })  
}
})