<template>
	<view class="container qualifications" :style="{ paddingBottom: isIphoneX ? '68rpx' : '0' }">
		<tui-skeleton
			v-if="skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		></tui-skeleton>
		<view class="remarks-content" v-else>
			<template v-if="handleType === '1'">
				<view class="list-view-title">
					<view class="list-view-h1"
						>机构名称:<text class="none">{{ reportInfo.name }}</text></view
					>
				</view>
				<view class="list-view-title">
					<view class="list-view-h1"
						>联系人:<text class="none">{{ reportInfo.linkMan }}</text></view
					>
				</view>
			</template>
			<template v-else>
				<view class="list-view-title">
					<view class="list-view-h1"
						>咨询人：<text class="none">{{ reportInfo.questionMan }}</text></view
					>
				</view>
			</template>
			<view class="list-view-title"> <view class="list-view-h1">报备商品：</view> </view>
			<view class="tui-remarks-content">
				<view class="tui-remarks-goods-input" v-if="handleGoods.length === 0" @click="handleShowGoodPopup">
					<view class="input-add"> <text class="iconfont icon-jiahao"></text> </view>
					<view class="input-text"> 点击添加客户咨询的商品 </view>
				</view>
				<view class="tui-remarks-showgoods" v-else>
					<view class="tui-remarks-goods" v-for="(pros, index) in handleGoods" :key="index">
						<view class="goods-image"> <image :src="pros.image" mode=""></image> </view>
						<view class="goods-main">
							<view class="name"> {{ pros.name }} </view>
							<view class="shop"> 供应商：{{ pros.shopName }} </view>
						</view>
					</view>
					<view class="tui-remarks-btn">
						<view class="btn delete" @click="handleClean">删除</view>
						<view class="btn update" @click="handleShowGoodPopup">更换</view>
					</view>
				</view>
			</view>
			<view class="list-view-title">
				<view class="list-view-h1"><text>*</text>报备事由：</view>
			</view>
			<view class="remarks-textarea">
				<textarea
					class="textarea"
					v-if="handleType==='1'"
					v-model="reportClubParams.reportText"
					value=""
					placeholder="请输入其他需要说明的内容"
					maxlength="200"
					@input="conInput"
				/>
				<textarea
					class="textarea"
					v-if="handleType==='2'"
					v-model="reportParams.reportText"
					value=""
					placeholder="请输入其他需要说明的内容"
					maxlength="200"
					@input="conInput"
				/>
				<text class="limit-text">{{ min }}/{{ max }}</text>
			</view>
		</view>
		<view class="remarks-btn" :style="{ paddingBottom: isIphoneX ? '68rpx' : '34rpx' }">
			<view class="edit-button" @click="editButtonConfim">确定</view>
		</view>
		<!-- 商品列表弹窗 -->
		<cm-goodspopup
			ref="cmgoodspopup"
			v-if="isGoodspopup"
			:show="isGoodspopup"
			@handleChoiceaGoods="handleChoiceaGoodsData"
		>
		</cm-goodspopup>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import authorize from '@/common/config/authorize.js'
import cmGoodspopup from '@/components/cm-module/cm-seller/cm-goods-popup'

