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

		<div class="mui-content">
			<div class="mui-content-padded vongi-qingjiadt">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>小区名称</label>
						<span v-text="subForm.companyName"></span>
					</div>
					<div class="mui-input-row">
						<label>房号</label>
						<span v-text="subForm.structureName"></span>
					</div>
				</form>
			</div>
			<div class="mui-content-padded vongi-qingjiadt vongi-editme marginbo">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label style="width:40%">租客姓名<i class="colorfe616c">*</i></label>
						<input type="text" style="width: 60%;" v-model="subForm.name" class="mui-input-clear" placeholder="请输入租客姓名">
					</div>
					<div class="mui-input-row">
						<label>手机号码<i class="colorfe616c">*</i></label>
						<input type="number" v-model="subForm.phone" class="mui-input-clear" placeholder="请输入手机号码">
					</div>
					<div class="mui-input-row">
						<label>身份证号</label>
						<input type="text" v-model="subForm.idCard" class="mui-input-clear" placeholder="请输入身份证号">
					</div>
					<div class="mui-input-row">
						<label style="width:50%">人脸识别照片<i class="colorfe616c">*</i><i class="iconfont icon-tishi color4fc5f7" @click="valert"></i></label>
						<button @click="chooseImage('faceImageUrl') " class="mui-btn mui-btn-block mui-navigate-right" type='button'
						 style="width:50%">
							<img v-if="subForm.faceImageUrl" :src="subForm.faceImageUrl" />
							<a v-else>请选择</a>
						</button>
					</div>
				</form>
			</div>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn"><button type="button" @click="submit" class="mui-btn mui-btn-primary ">保 存</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_Family from '@/apis/Master/family'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import isReachBottom from '$project/utils/isReachBottom'
	import TopHeader from '$project/components/TopHeader.vue'
	import {
		checkPhone,
		checkIdCard
	} from '$project/utils'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Cropper from '$project/components/Cropper.vue'
	import * as WxJsApi from '$project/utils/wxJsApi'
	export default {
		name: 'MasterTenantAdd',
		components: {
			Common,
			Loading,
			TopHeader,
			Cropper
		},
		data() {
			return {
				pageTitle: '添加租客账号',

				isLoading: false,

				rightLink: {
					show: true,
					icon: 'mui-icon-trash',
					style: 'color:#FF6666',
					title: ''
				},

				subForm: {
					companyName: '',
					faceImageUrl: '',
					id: this.$route.query.id,
					idCard: '',
					name: '',
					phone: '',
					structureName: '',
					type: '1'
				},

				imgBase64: '',

				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() {
			if (this.person_popedom.companyName) {
				this.subForm.companyName = this.person_popedom.companyName;
			}
			if (this.person_popedom.structureName) {
				this.subForm.structureName = this.person_popedom.structureName;
			}
		},
		methods: {
			//获取详情
			getDetail() {
				this.isLoading = true;
				API_Family.getPersonInfo(this.subForm.id).then(response => {
					this.isLoading = false;

					this.subForm = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//检测
			checkForm() {
				let phoneResult = checkPhone(this.subForm.phone);
				let idCardResult = checkIdCard(this.subForm.idCard);
				if (!this.subForm.name) {
					mui.toast('请输入姓名');
					return false;
				} else if (typeof phoneResult == 'string') {
					mui.toast(phoneResult);
					return false;
				} else if (this.subForm.idCard && typeof idCardResult == 'string') {
					mui.toast(idCardResult);
					return false;
				} else if (!this.subForm.faceImageUrl) {
					mui.toast('请上传人脸识别照片');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.checkForm()) {
					this.isLoading = true;
					API_Family.addPerson(this.subForm).then(response => {
						this.isLoading = false;
						mui.toast(this.actionTitle + '成功');

						this.$router.go(-1);
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//微信选择图片
			chooseImage(field) {
				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(field);
					//this.uploadpic(field);
				}).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(field) {
				this.isLoading = true;
				WxJsApi.uploadPic(this.imgBase64).then(response => {
					this.isLoading = false;

					this.subForm[field] = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//弹窗
			valert() {
				mui.alert("照片将用于人脸识别设备，请上传清晰的免冠登记照或肩部以上近照。", '   ', ['知道了'])
			},
			del() {
				this.isLoading = true;
				API_Family.delPerson(this.subForm.id).then(response => {
					this.isLoading = false;

					mui.toast('删除成功');
					this.$router.go(-1);
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//右上角点击事件
			doRightLink() {
				var _this = this;
				var btnArray = ['否', '是'];
				mui.confirm('确认删除此家庭成员？删除后系统将清除该家庭成员的帐号信息，该名家庭成员将无法以此身份登录本系统。', '确认删除', btnArray, function(e) {
					if (e.index == 1) {
						_this.del();
					}
				})
			},
			asynCallBack() {

			},
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
			if (this.subForm.id) {
				//获取详情
				this.getDetail();
			} else {
				this.rightLink.show = false;
			}
		},
		destroyed() {

		},
		computed: {
			actionTitle: {
				// getter
				get: function() {
					return this.subForm.id ? '修改' : '添加';
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			...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>
