<template>
	<div>
		<common @asynCallBack="asynCallBack" :projectCheck="false"></common>
		<top-header :pageTitle="pageTitle"></top-header>

		<div class="mui-content">
			<div class="mui-content-padded">
				<div class="mui-content-padded">
					<h5>请上传照片</h5>
					<div class="mui-h5 color999">
						照片将用于人脸识别设备，请上传清晰的免冠登记照或肩部以上近照。
					</div>
				</div>
				<div class="mui-content-padded vongi-photo" @click="chooseImage">
					<div class="faceaifyy-content-region" :style="subForm.faceImageUrl?'background:none':''">
						<a v-if="!subForm.faceImageUrl"><span class="mui-icon iconfont icon-weibiaoti1"></span></a>
						<img v-if="subForm.faceImageUrl" :src="subForm.faceImageUrl" width="100%" />
					</div>
				</div>
			</div>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn"><button type="submit" class="mui-btn mui-btn-primary" @click="submit">提交</button></div>
		</div>

		<loading :visible="isLoading"></loading>

		<!--图片裁剪-->
		<cropper :cwidth="cropperWidth" :cheight="cropperHeight" :visible="cropperVisible" :field="cropperField" :cropper="cropper"
		 @cropperFinish="cropperFinish" @cropperCancel="cropperCancel"></cropper>
	</div>
</template>

<script>
	import * as API_Person from '@/apis/person'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Cropper from '$project/components/Cropper.vue'
	import * as WxJsApi from '$project/utils/wxJsApi'
	export default {
		name: 'UserUploadPhoto',
		components: {
			Common,
			Loading,
			TopHeader,
			Cropper
		},
		data() {
			return {
				pageTitle: '上传登记照',

				isLoading: false,

				subForm: {
					personId: this.$route.query.personId,
					openId: '',
					faceImageUrl: ''
				},


				cropperVisible: '',
				cropperField: '',
				cropperWidth: 250,
				cropperHeight: 350,
				cropper: {
					img: '',
					info: true,
					size: 1,
					outputType: 'jpeg',
					canScale: false,
					autoCrop: false,
					// 只有自动截图开启 宽度高度才生效
					autoCropWidth: 250,
					autoCropHeight: 350,
					fixed: true,
					// 真实的输出宽高
					infoTrue: true,
					fixedNumber: [5, 7]
				},
			}
		},
		created() {
			this.subForm.openId = this.openId;
		},
		methods: {
			//微信选择图片
			chooseImage() {
				WxJsApi.chooseImage().then(res => {
					var localData = res.localData;

					if (localData.indexOf('data:image') != 0) {
						//判断是否有这样的头部
						localData = 'data:image/jpeg;base64,' + localData
					}
					localData = localData.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg')
					this.imgBase64 = localData;
					//显示裁剪图片
					this.showCropper('faceImageUrl');
					//this.uploadpic();
				}).catch(error => {
					mui.toast(error);
				})
			},
			//显示裁剪图片
			showCropper(field) {
				this.cropper.img = this.imgBase64;
				this.cropperField = field;
				this.cropperVisible = true;
			},
			//裁剪图片
			cropperFinish(obj) {
				console.log(obj);
				this.imgBase64 = obj.data;
				this.uploadpic(obj.field);
				this.cropperVisible = false;
			},
			//隐藏裁剪图片
			cropperCancel() {
				this.cropperVisible = false;
			},
			//上传图片
			uploadpic() {
				this.isLoading = true;
				WxJsApi.uploadPic(this.imgBase64).then(response => {
					this.isLoading = false;

					this.subForm.faceImageUrl = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//表单检测
			checkFrom() {
				if (!this.subForm.personId) {
					mui.toast('用户id未定义');
					return false;
				} else if (!this.subForm.openId) {
					mui.toast('openId未获取到');
					return false;
				} else if (!this.subForm.faceImageUrl) {
					mui.toast('请上传人脸照片');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.checkFrom()) {
					this.isLoading = true;
					API_Person.save(this.subForm).then(response => {
						this.isLoading = false;

						//跳转到项目主页上去
						window.location.href = '../' + this.$route.query.project + '/#/master';

					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			asynCallBack() {
				//如果已经有头像了则跳过这里的注册上传头像
				if (this.person_data && this.person_data.faceImageUrl) {
					//跳转到项目主页上去
					window.location.href = '../' + this.$route.query.project + '/#/master';
				}
			},
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
		},
		destroyed() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
			})
		}
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style>
</style>
