<template>
	<view class="box">
		<view class="lanBox">
			<view class="headbox">
				<view class="touxiang">
					<image v-if="headimg" :src="headimg" class="headimage" />
					<icon v-else class="iconfont icon-user-filling headimage"></icon>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">{{$t("common.infoNickname")}}</view>
				<view class="right">
					<input name="name" type="text" v-model="memInfo.nickname"
						:placeholder="$t('tips.verifyNickName')" />
					<uni-icons type="clear" color="#999" v-if="memInfo.nickname" @click="memInfo.nickname=''"
						size="20"></uni-icons>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">{{$t("common.infoGender")}}</view>
				<view class="right">
					<view class="radio">
						<uni-icons :type="memInfo.gender==1?'checkbox-filled':'circle'" @click="memInfo.gender=1"
							size="24" :color="memInfo.gender==1?'#fea606':'#dfdfdf'"></uni-icons>
						{{$t("common.infoMan")}}
					</view>
					<view class="radio ml-15">
						<uni-icons :type="memInfo.gender==2?'checkbox-filled':'circle'" @click="memInfo.gender=2"
							size="24" :color="memInfo.gender==2?'#fea606':'#dfdfdf'"></uni-icons>
						{{$t("common.infoWoman")}}
					</view>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">{{$t("common.infoHeight")}}</view>
				<view class="right">
					<input name="name" class="mr-5" type="digit" v-model="memInfo.height"
						:placeholder="$t('tips.verifyHeight')" />CM
					<uni-icons type="clear" color="#999" v-if="memInfo.height" @click="memInfo.height=''"
						size="20"></uni-icons>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">{{$t("common.infoWeight")}}</view>
				<view class="right">
					<input name="name" type="digit" class="mr-5" v-model="memInfo.weight"
						:placeholder="$t('tips.verifyWeight')" />KG
					<uni-icons type="clear" color="#999" v-if="memInfo.weight" @click="memInfo.weight=''"
						size="20"></uni-icons>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">{{$t("common.infoBirthday")}}</view>
				<view class="right">
					<picker mode="date" :end="endDate" @change="maskClick"
						:value="memInfo.birthday?memInfo.birthday:endDate" :fields="fields">
						<view class="uni-input">{{memInfo.birthday?memInfo.birthday:$t("tips.verifyPicker")}}</view>
						<icon class="iconfont icon-arrow-down-bold"></icon>
					</picker>
				</view>
			</view>
		</view>
		<view class="btn" @click="confirmInfo">{{$t("tips.btnConfirm")}}</view>
	</view>
