import SignaturePad from '../../../../utils/signature_pad';
import * as UTIL from '../../../../utils/util.js';
import * as API from '../../../../utils/API.js';
let signaturePad = {};
let pix = 7;
let penColor = 'black';
let lineWidth = 0.6;
Page({
    data: {
        penColor: 'black',
        lineWidth: 0.6,
        isEmpty: true,
        baseItem:"",
        item:{},
        url:"",
        isShow:false
    },
    onLoad: function(options) {
        this.getUserInfo();
        this.getBaseUrl()
    },
      /* 获取用户信息*/
      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) {
                this.setData({url:res.rows[0].configValue+this.data.item.electronicSignature})
              }
            }
          })  
    },
    uploadScaleStart(e) {
        const item = {
            penColor: penColor,
            lineWidth: lineWidth
        };
        signaturePad._handleTouchStart(e, item);
    },
    back:function(){
      wx.navigateBack({
        delta: 1
      })
    },
    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) {
            return false
        }
        wx.canvasToTempFilePath({
            canvasId: 'handWriting',
            success: function(res) {
                wx.uploadFile({
                          url: API.URL_POST_UPLOADFILE, //服务器地址               
                          filePath:res.tempFilePath,//本地照片地址                
                          name: 'file',
                            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"
                  })
              }
            }
          })  
    }
})
