<template>
	<div>
		<common @asynCallBack="asynCallBack" :projectCheck="false"></common>
		<top-header :pageTitle="pageTitle"></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.roleName"></span>
					</div>
					<div v-if="companyStructureName" class="mui-input-row">
						<label>楼栋房号</label>
						<span v-text="companyStructureName"></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>姓名<i class="colorfe616c">*</i></label>
						<input v-model="subForm.name" type="text" class="mui-input-clear" placeholder="请输入姓名">
					</div>
					<div class="mui-input-row">
						<label>手机号码</label>
						<input v-model="subForm.telephone" type="text" class="mui-input-clear" placeholder="请输入手机号码">
					</div>
					<div class="mui-input-row">
						<label>身份证号</label>
						<input v-model="subForm.idCard" type="text" class="mui-input-clear" placeholder="请输入身份证号" style="text-transform: uppercase;">
					</div>
					<div class="mui-input-row">
						<label style="width:50%" @click="alertTips">照片<i class="colorfe616c">*</i><i class="iconfont icon-tishi color4fc5f7"></i></label>
						<button @click="chooseImage" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:50%">
							<img v-if="subForm.faceImageUrl" :src="subForm.faceImageUrl" />
							<font v-else>请选择</font>
						</button>
					</div>
				</form>
			</div>
			<div class="mui-content-padded vongi-qingjiadt vongi-editme marginbo">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>推广者id</label>
						<input v-model="subForm.popularizePersonId" type="text" class="mui-input-clear" placeholder="请输入推广者id">
					</div>
				</form>
			</div>
			<div class="vongi-xieyii mui-radio">
				<input name="radio1" v-model="agreement" type="radio" value="1">
				我已阅读并同意<router-link :to="{name:'RegisterAgreement'}" class="color4fc5f7">《用户使用协议》</router-link>
			</div>
			<div class="vongi-btn vongi-login-btn">
				<button 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 * as types from '$project/store/mutation-types'
	import Cropper from '$project/components/Cropper.vue'
	import * as WxJsApi from '$project/utils/wxJsApi'
	import {
		checkPhone,
	} from '$project/utils'
	export default {
		name: 'RegisterResidenceStep4',
		components: {
			Common,
			Loading,
			TopHeader,
			Cropper
		},
		data() {
			return {
				pageTitle: '注册',

				subForm: {
					sceneId: '',
					sceneName: '',
					personRoleId: '',
					roleName: '',
					name: '',
					telephone: '',
					personId: '',
					companyId: '',
					companyName: '请选择',
					companyStructureId: '',
					//companyStructureName: '请选择',
					//最终选择的数据
					selectCompanyStructureList: [],
					address: '',
					idCard: '',
					faceImageUrl: '',
					popularizePersonId: ''
				},

				isLoading: false,

				roleList: [],

				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]
				},

				agreement: 0,
			}
		},
		created() {
			this.subForm = this.register_form_data;

			if (this.person_data) {
				this.subForm.name = this.subForm.name ? this.subForm.name : this.person_data.name;
				this.subForm.telephone = this.subForm.telephone ? this.subForm.telephone : this.person_data.phone;
				this.subForm.idCard = this.subForm.idCard ? this.subForm.idCard : this.person_data.idCard;
				this.subForm.faceImageUrl = this.subForm.faceImageUrl ? this.subForm.faceImageUrl : this.person_data.faceImageUrl;
			}

			if (this.invitation_code) {
				this.subForm.popularizePersonId = this.invitation_code;
			}
		},
		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() {
				let phoneResult = checkPhone(this.subForm.telephone);
				if (!this.subForm.name) {
					mui.toast('请输入姓名');
					return false;
				} else if (typeof phoneResult == 'string') {
					mui.toast('请输入手机号');
					return false;
				} else if (!this.subForm.faceImageUrl) {
					mui.toast('请上传人脸照片');
					return false;
				} else if (!this.agreement) {
					mui.toast('请同意协议');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.checkFrom()) {
					this.isLoading = true;
					//传递openid
					this.subForm.openId = this.openId;
					API_Person.register(this.subForm).then(response => {
						this.isLoading = false;

						if (this.person_data) {
							//mui.toast('提交成功,等待审核中');
							mui.alert('提交成功,等待审核中', ' ', function() {
								//跳转到项目主页上去
								window.location.href = '#/master';
							});
						} else {
							//mui.toast('提交成功,等待审核中');
							var _this = this;
							mui.alert('提交成功,等待审核中', ' ', function() {
								_this.$router.push({
									name: 'UserLogin',
									project: 'business'
								})
							});
						}

					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//弹出提示
			alertTips() {
				mui.alert('照片将用于人脸识别设备，请上传清晰的免冠登记照或肩部以上近照。', ' ', function() {

				});
			},
			asynCallBack() {

			},
			...mapMutations({
				set_register_form_data: types.SET_REGISTER_FORM_DATA,
			})
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
		},
		destroyed() {

		},
		computed: {
			companyStructureName: {
				// getter
				get: function() {
					let nameArr = [];
					this.subForm.selectCompanyStructureList.forEach(function(item) {
						nameArr.push(item.name);
					})
					return nameArr.join('-');
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				register_form_data: 'register_form_data',
				invitation_code: 'invitation_code'
			})
		}
	}
</script>

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