<template>
	<view>
		<uni-nav-bar fixed :border="false" left-icon="left" :title="title" :statusBar="true" @clickLeft="back" @clickRight="handleDel">
			<!-- #ifndef MP-WEIXIN -->
			<template v-slot:right v-if="type == 'edit'">
				<view class="nav-bar-del">删除</view>
			</template>
			<!-- #endif -->
		</uni-nav-bar>
		<view class="container">
			<uni-forms ref="form" :rules="rules" :model="form" label-width="0" err-show-type="toast">
				<view class="card-list">
					<uni-forms-item name="name" class="card-list-item border-line nameinput">
						<uni-easyinput v-model="form.name" :trim="true" :inputBorder="false" :primaryColor="'#c0c4cc'"
							placeholder="请输入姓名" :placeholderStyle="placeholderStyle" :styles="inpuStyle" maxlength="10" />
					</uni-forms-item>
					<uni-forms-item name="relationship" class="card-list-item">
						<picker @change="bindPickerChange" :value="index" :range="array">
							<view class="pickerbox">
								<view class="pickerbox-left">关系</view>
								<view class="pickerbox-right">
									<text v-show="!array[index]">请选择关系</text>
									<text class="pickerbox-val" v-show="array[index]">{{array[index]}}</text>
									<image class="arrow_right_icon" src="@/static/images/pages_watch/icons/my_right_arrow_right_icon.png"></image>
								</view>
							</view>
						</picker>
					</uni-forms-item>
				</view>
			</uni-forms>
			<button class="confirm-btn" :loading="btnLoading" :disabled="disabled" :style="{opacity: disabled ? 0.5 : 1}"
				@click="submit">确定</button>
			<!-- #ifndef APP-PLUS || H5 -->
			<view class="footer-del" v-if="type == 'edit'" @click="handleDel">删除</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import { editMyfamily } from "@/api/pages_watch/user.js"
	export default {
		data() {
			return {
				btnLoading: false,
				type: "add",
				userList: [],
				title: '',
				relation: {
					"baba": "爸爸",
					"mama": "妈妈",
				},
				// 关系
				array: ['父亲', '母亲', '配偶', '儿子', '女儿', '其他'],
				index: undefined,
				// 修改家人的index
				userIdx: 0,
				form: {
					name: "",
					relationship: ""
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
				},
				placeholderStyle: "{font-family: PingFang SC, PingFang SC;font-weight: 500;font-size: 32rpx;color: #CCCCCC;}",
				inpuStyle: {
					fontFamily: "PingFang SC, PingFang SC",
					fontWeight: 500,
					fontSize: "32rpx",
					color: "#333"
				}
			}
		},
		computed: {
			disabled() {
				return !(this.form.name && this.array[this.index])
			}
		},
		onLoad(option) {
			this.title = option.type == "edit" ? "修改称呼" : "新增家人"
			if (this.$isLogin()) {
				let userInfo = JSON.parse(uni.getStorageSync('userWatchInfo'))
				this.userList = JSON.parse(userInfo.otherDevice) || []
				this.type = option.type || 'add'
				if(this.type == 'edit'&& option.index) {
					this.userIdx = option.index || 0
					this.form.name = this.userList[option.index].name
					this.form.relationship = this.userList[option.index].relationship
					this.index = this.array.indexOf(this.form.relationship) == -1 ? undefined : this.array.indexOf(this.form.relationship) 
				} 
			}
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 删除
			handleDel() {
				if(this.type == 'edit' ) {
					this.userList.splice(this.userIdx,1)
					this.handleEdit(JSON.stringify(this.userList))
				}
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
			},
			submit() {
				this.form.relationship = this.array[this.index]
				this.$refs["form"].validate().then(res => {
					this.btnLoading = true
					if(this.type == "add") {
						this.userList.push({
							name: this.form.name,
							relationship: this.form.relationship,
							deviceId: ""
						})
					} else if(this.type == "edit"){
						this.userList[this.userIdx].name = this.form.name
						this.userList[this.userIdx].relationship = this.form.relationship
					}
					this.handleEdit(JSON.stringify(this.userList))
				}).catch(err => {
					console.log('err', err);
				})
			},
			handleEdit(otherDevice) {
				editMyfamily({otherDevice: otherDevice}).then(res=>{
					this.btnLoading = false
					if(res.code == 200) {
						uni.navigateBack()
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				}).catch(err => {
					this.btnLoading = false
					console.log('err', err);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}

	::v-deep .uni-easyinput__content-input {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
	}
	.footer-del {
		width: 702rpx;
		height: 98rpx;
		margin-top: 40rpx;
		background: #fff;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
		line-height: 98rpx;
		text-align: center;
	}
	.nav-bar-del {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #222222;
		width: 68rpx;
	}

	.pickerbox {
		@include u-flex(row, center, space-between);
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;

		&-left {
			flex: 1;
		}

		&-right {
			flex-shrink: 0;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #999999;
			@include u-flex(row, center, flex-end);
		}
		&-val {
			color: #333333;
		}
		.arrow_right_icon {
			width: 48rpx;
			height: 48rpx;
		}
	}

	.container {
		padding: 24rpx;

		.card-list {
			padding: 0 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
		}

		.card-list-item {
			height: 120rpx;
			margin-bottom: 0;
			@include u-flex(row, center, flex-start);
		}

		.nameinput {}

		.confirm-btn {
			width: 702rpx;
			height: 98rpx;
			margin-top: 40rpx;
			background: #FF7700;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 98rpx;

			&::after {
				border: none;
			}
		}
	}
</style>