<template>
	<view class="detail">
		<view class="addDiver_card">
			<view class="card_title">
				<view class="tiao"></view>
				<view class="card_title_text">基础信息</view>
			</view>
			<uni-list>
				<uni-list-item>
					<view class="list_header" slot="header">
						<image src="@/static/bt.png" style="width: 12rpx;position: absolute;left: -23rpx;"
							mode="widthFix"></image>
						用户手机号
					</view>
					<view slot="body" class="list_right">
						<input v-model="postData.phone" :disabled="!isAdd" placeholder="请输入用户手机号"
							class="list_right_input" />
					</view>
				</uni-list-item>
				<uni-list-item>
					<view class="list_header" slot="header">
						<image src="@/static/bt.png" style="width: 12rpx;position: absolute;left: -23rpx;"
							mode="widthFix"></image>
						用户姓名
					</view>
					<view slot="body" class="list_right">
						<input v-model="postData.customerName" :disabled="!wantUpdate" placeholder="请输入用户姓名"
							class="list_right_input" />
					</view>
				</uni-list-item>
			</uni-list>
			<view class="card_title">
				<view class="tiao"></view>
				<view class="card_title_text">用户权限信息</view>
			</view>
			<uni-list>
				<uni-list-item
					@tap="jump('/userManagement/yunSite/components/serviceStation',{siteId:postData.siteId}, isAdd)">
					<view class="list_header" slot="header">
						<image src="@/static/bt.png" style="width: 12rpx;position: absolute;left: -23rpx;"
							mode="widthFix"></image>
						油站渠道
					</view>
					<view slot="body" class="list-frame site">
						<view class="tips rel2rpx">{{postData.siteName}}</view>
						<view v-if="isAdd && !postData.siteName" class="tips">选择油站渠道</view>
						<uni-icons v-show="isAdd" color="#999" type="right" size="15"></uni-icons>
					</view>
				</uni-list-item>
				<uni-list-item direction="column"
					@tap="jump('/userManagement/yunSite/components/auth',postData.sysRoleList)">
					<view class="list_header" slot="header">
						<image src="@/static/bt.png" style="width: 12rpx;position: absolute;left: -23rpx;"
							mode="widthFix"></image>
						所属权限
					</view>
					<view slot="body" class="list-frame">
						<view>
							<text class="label" v-for="(item,index) in postData.sysRoleList"
								:key="index">{{item.roleName}}</text>
						</view>
						<view v-if="!postData.sysRoleList.length" class="tips">选择所属权限</view>
						<uni-icons v-show="wantUpdate || !postData.sysRoleList.length" color="#999" type="right"
							size="15"></uni-icons>
					</view>
				</uni-list-item>
			</uni-list>
		</view>
		<view class="button-group">
			<view v-if="!wantUpdate" class="button orange" @tap="update">修改</view>
			<view v-else class="button blue" @tap="submit">保存</view>
			<view class="button special red" @tap="deleteData">删除</view>
		</view>
	</view>
</template>

