<template>
	<view>
		<view class="submit">
			<view class="submit-chat">
				<!-- 文本框 -->
				<textarea cursor-spacing='20' class="chat-send btn" :show-confirm-bar="false"
					disable-default-padding="true" auto-height="true" @focus="focus" v-model="msg"></textarea>
				<view class="bt-img" @tap="more">
					<text class="t-icon t-icon-gengduoneirong"></text>
				</view>
				<view class="bt-img" v-if="msg">
					<button class="button" @tap="inputs" type="primary" size="mini">发送</button>
				</view>
			</view>
			<!-- 更多 -->
			<view class="more" :class="{displaynone:!ismore}">
				<view class="more_1">
					<view class="more-list" @tap="sendImg('album')">
						<text class="t-icon t-icon-tupian"></text>
						<view class="more-list-title">图片</view>
					</view>
					<view class="more-list">
						<text class="t-icon t-icon-zaixiankefu"></text>
						<button :plain="true" class="button" size="mini" type="default" open-type="contact">人工</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ismore: false,
				msg: "",
			};
		},
		methods: {
			//获取高度方法
			getElementHeight() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.submit').boundingClientRect(data => {
					this.$emit('heights', data.height);
				}).exec();
			},
			//文字发送
			inputs() {
				if (this.msg.length > 0) {
					// 0为表情和文字
					let msg = this.msg.replace(/[\r\n]/g, "");
					this.send(msg, '0')
				}
			},
			// 输入框聚焦
			focus() {
				//关闭其他项
				this.ismore = false;
				setTimeout(() => {
					this.getElementHeight()
				}, 10)
			},
			//更多功能
			more() {
				this.ismore = !this.ismore;
				setTimeout(() => {
					this.getElementHeight();
				}, 10)
			},
			//图片发送
			sendImg(e) {
				const that = this;
				let serverUrl = that.$config.serverFile;
				that.$emit('choseImg', true);
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: async function(res) {
						that.$emit('choseImg', true);
						let tempFile = JSON.parse(JSON.stringify(res.tempFilePaths));
						const arr = await that.$apifile(`/files/notarization/chat/upload`, 'file', tempFile[0], 'file');
						if (arr.errcode == '0') {
							let filePaths = serverUrl + arr.uri
							that.send(filePaths, '1')
						} else {
							that.$emit('choseImg', false);
							uni.showToast({
								title: arr.errmsg,
								icon: 'none'
							})
						}
					}
				});
			},
			//发送
			send(msg, type) {
				let data = {
					message: msg,
					type: type
				}
				this.$emit('inputs', data);
				setTimeout(() => {
					this.msg = '';
				}, 0)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.displaynone {
		display: none;
	}

	.submit-chat {
		width: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 14rpx 14rpx;

		.bt-img {
			margin: 0 0 0 10rpx;

			.button {
				background-color: var(--fFFColor);
				color: var(--mainColor);
				font-size: var(--font14Size);
			}
		}

		.btn {
			flex: auto;
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
			max-height: 160rpx;
			margin: 0 10rpx;
		}

		.chat-send {
			line-height: 44rpx;
			width: 300rpx;
		}

	}

	.more {
		width: 100%;
		height: 350rpx;
		background: #ecedee;
		box-shadow: 0px 11rpx 0px 0px rgba(0, 0, 0, 0.1);
		bottom: env(safe-area-inset-bottom);
		padding: 8rpx;
		box-sizing: border-box;

		.more_1 {
			display: flex;
			flex-wrap: wrap;

			.more-list {
				width: 18%;
				text-align: center;
				margin: 10rpx;
				padding: 8px 5px 0 5px;
				background-color: #ffffff;
				border-radius: 20rpx;

				.more-list-title {
					margin: 10rpx 0;
					font-size: 30rpx;
					color: rgba(39, 40, 50, 0.5);
					line-height: 34rpx;
				}

				.button {
					border: none !important;
					font-size: 14px;
					color: rgba(39, 40, 50, 0.5);
				}
			}
		}
	}
</style>