export default {
	components: {
		cmGoodspopup
	},
	data() {
		return {
			isIphoneX: this.$store.state.isIphoneX,
			min: 0,
			max: 200,
			reportText: '',
			skeletonShow: true,
			isGoodspopup: false,
			handleGoods: [],
			handleType: '', // 1 机构添加报备 2 未注册客户添加报备
			reportClubParams: {
				serviceProviderId: 0,
				clubId: 0,
				productId: 0,
				reportText: ''
			},
			reportParams: {
				questionManId: 0,
				productId: 0,
				reportText: ''
			},
			reportInfo: {}
		}
	},
	onLoad(option) {
		console.log(option)
		this.handleType = option.type
		this.reportInfo = JSON.parse(option.reportInfo)
		if (this.handleType === '1') {
			// 机构新增报备
			this.reportClubParams.clubId = this.reportInfo.clubId
		} else {
			this.reportParams.questionManId = this.reportInfo.questionManId
		}
		this.initStorage()
		this.skeletonShow = false
	},
	methods: {
		...mapMutations(['login']),
		async initStorage() {
			const userInfo = await this.$api.getStorage()
			this.reportClubParams.serviceProviderId = userInfo.serviceProviderId
		},
		editButtonConfim() {
			// 确定提交保存
			switch (this.handleType) {
				case '1': // 机构报备保存
					this.userClubReportSave()
					break
				case '2': // 咨询人报备保存
					this.userReportVisitorSave()
					break
			}
		},
		userClubReportSave() {
			// 机构报备保存
			this.UserService.userClubReportSave(this.reportClubParams)
				.then(response => {
					this.$util.msg('保存成功', 3000, true, 'success')
					this.isConfirmLoding = false
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
					this.isConfirmLoding = false
				})
		},
		userReportVisitorSave() {
			// 未注册客户报备保存
			this.UserService.userReportVisitorSave(this.reportParams)
				.then(response => {
					this.$util.msg('保存成功', 3000, true, 'success')
					this.isConfirmLoding = false
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
					this.isConfirmLoding = false
				})
		},
		handleShowGoodPopup() {
			// 显示添加商品弹窗
			this.isGoodspopup = true
		},
		handleClean() {
			//删除选择的商品
			this.handleGoods = []
		},
		handleChoiceaGoodsData(data) {
			this.handleGoods = []
			this.reportClubParams.productId = this.reportParams.productId = data.productId
			this.handleGoods.push(data)
			console.log('handleGoods', this.handleGoods)
		},
		conInput(e) {
			//备注文字字数限制
			let value = e.detail.value
			let len = parseInt(value.length)
			if (len > this.max) return
			this.min = len
			if (this.min == 200) {
				this.$util.msg('您输入的字数已达上限', 2000)
			}
		}
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	height: auto;
	background: #ffffff;
}
.remarks-content {
	width: 100%;
	height: auto;
	box-sizing: border-box;
	padding: 0 24rpx;
	padding-bottom: 80rpx;
	.list-view-title {
		width: 100%;
		height: auto;
		margin-bottom: 16rpx;
		margin-top: 40rpx;
		.list-view-h1 {
			line-height: 40rpx;
			font-size: $font-size-30;
			color: #333333;
			text-align: left;
			font-weight: bold;
			text {
				color: #ff2a2a;
				&.none {
					color: #999999;
					font-weight: normal;
				}
			}
		}
	}
	.remarks-textarea {
		width: 100%;
		height: 226rpx;
		padding: 16rpx;
		margin: 20rpx 0 0 0;
		border-radius: 6rpx;
		position: relative;
		border: 1px solid #b2b2b2;
		box-sizing: border-box;
		.textarea {
			width: 100%;
			height: 100%;
			line-height: 36rpx;
			font-size: $font-size-26;
			color: $text-color;
			z-index: 1;
		}
		.limit-text {
			position: absolute;
			right: 20rpx;
			bottom: 16rpx;
			line-height: 44rpx;
			font-size: $font-size-24;
			color: #b2b2b2;
		}
	}
	.tui-remarks-content {
		width: 100%;
		height: auto;
		margin-bottom: 24rpx;
		.tui-remarks-goods-input {
			width: 100%;
			height: 180rpx;
			box-sizing: border-box;
			background-color: #f7f7f7;
			padding: 26rpx;
			border-radius: 6rpx;
			.input-add {
				width: 128rpx;
				height: 128rpx;
				line-height: 128rpx;
				float: left;
				text-align: center;
				box-sizing: border-box;
				border: 1px dashed #b2b2b2;
				border-radius: 6rpx;
				.iconfont {
					font-size: 44rpx;
					color: #b2b2b2;
				}
			}
			.input-text {
				height: 128rpx;
				box-sizing: border-box;
				padding: 0 32rpx;
				line-height: 128rpx;
				text-align: left;
				font-size: 26rpx;
				color: #b2b2b2;
				float: left;
			}
		}
		.tui-remarks-showgoods {
			width: 100%;
			height: 180rpx;
			box-sizing: border-box;
			.tui-remarks-btn {
				width: 90rpx;
				height: 180rpx;
				float: right;
				.btn {
					width: 100%;
					height: 90rpx;
					float: left;
					line-height: 90rpx;
					text-align: center;
					font-size: 26rpx;
					&.delete {
						color: #f94b4b;
					}
					&.update {
						color: #1890f9;
					}
				}
			}
			.tui-remarks-goods {
				width: 612rpx;
				height: 180rpx;
				box-sizing: border-box;
				background-color: #f7f7f7;
				padding: 26rpx;
				border-radius: 6rpx;
				float: left;
				.goods-image {
					width: 128rpx;
					height: 128rpx;
					float: left;
					image {
						width: 128rpx;
						height: 128rpx;
						display: block;
						border-radius: 4rpx;
					}
				}
				.goods-main {
					width: 432rpx;
					height: 128rpx;
					box-sizing: border-box;
					padding-left: 32rpx;
					float: right;
					.name {
						width: 100%;
						height: 60rpx;
						box-sizing: border-box;
						line-height: 60rpx;
						color: #333333;
						text-overflow: ellipsis;
						overflow: hidden;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						line-clamp: 1;
						-webkit-box-orient: vertical;
						font-size: 26rpx;
					}
					.shop {
						line-height: 60rpx;
						color: #999999;
						font-size: 26rpx;
						text-overflow: ellipsis;
						overflow: hidden;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						line-clamp: 1;
						-webkit-box-orient: vertical;
					}
				}
			}
		}
	}
	.list-view {
		width: 100%;
		height: auto;
		margin-top: 20rpx;
		.list-view-text {
			width: 100%;
			float: left;
			.input {
				width: 500rpx;
				height: 50rpx;
				box-sizing: border-box;
				line-height: 50rpx;
				color: #333333;
				text-overflow: ellipsis;
				overflow: hidden;
				display: -webkit-box;
				-webkit-line-clamp: 1;
				line-clamp: 1;
				-webkit-box-orient: vertical;
				float: left;
				font-size: 26rpx;
			}
			.delbtn {
				width: 96rpx;
				height: 44rpx;
				border-radius: 8rpx;
				font-size: $font-size-24;
				color: #e15616;
				line-height: 44rpx;
				text-align: center;
				float: left;
				&.down {
					color: #1890f9;
				}
			}
		}
	}
}
.remarks-btn {
	width: 100%;
	padding-top: 20rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	background-color: #ffffff;
	z-index: 99;
	.edit-button-canel {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		color: #e15616;
		font-size: $font-size-24;
	}
	.edit-button {
		width: 600rpx;
		height: 90rpx;
		background: $btn-confirm;
		line-height: 90rpx;
		text-align: center;
		color: #ffffff;
		font-size: $font-size-30;
		margin: 0 auto;
		border-radius: 45rpx;
	}
}
</style>