<script>
	import tool from '@/utils/tool'

	import serve from '@/api/userManagement/yunSite.js'

	export default {
		data() {
			return {
				isAdd: false,
				wantUpdate: false,
				postData: {
					phone: '',
					siteName: '',
					customerName: '',
					sysRoleList: [],
				},
			}
		},
		onLoad(options) {
			if (options.item) {
				this.postData = JSON.parse(decodeURI(options.item))
				if (!this.postData.sysRoleList) {
					this.$set(this.postData, 'sysRoleList', [])
				}
			} else this.isAdd = this.wantUpdate = true

			uni.$on('yunSiteServiceStation', (item) => {
				this.postData.channelId = item.id
				this.postData.siteName = item.otherSiteName
			})

			uni.$on('yunSiteAuth', (item) => {
				if (!item.isChecked) {
					let index = this.postData.sysRoleList.findIndex(secItem => secItem.id == item.id)
					if (index !== -1) {
						this.postData.sysRoleList.splice(index, 1)
					}
					return
				}
				this.postData.sysRoleList.push(item)
			})
		},
		methods: {
			deleteData() {
				uni.showModal({
					title: '确定删除吗?',
					success: res => {
						if (res.confirm) {
							serve.removeCustomerRelationSite(this.postData).then(res => {
								if (res.code === 20000) {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 1500)
								}
							})
						}
					}
				})
			},
			submit() {
				let needCheckMap = {
					customerName: {
						tacitly: '',
						WrongText: '请输入用户姓名'
					},
					phone: {
						custom: /^[1][3,4,5,7,8,9][0-9]{9}$/,
						WrongText: '请输入正确手机号码'
					},
					siteName: {
						tacitly: '',
						WrongText: '请选择油站渠道'
					},
					sysRoleList: {
						minLength: 1,
						WrongText: '请选择所属权限'
					}

				}
				let check = tool.checkFn(this.postData, [], needCheckMap)
				if (!check.result) {
					uni.showToast({
						title: check.WrongText,
						icon: 'none'
					})
					return
				}
				let ids = this.postData.sysRoleList.reduce((pre, cur) => {
					pre.push(cur.id)
					return pre
				}, [])
				this.postData.roleIds = ids
				this.judgeQuery().then(res => {
					if (res.code === 20000) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				})
			},
			judgeQuery() {
				if (this.isAdd) return serve.saveCustomerRelationSite(this.postData)
				else return serve.updateCustomerRelationSite(this.postData)
			},
			update() {
				this.wantUpdate = true
			},

			jump(path, item, banJump = true) {
				if (!banJump) return
				if (!this.wantUpdate) return
				let url = item ? `${path}?item=${encodeURI(JSON.stringify(item))}` : path
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding: 0 40rpx;

		.addDiver_card {

			background: #FFFFFF;
			box-shadow: 0px 3px 9px 0px rgba(88, 88, 88, 0.2);
			border-radius: 20rpx;
			margin-top: 38rpx;
			overflow: hidden;
			padding: 20rpx;
			box-sizing: border-box;

			.card_title {
				display: flex;
				// border-bottom: 1px solid #F0F0F0;
				align-items: center;
				padding-bottom: 17rpx;
				height: 86rpx;
			}

			.card_title_text {
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
				margin-left: 10rpx;
			}

			.tiao {
				width: 8rpx;
				height: 34rpx;
				background: #2866FF;
			}

			.list_header {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
				display: flex;
				align-items: center;
				// padding-left: 16px;
				position: relative;
				height: fit-content;
			}


			.list_right {
				flex: 1;
				display: flex;
				justify-content: flex-end;
			}

			.list_right_input {
				width: 100%;
				color: #333333;
				text-align: end !important;
				font-size: 28rpx;
				text-align: right !important;
				padding-left: 90rpx;
				box-sizing: border-box;
			}

			.list-frame {
				margin-top: 25rpx;
				position: relative;

				&.site {
					margin: 0;
					flex: 1
				}

				uni-icons {
					position: absolute;
					top: 50%;
					transform: translateY(-51%);
					right: -30rpx;
				}

				.tips {
					font-size: 24rpx;
					color: #999;
					text-align: right;

					&.rel2rpx {
						position: relative;
						top: 5rpx
					}
				}
			}

			.single-label {
				color: #999;
				font-size: 24rpx;
			}

			.label {
				margin: 3rpx 1rpx;
				display: inline-block;
				padding: 10rpx 15rpx;
				color: #999;
				font-size: 24rpx;
				border: 1px solid #F0F0F0;
				border-radius: 5rpx;
			}
		}

		.button-group {
			position: absolute;
			left: 50%;
			bottom: 70rpx;
			transform: translateX(-50%);
		}

		.button {
			width: 670rpx;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			color: #fff;
			border-radius: 10rpx;

			&.special {
				margin-top: 35rpx;
			}

			&.orange {
				background: #FF8C00;
			}

			&.red {
				background: #EA0000;
			}

			&.blue {
				background: #2866FF;
			}
		}
	}
</style>