</template>
<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				isEdit: false,
				fields: "",
				sexItem: [
					"男",
					"女"
				],
				memInfo: {
					measure_model: 2,
					birthday: "",
					gender: 1,
					nickname: "",
					grade: "nothing",
					height: "",
					weight: "",
					identity_name: "陌生人",
					identity_id: "P0",
				},
				headimg: "",
				identityIndex: 0,
			};
		},
		computed: {
			...mapState(["user", "familayList", "cityList", "gradeList", ]),
			endDate() {
				return this.$tools.getDate("start")
			},
			startDate() {
				return this.$tools.GetDateStr(-90);
			},
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t('common.titleMember'),
			})
		},
		onLoad(options) {
			let that = this
			if (options.info) {
				let info = JSON.parse(options.info)
				that.memInfo = info
				console.log("that.memInfo ", that.memInfo)
				that.gradeIndex = that.gradeList.findIndex((profile) => profile.id === that.memInfo.grade);
				that.memInfo.grade = that.gradeList[that.gradeIndex].id
				that.isEdit = true
			}
			// #ifdef APP-PLUS 
			that.fields = "time"
			// #endif 
			// #ifndef APP-PLUS 
			that.fields = "day"
			// #endif
		},
		methods: {
			// 提交
			confirmInfo() {
				let that = this
				if (!that.memInfo.nickname) {
					that.$tools.msg(that.$t("tips.verifyNickName"))
					return;
				}
				if (!that.memInfo.height) {
					that.$tools.msg(that.$t("tips.verifyHeight"))
					return;
				}
				if (!that.memInfo.weight) {
					that.$tools.msg(that.$t("tips.verifyWeight"))
					return;
				}
				if (!that.memInfo.birthday) {
					that.$tools.msg(that.$t("tips.verifyBirthday"))
					return;
				}
				let https = that.isEdit ? that.$model.getEditUser(that.memInfo) : that.$model.getAddUser(that.memInfo)
				return https.then(res => {
					console.log("成功", res)
					if (res.code == 0) {
						that.$tools.msg(that.$t("tips.msgSetSuccess"));
						that.handleUserList(res.data.aud_id)
					} else {
						that.$tools.msg(res.msg);
					}
				});
			},
			handleUserList(id) {
				let that = this
				that.$model.getUserList({
					type: 2
				}).then(res => {
					console.log("成员列表", res)
					that.$store.commit('changeFamilay', res.data)
					let pages = getCurrentPages()
					let prevPage = pages[pages.length - 2]
					// 修改用户
					if (that.isEdit && that.memInfo.id == uni.getStorageSync('userid')) {
						console.log("修改用户", that.memInfo.id, uni.getStorageSync('userid'))
						that.$store.dispatch('getUserInfo', {
							aud_id: uni.getStorageSync('userid')
						})
						that.$store.dispatch("getResult", {
							aud_id: uni.getStorageSync('userid')
						})
					}
					// 添加用户
					if (!that.isEdit) {
						console.log("添加用户")
						uni.setStorageSync('userid', id)
						uni.setStorageSync('gender', that.memInfo.gender)
						that.$store.dispatch('getUserInfo', {
							aud_id: id
						})
						that.$store.dispatch("getResult", {
							aud_id: id
						})
					}
					if (!that.isEdit && that.familayList.length == 1) {
						uni.setStorageSync('isBle', true)
						uni.switchTab({
							url: "/pages/home/home"
						})
					} else {
						uni.navigateBack()
					}

				}).catch(err => {})
			},
			//确定年龄
			maskClick(e) {
				console.log("出生日期", e.detail.value)
				this.memInfo.birthday = e.detail.value
			},
			//确定性别
			onsexArr(e) {
				this.memInfo.gender = this.sexItem[e.target.value] == that.$t("common.infoMan") ? 1 : 2
			},
		},

	};
</script>

<style scoped="scoped" lang="scss">
	.box {
		height: 100vh;
		background-color: #fff;
	}

	input {
		border: none;
		background: inherit;
	}

	.headbox {
		height: 180rpx;
		padding-top: 10px;
		border-radius: 0 0 5px 5px;
		background: $maincolor;
	}

	.headimage {
		display: block;
		padding-top: 10px;
		width: 70px;
		height: 70px;
		border-radius: 50%;
		font-size: 140rpx;
		margin: auto;
		color: #fff;
	}

	.lan {
		display: flex;
		align-items: center;
		font-size: 32rpx;
		padding: 5px 0;
		margin: 5px 15px;
		border-bottom: 1px solid #f7f7f7;
	}

	.left {
		width: 24%;
		text-align: left;
	}

	.right {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		width: 72%;
		height: 40px;
		line-height: 40px;
		box-sizing: border-box;
		position: relative;
		text-align: right;

		.radio {
			display: flex;
			align-items: center;
		}

		picker {
			width: 100%;
			text-align: right;
			border: none;
			margin-right: 15px;
			font-size: 32rpx;
			color: #333333;
		}

		/deep/input {
			height: 35px;
			padding-top: 1px;
			font-size: 32rpx;
			color: #333333;
			margin-right: 15px;
		}

		.iconfont {
			color: #333333;
			font-size: 32rpx;
			position: absolute;
			right: -10px;
			top: 0;
		}
	}

	.btn {
		width: auto;
		margin: 40px 15px 0;
		background: $btncolor !important;
	}
</style>