<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" @click="handleUpImg"></icon>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">姓名</view>
				<view class="right">
					<input name="name" type="text" v-model="memInfo.nickname" placeholder="请输入姓名" />
					<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">性别</view>
				<view class="right">
					<picker mode="selector" :range="sexItem" @change="onsexArr">
						<view class="uni-input">{{memInfo.gender==0?'请选择':memInfo.gender==1?'男':'女'}}</view>
						<icon class="iconfont icon-arrow-down-bold"></icon>
					</picker>
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">身高</view>
				<view class="right">
					<input name="name" class="mr-5" type="digit" v-model="memInfo.height" placeholder="请输入身高" />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">体重</view>
				<view class="right">
					<input name="name" type="digit" class="mr-5" v-model="memInfo.weight" placeholder="请输入体重" />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">出生日期</view>
				<view class="right">
					<picker mode="date" :end="endDate" @change="maskClick"
						:value="memInfo.birthday?memInfo.birthday:endDate" fields="time">
						<view class="uni-input">{{memInfo.birthday?memInfo.birthday:'请选择'}}</view>
						<icon class="iconfont icon-arrow-down-bold"></icon>
					</picker>
					<!-- <uni-datetime-picker type="date" :end="startDate" :clear-icon="false" v-model="memInfo.birthday"
						@change="maskClick" :border="false" /> -->
				</view>
			</view>
			<view class="lan border-bottom">
				<view class="left">所在年级</view>
				<view class="right">
					<picker mode="selector" :range="gradeList" :value="index" range-key="name" @change="changegType">
						<view class="uni-input">{{gradeList[index]?gradeList[index].name:"请选择"}}</view>
						<icon class="iconfont icon-arrow-down-bold"></icon>
					</picker>
				</view>
			</view>
		</view>
		<view class="btn" @click="confirmInfo">提交</view>
	</view>
</template>
<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				isEdit: false,
				sexItem: [
					"男",
					"女"
				],
				memInfo: {
					birthday: "",
					gender: 0,
					nickname: "",
					grade: "",
					height: "",
					weight: ""
				},
				headimg: "",
				index: 0,
				gradeList: []
			};
		},
		computed: {
			...mapState(["user", "familayList"]),

			endDate() {
				return this.$tools.getDate("start")
			},
			startDate() {
				return this.$tools.GetDateStr(-90);
			},
		},
		onLoad(options) {
			let that = this
			this.handleGradeList()
			if (options.info) {
				that.memInfo = JSON.parse(options.info)
				that.isEdit = true
			}
			console.log("1111", this.memInfo)
		},
		methods: {
			handleGradeList() {
				let that = this
				that.$model.getGradeList({}).then(res => {
					console.log("年级列表", res)
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.gradeList = res.data
					if (that.isEdit == true) {
						that.index = res.data.findIndex(ite => ite.id == that.memInfo.grade)
					}
					that.memInfo.grade = res.data[that.index].id
				}).catch(err => {})
			},
			// 提交
			confirmInfo() {
				let that = this
				if (!that.memInfo.nickname) {
					that.$tools.msg("请输入姓名")
					return;
				}
				if (!that.memInfo.gender) {
					that.$tools.msg("请选择性别")
					return;
				}
				if (!that.memInfo.height) {
					that.$tools.msg("请选择身高")
					return;
				}
				if (!that.memInfo.weight) {
					that.$tools.msg("请选择体重")
					return;
				}
				if (!that.memInfo.birthday) {
					that.$tools.msg("请选择出生日期")
					return;
				}
				if (!that.memInfo.grade) {
					that.$tools.msg("请选择所在年级")
					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.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)
					// 修改用户
					if (that.isEdit && that.memInfo.id == uni.getStorageSync('userid')) {
						console.log("修改用户")
						that.$store.dispatch('getUserInfo', {
							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("getCardList", {
							aud_id: id
						})
						uni.redirectTo({
							url: "/pages/cardList/card"
						})
					} else {
						uni.navigateBack({
							delta: 1
						});
					}


				}).catch(err => {})
			},
			//确定年龄
			maskClick(e) {
				this.memInfo.birthday = e.detail.value
			},
			//确定性别
			onsexArr(e) {
				this.memInfo.gender = this.sexItem[e.target.value] == "男" ? 1 : 2
			},
			// 年级
			changegType(e) {
				this.index = e.detail.value
				this.memInfo.grade = this.gradeList[e.target.value].id
			},
			handleUpImg() {
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						uni.showLoading({
							title: '识别中...'
						})
						uni.uploadFile({
							header: {
								'Authorization': "Bearer " + uni.getStorageSync('token'),
								'X-Authorization': "Bearer " + uni.getStorageSync('refreshtoken'),
							},
							url: that.$http.baseUrl + '/api/app/wxopen/uploadimg',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								let attr = JSON.parse(res.data)
								uni.hideLoading()
								if (attr.code == 0) {
									that.headimg = attr.data.url
								} else {
									that.$tools.msg("不支持该图像")
								}
							}
						})
					}
				})
			},
		},

	};
</script>

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

	input {
		border: none;
		background: inherit;
	}

	.headbox {
		height: 170rpx;
		padding-top: 15px;
		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;

		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>