<template>
	<view class="page">
		<u-navbar :title="title" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			:title-bold='true' height='45' id="navbar">
		</u-navbar>
		<view class="top">
			<view class="top_left">
				{{apiaryName}}
			</view>
			<view class="top_right" @click="showmiyuan = true">
				其他蜂箱 <view class="iconfont icon-xiangyou1" style="font-size: 40rpx;">

				</view>
			</view>
		</view>
		<view class="set_log_box">
			<view class="tit">
				选择操作类型
			</view>
			<view class="set_type_box">
				<view class="type_li" v-for="(item,index) in tabs" :key="index" @click="btntab(index,item.type)"
					:class='indexs==index?"act1":""'>
					{{item.title}}
				</view>
			</view>
			<view class="tit" style="margin-top: 48rpx;">
				上传操作图片
			</view>
			<view class="icon">
				<view class="imgbox" v-if="imglist != ''" v-for="(item,index) in imglist " :key="index">
					<view v-if="title == '编辑操作日志'" class="xx" @click="imgdel(index)">×</view>
					<image :src="item" mode=""></image>
				</view>
				<view class="imgbox" @click="btn">
					<image src="https://api.ccttiot.com/smartmeter/img/static/ulkCEWjWYqwijg7yhdY9" mode=""></image>
				</view>

			</view>
			<view class="tit" style="margin-top: 48rpx;">
				操作说明描述
			</view>
			<view class="input-container">
				<view class="placeholder" v-if="!textValue">在此输入文字描述（选填）</view>
				<textarea class="custom-textarea" v-model="textValue" @focus="hidePlaceholder" style="border: none;"
					@input="updateWordCount" @blur="showPlaceholder"></textarea>
				<text class="word-count">{{ currentCount }}/140</text>
			</view>
			<view class="xy">
				<u-checkbox-group>
					<u-checkbox v-model="checked" shape="circle" active-color="#FFCC25"
						@change="checkboxChange">该蜂场的所有蜂箱进行此日志,并且记录</u-checkbox>
				</u-checkbox-group>
				<view class="txt">
				</view>
			</view>
			<view class="btn" v-if="title == '添加操作日志'" @click="btnadd">
				确认添加
			</view>
			<view class="btn" v-else @click="btnedit">
				确认修改
			</view>
		</view>
		<u-select v-model="showmiyuan" :list="fclist" title='选择蜂场' @confirm="confirmmy"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: " #F4FAF8",
				},
				showmiyuan: false,
				title: "添加操作日志",
				currentIndex: 0,
				wuflag: false,
				tabs: [{
						title: '换脾',
						type: 1
					},
					{
						title: '饲喂',
						type: 2
					},
					{
						title: '取蜜',
						type: 3
					},
					{
						title: '加脾',
						type: 4
					},
					{
						title: '除螨',
						type: 5
					},
					{
						title: '换王',
						type: 6
					},
					{
						title: '换位置',
						type: 7
					},

				],
				textValue: '',
				currentCount: 0,
				showxy: false,
				apiaryId: '',
				beehiveId: '',
				fclist: [],
				apiaryName: '',
				type: null,
				indexs: -1,
				imglist: [],
				token: '',
				checked: false,
				id: '',
				arrimg: []
			}
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
				this.title = '编辑操作日志'
				this.getxq()
			}
			this.getQiniuToken()
			this.getfc()
		},
		onShow() {
			this.imglist = []
		},
		methods: {
			// 编辑
			getxq() {
				this.$u.get(`/farm/beehive/log/${this.id}`).then(res => {
					if (res.code == 200) {
						this.type = res.data.type
						this.apiaryId = res.data.beehiveId
						this.indexs = res.data.type - 1
						this.apiaryName = res.data.beehiveName
						this.textValue = res.data.content
						this.checked = res.data.apiaryLog
						if (res.data.picture.length > 0) {
							this.imglist = res.data.picture.split(',')
						} else {
							this.imglist = []
						}
					}
				})
			},
			imgdel(indexToRemove) {
				this.imglist = this.imglist.filter((item, index) => index !== indexToRemove)
			},
			btnedit() {
				if (this.imglist) {
					let validUrls = this.imglist.filter(item => typeof item === 'string')
					this.arrimg = validUrls.join(',')
				}
				let data = {
					id: this.id,
					beehiveId: this.apiaryId,
					type: this.type,
					content: this.textValue,
					picture: this.arrimg,
					apiaryLog: this.checked
				}
				this.$u.put(`/farm/beehive/log`, data).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '编辑成功',
							icon: 'success',
							duration: 1000
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},



			checkboxChange(e) {
				this.checked = e
			},
			btnadd() {
				if (this.apiaryId == '') {
					uni.showToast({
						title: '请选择蜂箱',
						icon: 'none',
						duration: 1000
					})
				} else {
					if (this.imglist.length > 80) {
						let validUrls = this.imglist.filter(item => typeof item == 'string')
						this.imglist = validUrls.join(',')
						console.log(this.imglist.length);
					}else{
						this.imglist = this.imglist[0]
					}
					let data = {
						beehiveId: this.apiaryId,
						type: this.type,
						content: this.textValue,
						picture: this.imglist,
						apiaryLog: this.checked
					}
					this.$u.post(`/farm/beehive/log`, data).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '创建成功',
								icon: 'success',
								duration: 1000
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				}
			},

			confirmmy(e) {
				this.apiaryId = e[0].value
				this.apiaryName = e[0].label
			},
			// 请求蜂箱
			getfc() {
				this.$u.get(`/farm/beehive/listAll`).then(res => {
					if (res.code == 200) {
						this.fclist = res.data.map(item => ({
							value: item.beehiveId,
							label: item.name
						}))
					}
				})
			},
			btntab(index, type) {
				this.type = type
				this.indexs = index
			},
			hidePlaceholder() {
				this.placeholderVisible = false;
			},
			showPlaceholder() {
				if (!this.textValue) {
					this.placeholderVisible = true;
				}
			},
			updateWordCount() {
				this.currentCount = this.textValue.trim().replace(/\s+/g, '').length;
			},
			getQiniuToken() {
				this.$u.get("/common/qiniu/uploadInfo").then((res) => {
					if (res.code == 200) {
						this.token = res.token
					}
				});
			},
			btn() {
				let _this = this
				let math = 'static/' + _this.$u.guid(20)
				uni.chooseImage({
					count: 9,
					type: 'all',
					success(res) {
						const tempFilePaths = res.tempFiles
						wx.uploadFile({
							url: 'https://up-z2.qiniup.com',
							name: 'file',
							filePath: tempFilePaths[0].path,
							formData: {
								token: _this.token, //后端返回的token
								key: 'smartmeter/img/' + math
							},
							success: function(res) {
								let str = JSON.parse(res.data)
								_this.userImgs = 'https://api.ccttiot.com/' + str.key
								_this.imglist.push(_this.userImgs)
							}
						});
					}
				})
			},
			changeTag(idx, type) {
				this.type = type
				this.currentIndex = idx
				this.getfxrz()
			},
			ClickImage(PhotoAddress) {
				uni.previewImage({
					urls: [PhotoAddress], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: '', // 当前显示图片的http链接，默认是第一个
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				})
			},


		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F4FAF8;
	}

	.addtj {
		width: 300rpx;
		height: 90rpx;
		background-color: #FFCC25;
		color: #fff;
		text-align: center;
		line-height: 90rpx;
		position: fixed;
		left: 50%;
		bottom: 150rpx;
		border-radius: 10rpx;
		font-size: 36rpx;
	}

	.page {
		// position: relative;
		width: 750rpx;
		padding-bottom: 300rpx;
		box-sizing: border-box;

		.no_cont {
			margin: 152rpx auto 0;
			width: 432rpx;

			.img {
				image {
					width: 432rpx;
					height: 432rpx;
				}
			}

			.txt {
				margin-top: 50rpx;
				width: 100%;
				text-align: center;
				font-weight: 500;
				font-size: 32rpx;
				color: #808080;
			}
		}

		.set_log_box {
			padding: 50rpx 56rpx;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				position: fixed;
				left: 58rpx;
				bottom: 68rpx;
				width: 638rpx;
				height: 88rpx;
				background: #FFCC25;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
			}

			.xy {
				margin-top: 46rpx;
				width: 100%;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: center;

				.yuans {
					margin-top: 2rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 34rpx;
					height: 34rpx;
					border-radius: 50%;
					border: #808080 solid 2rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}
				}

				.txt {
					margin-left: 10rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #808080;

					span {
						color: #3D3D3D;
					}
				}

			}

			.input-container {
				position: relative;
				width: 612rpx;
				height: 172rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
				border-radius: 20rpx;
				margin-top: 40rpx;
				overflow: hidden;
				padding-right: 38rpx;
				box-sizing: border-box;
				border: 2rpx solid #C7C7C7;
			}

			.placeholder {
				position: absolute;
				top: 18rpx;
				left: 38rpx;
				color: #999;
				/* placeholder颜色 */
				pointer-events: none;
				/* 确保点击事件可以穿透到textarea上 */
			}

			.custom-textarea {
				width: 100%;
				height: 100%;
				/* 设置一个合适高度 */
				padding-top: 18rpx;
				/* 为placeholder留出空间 */
				padding-left: 38rpx;
				box-sizing: border-box;
				border: 1px solid #ccc;
			}

			.word-count {
				position: absolute;
				right: 10px;
				bottom: 10px;
				font-size: 12px;
				color: #999;
			}

			.icon {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				margin-top: 40rpx;

				.imgbox {
					width: 33%;
					padding-right: 10rpx;
					box-sizing: border-box;
					position: relative;

					.xx {
						width: 40rpx;
						height: 40rpx;
						border-radius: 50%;
						background-color: red;
						text-align: center;
						line-height: 40rpx;
						color: #fff;
						font-size: 32rpx;
						position: absolute;
						top: -8rpx;
						right: 0;
					}

					image {
						height: 212rpx;
						border-radius: 20rpx;
					}
				}

			}

			.tit {
				font-weight: 600;
				font-size: 36rpx;
				color: #3D3D3D;
			}

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

				// margin-top: 26rpx;
				.type_li {
					margin-top: 26rpx;
					margin-right: 18rpx;
					padding: 12rpx 50rpx;
					background: #FFF5D6;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					border: 2rpx solid #FFC107;
					font-weight: 500;
					font-size: 32rpx;
					color: #3D3D3D;
				}

				.act1 {
					background: #FFC107;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					border: 2rpx solid #FFC107;
					color: #FFFFFF;
				}
			}
		}

		.top {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 22rpx;
			margin: 0 auto;
			width: 658rpx;
			height: 106rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.top_left {
				font-weight: 600;
				font-size: 36rpx;
				color: #3D3D3D;
			}

			.top_right {
				display: flex;
				align-items: center;
				flex-wrap: nowrap;
				font-weight: 500;
				font-size: 36rpx;
				color: #808080;
			}
		}

		.cont_box {
			margin-top: 26rpx;
			display: flex;
			flex-wrap: nowrap;

			.left {
				margin-left: 48rpx;
				width: 216rpx;

				.left_cont_box {
					width: 216rpx;
					height: 100rpx;
					background: #fff;

					.left_cont {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 216rpx;
						height: 100rpx;
						font-weight: 600;
						font-size: 36rpx;
						color: #3D3D3D;
						background-color: #F4FAF8;

						// border-radius: 0rpx 40rpx 40rpx 0rpx;
						// border: 1rpx solid #fff;

					}

					.act1 {
						background: #fff;
						border-radius: 40rpx 0 0 40rpx;
						// box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
					}

					.act2 {
						border-radius: 0rpx 0rpx 40rpx 0rpx;
					}

					.act3 {
						border-radius: 0rpx 40rpx 0rpx 0rpx;
					}
				}

				.act4 {
					background-color: #F4FAF8;
					border-radius: 40rpx 0 0 40rpx;
				}

			}

			.right {
				padding: 16rpx;
				margin-left: -10rpx;
				width: 100%;
				height: 100vh;
				background: #fff;

				.right_cont {
					padding: 20rpx 26rpx;
					border: 2rpx solid #D8D8D8;
					border-radius: 20rpx;
					margin-top: 20rpx;

					.log_work {
						margin-top: 16rpx;
						display: flex;
						flex-wrap: nowrap;
						align-items: center;

						.work_cont {
							display: flex;
							flex-wrap: nowrap;
							align-items: center;

							image {
								width: 29rpx;
								height: 29rpx;
							}

							.cont_xtx {
								margin-left: 6rpx;
								font-weight: 400;
								font-size: 28rpx;
								color: #3D3D3D;
							}
						}
					}

					.log_info {
						margin-top: 16rpx;
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						justify-content: space-between;

						.txt {
							font-weight: 400;
							font-size: 28rpx;
							color: #3D3D3D;
						}
					}

					.imgcont {
						display: flex;
						flex-wrap: wrap;
						margin-top: 16rpx;

						.img_box {
							margin-top: 16rpx;
							margin-right: 14rpx;
							height: 124rpx;
							width: 124rpx;

							image {
								background: #D8D8D8;
								border-radius: 12rpx 12rpx 12rpx 12rpx;
								height: 124rpx;
								width: 124rpx;
							}
						}
					}

					// border-radius: 20rpx 20rpx 20rpx 20rpx;
					.cont {
						margin-top: 16rpx;
						font-weight: 400;
						font-size: 28rpx;
						color: #3D3D3D;
					}

					.right_cont_top {
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						justify-content: space-between;

						.cont_left {
							font-weight: 400;
							font-size: 28rpx;
							color: #808080;
						}

						.cont_right {
							font-weight: 500;
							font-size: 28rpx;
							color: #FFC107;
						}
					}
				}

				// border-radius: 40rpx 0 0 40rpx;
				// box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
			}
		}




	}
</style>