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

		<div class="mui-content">
			<div class="mui-content-padded vongi-editme">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>姓名</label>
						<input name="name" type="text" class="mui-input-clear" v-model="subForm.name">
					</div>
					<div class="mui-input-row">
						<label>手机号码</label>
						<input name="phone" type="number" class="mui-input-clear" v-model="subForm.phone">
					</div>
					<div class="mui-input-row">
						<label>身份证号</label>
						<input name="idCard" type="text" class="mui-input-clear" v-model="subForm.idCard" style="text-transform: uppercase;">
					</div>
					<div class="mui-input-row">
						<label style="width: 40%">人脸识别照片</label>
						<button @click="chooseImage('faceImageUrl') " class="mui-btn mui-btn-block mui-navigate-right" type="button"
						 style="width: 60%">
							<img :src="subForm.faceImageUrl" />
						</button>
					</div>
				</form>
			</div>
			<div class="mui-content-padded vongi-editme">
				<form class="mui-input-group">
					<div class="mui-input-row mui-radio">
						<label>是否接受微信消息推送</label>
						<input name="radio1" type="radio" :checked="subForm.wechatNoticeEnabled" @click="subForm.wechatNoticeEnabled=!subForm.wechatNoticeEnabled">
					</div>
				</form>
			</div>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn">
				<button type="submit" class="mui-btn mui-btn-primary" @click="save">保 存</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_WeiXin from '$project/apis/weixin'
	import * as API_Common from '$project/apis/common'
	import * as API_Master from '@/apis/Master/index'
	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: 'MasterMineEditInfo',
		components: {
			Common,
			Loading,
			TopHeader,
			Cropper
		},
		data() {
			return {
				pageTitle: '修改个人信息',

				isLoading: false,

				subForm: {
					name: '',
					phone: '',
					idCard: '',
					faceImageUrl: '',
					wechatNoticeEnabled: true,
					openId: '',
				},

				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() {
			this.subForm.name = this.person_data.name;
			this.subForm.phone = this.person_data.phone;
			this.subForm.idCard = this.person_data.idCard;
			this.subForm.faceImageUrl = this.person_data.faceImageUrl;
			this.subForm.openId = this.openId;
		},
		methods: {
			//保存
			save() {
				this.isLoading = true;
				API_Master.savePerson(this.subForm).then(response => {
					this.isLoading = false;

					mui.toast('保存成功');
					this.$router.push({
						name: 'Master',
						query: {}
					})
				}).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);
				})
			},
			asynCallBack() {

			},
		},
		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 scoped>
</style>
