<template>
	<view class="common">
		<view class="add" @click="handleAddUser">
			<icon class="iconfont icon-tianjia"></icon>添加成员
		</view>
		<view class="box" v-if="familayList.lenght!=0">
			<view class="list">
				<uni-swipe-action>
					<uni-swipe-action-item v-for="(item ,index) in familayList" :key="index"
						:right-options="item.options" @click="handleDeldet($event, index)">
						<view class="item">
							<view class="left">
								<image :src="item.head_pic" class="image1" />
								<view class="name">
									<view class="title">
										{{item.nickname}}
									</view>
									<view class="title2">
										<text>{{item.gender==1?'男':'女'}}</text>
										<text>{{item.age}}岁</text>
									</view>
								</view>
							</view>
							<view class="right" @click.stop>
								<view class="blueBtn" @click="editorInfo(item)">编辑</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
		</view>
		<view v-else>
			没有数据了！
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				id: ""
			}
		},
		computed: {
			...mapState(["familayList"])
		},
		onPullDownRefresh() {
			let that = this
			that.handleUserList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000);
		},
		methods: {
			//删除
			handleDeldet(e, ind) {
				let that = this
				that.id = that.familayList[ind].id
				uni.showModal({
					title: '友情提示',
					content: '确定删除该成员吗',
					success: function(res) {
						if (res.confirm) {
							that.$model.getDelUser({
								aud_id: that.id,
							}).then(res => {
								if (res.code != 0) return
								that.$tools.msg("删除成功！");
								that.familayList.splice(ind, 1)
								that.handleUserList()
							})
						} else if (res.cancel) {
							that.$tools.msg("您已取消删除！");
						}
					}
				});
			},
			handleUserList() {
				let that = this
				that.$model.getUserList({
					type: 2
				}).then(res => {
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.$store.commit('changeFamilay', res.data.user_list)

					console.log("删除", that.id, uni.getStorageSync('userid'))
					// 删除的是当前用户
					if (that.id == uni.getStorageSync('userid')) {
						let userid = res.data.user_list[0].id
						uni.setStorageSync('userid', userid)
						that.$store.dispatch('getUserInfo', {
							aud_id: userid
						})
						// 厨房秤接口
						that.$store.dispatch("getCountFoodInfo", {
							aud_id: userid,
							time: that.$tools.getDate("start")
						})
						// 全部卡片
						that.$store.dispatch("getCardAllList", {
							aud_id: userid
						})
						// 
						that.$store.dispatch("getResult", {
							aud_id: userid
						})
						that.$store.dispatch("getPublicRecord", {
							aud_id: userid
						})
					}
				}).catch(err => {})
			},
			//编辑
			editorInfo(item) {
				uni.navigateTo({
					url: "/body/my/userInfo?info=" + JSON.stringify(item)
				})
			},
			//添加
			handleAddUser() {
				uni.navigateTo({
					url: "/body/my/userInfo"
				})
			},
		}
	}
</script>

<style scoped="scoped" lang="scss">
	.common {
		padding: 15px;
		background-color: #f7f7f7;
		min-height: calc(100vh - 30px);
	}

	.add {
		width: 100%;
		height: 40px;
		line-height: 40px;
		font-size: 32rpx;
		margin-bottom: 10px;
		color: #fff;
		border-radius: 15px;
		display: flex;
		justify-content: center;
		background: $btncolor;
	}

	.box {
		width: 100%;
		height: auto;
		margin: 15px 0;
		padding-bottom: 40px;
	}

	.list {
		width: 100%;
		font-size: 36rpx;

		.item {
			background: #fff;
			padding: 10px 15px;
			width: calc(100% - 30px);
			border-radius: 10px;
			margin-top: 15px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.left {
			width: 75%;
			display: flex;
			align-items: center;

			.image1 {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
				margin-right: 15px;
				display: block;
			}

			.name {
				width: calc(100% - 70px);

				.title {
					font-size: 34rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					margin-top: 5px;
				}

				.title2 {
					font-size: 32rpx;
					color: #999;
					margin-top: 15px;

					text {
						margin-right: 10px;
					}
				}
			}

		}

		.blueBtn {
			width: auto;
			font-size: 32rpx;
			text-align: center;
		}
	}
</style>