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

		<div class="mui-content margin60">
			<div class="mui-content-padded">
				<h5>长者信息</h5>
				<ul class="mui-table-view openfyy-list">
					<li class="mui-table-view-cell vongi-wordcard">
						<div class="mui-media-object mui-pull-left "><img :src="oldDetail.faceImagUrl" width="50" /></div>
						<div class="mui-media-body">
							<h3 v-text="oldDetail.personName"></h3>
							<p class='mui-ellipsis mui-h4' v-text="oldDetail.address"></p>
						</div>
					</li>
				</ul>
			</div>

			<div class="mui-content-padded vongi-qingjiadt">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>姓名</label>
						<span v-text="old_relation_form_data?old_relation_form_data.name:''"></span>
					</div>
					<div class="mui-input-row">
						<label>手机号码</label>
						<span v-text="old_relation_form_data?old_relation_form_data.telephone:''"></span>
					</div>
				</form>
			</div>
			<div class="mui-content-padded vongi-qingjiadt vongi-editme">
				<form class="mui-input-group">
					<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>联系地址</label>
						<input v-model="subForm.address" type="text" class="mui-input-clear" placeholder="请填写联系人居住实际地址">
					</div>
				</form>
			</div>
			<div class="mui-content-padded">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label style="width:40%">我是长者的<i class="colorfe616c">*</i></label>
						<button @click="selectRelation" class="mui-btn mui-btn-block mui-navigate-right mui-ellipsis" type='button' style="width:60%">
							{{subForm.relationName}}
						</button>
					</div>
				</form>
			</div>
			<div class="mui-content-padded vongi-qingjiadt vongi-editme">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label style="width:70%">上传我的人脸识别照片</label>
						<button @click="chooseImage" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:30%">
							<img v-if="subForm.faceImageUrl" :src="subForm.faceImageUrl" />
							<font v-else>请选择</font>
						</button>
					</div>
				</form>
			</div>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn"><button @click="submit" type="submit" class="mui-btn mui-btn-pink ">保 存</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>
	require('$project/assets/js/mui.picker.min.js');
	import * as API_User from '@/apis/Master/user'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import NavMenu from '@/components/NavMenu.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Cropper from '$project/components/Cropper.vue'
	import * as types from '$project/store/mutation-types'
	import * as WxJsApi from '$project/utils/wxJsApi'
	export default {
		name: 'MasterUserRelationPerson',
		components: {
			Common,
			Loading,
			TopHeader,
			NavMenu,
			Cropper
		},
		data() {
			return {
				isLoading: false,

				pageTitle: '关联长者',

				subForm: {
					olderPopedomId: this.$route.query.id,
					name: '',
					telephone: '',
					relationName: '',
					faceImageUrl: '',
					openId: '',
					personId: '',
					idCard: '',
					address: ''
				},
				oldDetail: {},

				relationArr: [
					'子女', '配偶', '父母', '其他亲属'
				],

				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.subForm.olderPopedomId) {
				mui.toast('数据错误');
				window.location.href = '../home/#/';
			} else if (!this.old_relation_form_data) {
				mui.toast('数据不全');
				this.$router.push({
					name: 'MasterUserRelationPersonRegister',
					query: {
						id: this.subForm.olderPopedomId
					}
				})
			} else {
				this.subForm = Object.assign(this.subForm, this.old_relation_form_data);
			}

			if (this.person_data) {
				this.subForm.personId = this.person_data.id;
				this.subForm.idCard = this.person_data.idCard;
				this.subForm.faceImageUrl = this.person_data.faceImageUrl;
			}

			this.subForm.openId = this.openId;

			this.subForm.relationName = this.relationArr[0];
		},
		methods: {
			//获取长者信息
			getOlderDetail() {
				this.isLoading = true;
				API_User.getOlderDetail(this.subForm).then(response => {

					this.oldDetail = response;

					this.isLoading = false;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//选择亲属关系
			selectRelation() {
				var roleList = [];
				for (var i = 0; i < this.relationArr.length; i++) {
					roleList.push({
						value: i,
						text: this.relationArr[i],
					})
				}
				var _this = this;
				var picker = new mui.PopPicker();
				picker.setData(roleList);
				picker.show(function(selectItems) {
					_this.subForm.relationName = selectItems[0].text;
				})
			},
			//微信选择图片
			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.openId) {
					mui.toast('openId未获取到');
					return false;
				} else if (!this.subForm.relationName) {
					mui.toast('请选择亲属关系');
					return false;
				} else if (!this.subForm.faceImageUrl) {
					mui.toast('请上传人脸照片');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.checkFrom()) {
					this.isLoading = true;
					API_User.relationOlder(this.subForm).then(response => {
						this.isLoading = false;

						this.$router.push({
							name: 'Master'
						})

					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			asynCallBack() {

			},
			...mapMutations({
				set_old_relation_form_data: types.SET_OLD_RELATION_FORM_DATA,
			})
		},
		mounted() {
			//获取长者信息
			this.getOlderDetail();
			//获取微信配置
			WxJsApi.getWxConfig();
		},
		destroyed() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				old_relation_form_data: 'old_relation_form_data',
			})
		}
	}
</script>
<style scoped src="$project/assets/css/pension.css"></style>
<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style src="$project/assets/css/mui.picker.min.css"></style>
<style>
</style>
