<template>
	<view>
		<uni-nav-bar @clickLeft="goBack()" class="nav-bar" height="110rpx" leftWidth="400rpx" leftText="我的电子签名"
			leftIcon="left" border backgroundColor="#2cade8" color="#fff" fixed statusBar shadow></uni-nav-bar>
		<view>
			 <view class="imgs">
				 <image class="img" :src="img1" mode="widthFix" style="margin: 0px 24px;"></image>
			 </view>
			 <view style="display: flex;justify-content: space-around;">
				 <button type="primary" @tap="doss" style="width: 40%;">生成签名</button>
				 <button type="primary" @tap="checks" style="width: 40%;">选择签名</button>
			 </view>
			
			<catSignature canvasId="canvas1" @close="close" @save="save" :visible="isShow" ref="eleSignature"/>
		</view>
	</view>
</template>

<script>
	import catSignature from '@/components/canvas.vue';
	export default {
		components: {
				catSignature
		},
		data() {
			return {
				userInfo:{},
				isShow: false,
				img1:"",
				header:'',
				tempFilePath:''
			}
		},
		mounted() {
			this.header = {Authorization: "Bearer " + this.vuex_token};
			this.getUserInfo();
			console.log(this.vuex_user)
		},
		methods: {
			getUserInfo() {
				let that = this;
				that.$u.api.hrmUserInfo().then(res => {
					that.userInfo = res;
					that.img1 = this.vuex_host+res.signature;
				})
			},
			doss() {
				this.isShow = true;
				// debugger;
				console.log(this.vuex_apifile)
			},
			close() {
				this.isShow = false;
			},
			checks(){
				let that = this;
				uni.chooseImage({
					count:1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						// debugger;
						that.signImage = res.tempFilePath;
						console.log(res);
						that.img1 = res.tempFilePaths[0];
						that.imgUp(res.tempFilePaths[0]);
					}
				});
			},
			imgUp(tempFilePaths){
				// debugger;	
				let that = this;
				uni.uploadFile({
					url: this.vuex_apifile,
					filePath: tempFilePaths,
					name: 'file',
					header: this.header,
					success: resImg => {
						let data = this.toJson && this.$u.test.jsonString(resImg.data) ? JSON.parse(resImg.data) : resImg.data;
						let pData = JSON.parse(data)
						let sform = {
							"signature": pData.path,
							"id_number":that.userInfo.id_number,
							"photo": that.userInfo.photo,
						}
						that.img1 = this.vuex_host+pData.path;
						console.log(sform.signature)
						alert(sform.signature)
						that.$u.api.hrmUpdateInfo(sform).then(res=>{
							debugger;
							console.log('签名更新成功')
						})
					},
					fail: e => {
						debugger;	
					},
					complete: resImg => {
						debugger;	
					}
				});
			},
			save(val,data) {
				let that = this;
				that.img1 = that.$refs.eleSignature.signImage;
				that.isShow = false;
				uni.uploadFile({
					url: this.vuex_apifile,
					filePath: data.file,
					name: 'file',
					header: this.header,
					success: resImg => {
						let data = this.toJson && this.$u.test.jsonString(resImg.data) ? JSON.parse(resImg.data) : resImg.data;
						let pData = JSON.parse(data)
						// console.log("返回数据："+data)
						// console.log("返回数据："+pData)
						// debugger;
						console.log("签名地址："+pData.path)
						
						let sform = {
							"signature": pData.path,
							"id_number":that.userInfo.id_number,
							"photo": that.userInfo.photo,
						}
						sform.signature = pData.path;
						
						that.$u.api.hrmUpdateInfo(sform).then(res=>{
							debugger;
							console.log('签名更新成功')
						})
					},
					fail: resImg => {
						debugger;
						console.log(resImg)
					},
					complete: resImg => {
						debugger;
						console.log(resImg)
					}
				});
			},
			base64toFile(urlString, fileName) {
			  const dataArr = urlString.split(",");
			  const byteString = atob(dataArr[1]);
			  const options = {
			    type: "image/png",
			    endings: "native"
			  };
			  const u8Arr = new Uint8Array(byteString.length);
			  for (let i = 0; i < byteString.length; i++) {
			    u8Arr[i] = byteString.charCodeAt(i);
			  }
			  return new File([u8Arr], fileName + ".png", options);//返回文件流
			},
			dataURLtoFile(dataURI, type) {
			  let binary = atob(dataURI.split(",")[1]);
			  let array = [];
			  for (let i = 0; i < binary.length; i++) {
				array.push(binary.charCodeAt(i));
			  }
			  return new Blob([new Uint8Array(array)], { type: type });
			},
			goBack() {
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style>
>>>.uni-navbar__header,
	>>>.uni-status-bar {
		background-image: linear-gradient(90deg, #164cc3 0%, #2c6fd9 100%), linear-gradient(#e60012, #e60012) !important;
	}
</style>
