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

		<div class="mui-content fyy-upphoto margin60">
			<div class="mui-content-padded">
				<h5>请输入您的信息</h5>
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>姓名</label>
						<input type="text" v-model="subForm.name" class="mui-input-clear" placeholder="输入姓名">
					</div>
					<div class="mui-input-row">
						<label>手机号码</label>
						<input type="number" v-model="subForm.phone" class="mui-input-clear" placeholder="输入手机号码">
					</div>
				</form>
			</div>
			<div class="mui-content-padded">
				<div class="mui-content-padded faceaifyy-content-text">
					<h5>请上传照片</h5>
					<div class="mui-media-body">
						照片将用于人脸识别设备，请上传清晰的免冠登记照或肩部以上近照。
					</div>
				</div>
				<div class="mui-content-padded faceaifyy-content" @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="button" class="mui-btn mui-btn-primary" @click="submit">下一步</button></div>
		</div>

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

	</div>
</template>

<script>
	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 * as WxJsApi from '$project/utils/wxJsApi'
	import * as types from '$project/store/mutation-types'
	import Cropper from '$project/components/Cropper.vue'
	export default {
		name: 'MasterCompanyAddPerson',
		components: {
			Common,
			Loading,
			TopHeader,
			Cropper
		},
		data() {
			return {
				pageTitle: '添加人员',

				isLoading: false,

				subForm: {
					name: '',
					phone: '',
					faceImageUrl: '',
					bindDevices: '',
				},

				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() {},
		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.name) {
					mui.toast('请输入姓名');
					return false;
				} else if (!this.subForm.phone) {
					mui.toast('请输入电话号码');
					return false;
				} else if (!this.subForm.faceImageUrl) {
					mui.toast('请上传人脸照片');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.checkFrom()) {
					this.set_add_person_data(this.subForm);
					this.$router.push({
						name: 'MasterCompanySelectDevice',
						query: {}
					})
				}
			},
			asynCallBack() {

			},
			...mapMutations({
				set_add_person_data: types.SET_ADD_PERSON_DATA,
			})
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
		},
		destroyed() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				add_person_data: 'add_person_data'
			})
		}
	}
</script>

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