<template>
	<view class="addIssue">
		<view class="fixedheader">
			<headers :showBack="true">
				<view class="headerName">
					施工日志
				</view>
			</headers>
		</view>
		<view class="search-box" :style="{paddingTop: mobileTopHeight + 44 + 'px'}">
			<u-tabs :bar-height="4" :font-size="28" active-color="#498CEC" inactive-color="#B3B3B3" :bar-width="375"
				:list="list" :is-scroll="false" :current="current" @change="currentChange"></u-tabs>
		</view>
		<view class="content" :style="{paddingTop: mobileTopHeight + 84 + 'px'}">
			<CalendarView @examineCalendarView="examineCalendarView" :ocrBuildLogAllList="
          formInline.type == 1
            ? ocrBuildLogCountOcrBuildLogForMonth
            : ocrBuildLogAllList
        " :defaultMonth="formInline.month" :type="formInline.type"></CalendarView>
			<view class="content_main" :class="{'pb-138':formInline.type == 2}">
				<view class="main3_box" v-if="formInline.type == 2 && ocrBuildLogInfo.id">
					<view class="box-header">
						<view>施工情况</view>
						<view @click="onNavigateToDetail(ocrBuildLogInfo)">查看详情</view>
					</view>
					<view class="box-item">
						<view>施工区域</view>
						<view>{{ocrBuildLogInfo.constructionAreaNames}}</view>
					</view>
					<view class="box-content">
						<view>施工任务</view>
						<view v-if="ocrBuildLogInfo.lineHeightFlag1"
							@click.stop="ocrBuildLogInfo.expandMoreShow1 = !ocrBuildLogInfo.expandMoreShow1">
							{{ocrBuildLogInfo.expandMoreShow1 ? '收起更多': '展开更多'}}
						</view>
					</view>
					<view
						:class="{'webkit-clamp_2': ocrBuildLogInfo.lineHeightFlag1 && !ocrBuildLogInfo.expandMoreShow1}"
						id="box-content_detail1" class="box-content_detail">
						{{ocrBuildLogInfo.constructionTask}}
					</view>
					<view class="box-content">
						<view>出勤情况</view>
						<view v-if="ocrBuildLogInfo.lineHeightFlag2"
							@click.stop="ocrBuildLogInfo.expandMoreShow2 = !ocrBuildLogInfo.expandMoreShow2">
							{{ocrBuildLogInfo.expandMoreShow2 ? '收起更多': '展开更多'}}
						</view>
					</view>
					<view
						:class="{'webkit-clamp_2': ocrBuildLogInfo.lineHeightFlag2 && !ocrBuildLogInfo.expandMoreShow2}"
						id="box-content_detail2" class="box-content_detail">
						{{ocrBuildLogInfo.attendance}}
					</view>
					<view class="box-content">
						<view>施工机械</view>
						<view v-if="ocrBuildLogInfo.lineHeightFlag3"
							@click.stop="ocrBuildLogInfo.expandMoreShow3 = !ocrBuildLogInfo.expandMoreShow3">
							{{ocrBuildLogInfo.expandMoreShow3 ? '收起更多': '展开更多'}}
						</view>
					</view>
					<view
						:class="{'webkit-clamp_2': ocrBuildLogInfo.lineHeightFlag3 && !ocrBuildLogInfo.expandMoreShow3}"
						id="box-content_detail3" class="box-content_detail">
						{{ocrBuildLogInfo.constructionMachinery}}
					</view>
				</view>
				<view class="main2_box" v-else-if="formInline.type == 1 && personalDiaryList.length > 0">
					<view class="main2_box-main" v-for="item in personalDiaryList" :key="item.id">
						<view class="box-header">
							<view>{{$dayjs(item.date).format("YYYY年MM月DD日")}}</view>
							<view @click="onNavigateToDetail(item)">查看详情</view>
						</view>
						<view class="box-item1">
							<view>
								风力:{{item.weatherJson ? item.weatherJson.win_speed : '--'}}
							</view>
							<view>
								气温:{{item.weatherJson ? item.weatherJson.tem_day : '--'}}
							</view>
							<view>
								天气: {{item.weatherJson ? item.weatherJson.wea: '--'}}
							</view>
						</view>
						<view class="box-item">
							<view>施工区域</view>
							<view>{{item.constructionAreaNames}}</view>
						</view>
						<view class="box-item">
							<view>施工单位</view>
							<view>{{item.constructionUnitNames}}</view>
						</view>
						<view class="box-item">
							<view>施工人员</view>
							<view>{{item.constructionWorkerNames}}</view>
						</view>
					</view>
					<view @click="onProjectLog" class="main2_box-btn">
						查看全部项目日志
						<u-icon name="arrow-right-double" color="#5382F6" size="40"></u-icon>
					</view>
				</view>
				<view class="main1_box" v-else>
					<view class="new-nodata">
						<view></view>
						<text>暂无数据...</text>
					</view>
				</view>
			</view>

			<view v-if="formInline.type == 2 && !ocrBuildLogInfo.id" class="confrim-btn">
				<view @click="createProjectLog">创建个人安全日志</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import CalendarView from './components/calendarView.vue';
	import {
		isJSON
	} from "@/utils/tool.js"
	export default {
		components: {
			CalendarView
		},
		data() {
			return {
				mobileTopHeight: 0,
				projectDetail: {},
				list: [{
					name: '个人日志'
				}, {
					name: '项目日志'
				}],
				current: 0,
				formInline: {
					type: 2,
					month: "",
				},
				ocrBuildLogAllList: [],
				ocrBuildLogInfo: {
					id: "",
				},
				ocrBuildLogCountOcrBuildLogForMonth: [],
				personalDiaryList: [],
				personalDiaryInfo: {
					total: 0,
					date: "",
				},
			}
		},
		onLoad(opts) {
			this.projectDetail = JSON.parse(uni.getStorageSync('projectDetail'));
			this.formInline.month = this.$dayjs().format("YYYY-MM-DD");
			this.onQuery();
		},
		onShow() {
			let that = this
			uni.$on('updateData', function(data) {
				that.onQuery();
			})
		},
		mounted() {
			var that = this
			uni.getSystemInfo({
				success(res) {
					that.mobileTopHeight = res.statusBarHeight ? res.statusBarHeight : 0;
					uni.setStorageSync('systemInfo', res)
					console.log(res)
				}
			})
		},
		methods: {
			createProjectLog() {
				uni.navigateTo({
					url: `./addBuildersDiary?date=${this.personalDiaryInfo.date}&type=${this.formInline.type}`
				})
			},
			onProjectLog() {
				uni.navigateTo({
					url: `./buildersDiaryAll?date=${this.personalDiaryInfo.date}&type=${this.formInline.type}`
				})
			},
			onQuery() {
				if (this.formInline.type == 1) {
					this.getOcrBuildLogCountOcrBuildLogForMonth();
				} else {
					this.getOcrBuildLogList();
				}
			},
			getOcrBuildLogCountOcrBuildLogForMonth() {
				let data = {
					projectSn: this.projectDetail.projectSn,
					type: this.formInline.type,
					yearMonth: this.$dayjs(this.formInline.month).format("YYYY-MM"),
				}
				this.sendRequest({
					url: 'xmgl/ocrBuildLog/countOcrBuildLogForMonth',
					method: 'POST',
					data: data,
					success: res => {
						if (res.code == 200) {
							this.ocrBuildLogCountOcrBuildLogForMonth = res.result;
						}
					}
				})
			},
			// 打开详情
			onNavigateToDetail(row) {
				uni.navigateTo({
					url: `./buildersDiaryDetail?id=${row.id}&type=${this.formInline.type}`
				})
			},
			getPersonalDiaryList() {
				let data = {
					projectSn: this.projectDetail.projectSn,
					pageNo: 1,
					pageSize: 2,
					type: this.formInline.type,
					date: this.personalDiaryInfo.date,
				}
				this.sendRequest({
					url: 'xmgl/ocrBuildLog/page',
					method: 'GET',
					data: data,
					success: res => {
						if (res.code == 200) {
							this.personalDiaryList = res.result.records.map(item => {
								return {
									...item,
									weatherJson: isJSON(item.weatherJson) ?
										JSON.parse(item.weatherJson) : {},
								}
							});
							this.personalDiaryInfo.total = res.result.total;
						}
					}
				})
			},
			examineCalendarView(item, type) {
				console.log(item, type)
				const time = this.$dayjs(item.date).format("YYYY-MM-DD");
				this.personalDiaryInfo.date = time;
				if (this.formInline.type == 1 && type != 1) {
					this.getPersonalDiaryList();
					return;
				}
				this.ocrBuildLogInfo = {
					...item,
					expandMoreShow1: false,
					expandMoreShow2: false,
					expandMoreShow3: false,
					lineHeightFlag1: false,
					lineHeightFlag2: false,
					lineHeightFlag3: false,
				}
				this.$nextTick(() => {
					// 增加延迟时间，确保DOM完全渲染
					this.getLineCount();
				})
			},
			getLineCount() {
				const that = this;
				// 使用 Promise 确保所有查询完成
				const promises = [];

				for (var i = 1; i <= 3; i++) {
					const promise = new Promise((resolve) => {
						// 增加重试机制
						const queryElement = (i, countNum = 3) => {
							countNum = countNum - 1;
							uni.createSelectorQuery().in(this).select(`#box-content_detail${i}`)
								.boundingClientRect(rect => {
									if (rect && rect.height > 0) {
										// 计算行数：每行高度约20rpx
										let lineHeight = Math.ceil(rect.height / uni.upx2px(20));
										console.log(`元素${i}高度:`, rect.height, '行数:', lineHeight);
										if (lineHeight > 2) {
											that.ocrBuildLogInfo[`lineHeightFlag${i}`] = true;
											console.log(that.ocrBuildLogInfo[`lineHeightFlag${i}`], that
												.ocrBuildLogInfo)
										}
										resolve();
									} else {
										// 如果元素未找到或高度为0，延迟重试
										console.log(`元素${i}未找到或高度为0，重试中...`);
										if (countNum > 0) {
											setTimeout(() => {
												queryElement(i, countNum);
											}, 200);
										}

									}
								}).exec();
						};

						queryElement(i, 5);
					});

					promises.push(promise);
				}

				// 等待所有查询完成
				Promise.all(promises).then(() => {
					console.log('所有行数计算完成');
				});
			},
			currentChange(index) {
				this.current = index;
				this.formInline.type = index == 0 ? 2 : 1;
				this.onQuery();
			},
			getOcrBuildLogList() {
				const firstDayOfMonth = this.$dayjs().startOf("month").format("YYYY-MM-DD");
				const lastDayOfMonth = this.$dayjs().endOf("month").format("YYYY-MM-DD");
				let data = {
					projectSn: this.projectDetail.projectSn,
					type: this.formInline.type,
					date_begin: this.formInline.month ? firstDayOfMonth : "",
					date_end: this.formInline.month ? lastDayOfMonth : "",
				}
				this.sendRequest({
					url: 'xmgl/ocrBuildLog/list',
					method: 'GET',
					data: data,
					success: res => {
						if (res.code == 200) {
							this.ocrBuildLogAllList = res.result;
						}
					}
				})
			},
			showToast(title, type) {
				this.$refs.uToast.show({
					title: title,
					type: type,
				})
			},
		},
		computed: {

		}
	}
</script>

<style scoped lang="scss">
	.addIssue {
		min-height: 100vh;
		background-color: #F2F3F7;
	}

	.content {
		.pb-138 {
			padding-bottom: 138rpx;
		}

		.content_main {
			margin-top: 26rpx;

			.main1_box {
				background-color: white;
				min-height: 650rpx;
				position: relative;
			}

			.main2_box {
				.main2_box-btn {
					margin-top: 26rpx;
					background-color: white;
					padding: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: 500;
					font-size: 30rpx;
					color: #5382F6;

					>.u-icon {
						margin-left: 10rpx;
					}
				}

				.main2_box-main {
					padding: 20rpx 26rpx;
					margin-top: 26rpx;
					position: relative;
					background-color: white;

					.box-item1 {
						margin-top: 20rpx;
						background-color: white;
						font-size: 28rpx;
						color: #808080;
						display: flex;
						justify-content: space-between;
					}

					.box-item {
						display: flex;
						align-items: center;
						margin-top: 26rpx;
						font-size: 28rpx;

						>view:first-child {
							width: 168rpx;
							margin-right: 26rpx;
							color: #808080;
						}

						>view:last-child {
							flex: 1;
							color: #4D4D4D;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							/* 限制为两行 */
							overflow: hidden;
						}
					}

					.box-header {
						display: flex;
						align-items: center;
						justify-content: space-between;

						>view:first-child {
							font-weight: 500;
							font-size: 30rpx;
							color: #1A1A1A;
						}

						>view:last-child {
							font-weight: 500;
							font-size: 24rpx;
							color: #4D8EEC;
						}
					}
				}
			}

			.main3_box {
				margin-top: 26rpx;
				background-color: white;
				padding: 26rpx 22rpx;
				position: relative;

				.box-content_detail {
					font-size: 28rpx;
					color: #4D4D4D;
					margin-top: 4rpx;
					word-break:break-all;
				}

				.webkit-clamp_2 {
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					/* 限制为两行 */
					overflow: hidden;
				}

				.box-content {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 26rpx;

					>view:last-child {
						font-size: 24rpx;
						color: #4D8EEC;
					}

					>view:first-child {
						font-size: 28rpx;
						color: #808080;
					}
				}

				.box-item {
					display: flex;
					align-items: center;
					margin-top: 26rpx;
					font-size: 28rpx;

					>view:first-child {
						width: 168rpx;
						margin-right: 26rpx;
						color: #808080;
					}

					>view:last-child {
						flex: 1;
						color: #4D4D4D;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						/* 限制为两行 */
						overflow: hidden;
					}
				}

				.box-header {
					display: flex;
					align-items: center;
					justify-content: space-between;

					>view:first-child {
						font-weight: 500;
						font-size: 30rpx;
						color: #1A1A1A;
					}

					>view:last-child {
						font-weight: 500;
						font-size: 24rpx;
						color: #4D8EEC;
					}
				}
			}
		}

		.confrim-btn {
			width: 100%;
			padding: 18rpx 26rpx;
			background-color: #FFFFFF;
			box-shadow: 0rpx -8rpx 8rpx 0rpx rgba(0, 0, 0, 0.05);
			display: flex;
			position: fixed;
			bottom: 0;
			z-index: 1;

			>view {
				width: 100%;
				height: 76rpx;
				font-weight: 500;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #5181F6;
				border-radius: 0rpx 6rpx 6rpx 0rpx;
				color: #FFFFFF;
			}
		}
	}

	.fixedheader {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 999;

		/deep/ .headerBox {
			border-bottom: none;
		}

		.headerName {
			z-index: 1;
		}
	}

	.search-box {
		background-color: white;
		width: 100%;
		position: fixed;
		z-index: 99;

		.uni-form-item {
			padding: 26rpx 26rpx 0;
			position: relative;
			display: flex;

			.uni-input {
				border: 2rpx solid #E4E4E4;
				border-radius: 68rpx;
				padding: 0;
				font-size: 30rpx;
				color: #444444;

				/deep/ .u-content {
					background-color: #FFFFFF !important;

					.u-input {
						background-color: #FFFFFF !important;
						color: #999999 !important;
					}
				}
			}

			.search-btn {
				width: 72rpx;
				height: 72rpx;
				background: #498CEC;
				border-radius: 48rpx;
				margin-left: 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				>image {
					width: 50rpx;
					height: 50rpx;
				}
			}
		}

		/deep/ .u-dropdown {
			padding-right: 60rpx;
		}

		.calendar_box {
			position: absolute;
			right: 28rpx;
			bottom: 20rpx;

			.calendar {
				width: 32rpx;
				height: 32rpx;
				background-image: url('@/static/workTicketManage/calendar.png');
				background-repeat: no-repeat;
				background-size: 100% 100%;
			}

			.calendar_active {
				background-image: url('@/static/workTicketManage/calendar-active.png');
			}
		}
	}

	.scroll-view {
		margin: 10rpx 26rpx;
		width: calc(100% - 26rpx - 26rpx);
		max-height: 60vh;
		border-radius: 6rpx;
	}

	/deep/ .u-dropdown__menu__item__text {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		/* 限制为两行 */
		overflow: hidden;
	}

	.cell-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		position: relative;
		box-sizing: border-box;
		width: 100%;
		padding: 13px 16px;
		font-size: 14px;
		line-height: 27px;
		color: #606266;
		background-color: #fff;
		text-align: left;

		>view {
			flex: 1;
			margin-right: 20rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 1;
			/* 限制为两行 */
			overflow: hidden;
		}

		.box-active {
			color: rgb(49, 144, 243);
		}

		>image {
			width: 32rpx;
			height: 32rpx;
		}
	}

	/deep/ .u-mask {
		z-index: 10 !important;
	}

	/deep/ .u-dropdown__content {
		// height: initial !important;
		// top: 166rpx !important;
		overflow: visible !important;
	}

	/deep/ .van-dropdown-item__content,
	/deep/ .u-cell-item-box {
		margin: 20rpx 26rpx;
		width: calc(100% - 26rpx - 26rpx);
		border-radius: 6rpx;
	}

	/deep/ .u-cell-item-box {
		// max-height: 60vh;
		// overflow-y: scroll;
	}

	/deep/ .uicon-arrow-down {
		// position: absolute;
		// top: 50%;
		// right: -4px;
		margin-top: -5px;
		border: 3px solid;
		border-color: transparent transparent #dcdee0 #dcdee0;
		-webkit-transform: rotate(-45deg);
		transform: rotate(-45deg);
		opacity: .8;
		content: '';
	}

	/deep/ .u-dropdown__menu__item__arrow--rotate .uicon-arrow-down {
		border-color: transparent transparent rgb(49, 144, 243) rgb(49, 144, 243);
	}

	/deep/ .uicon-arrow-down::before {
		display: none;
	}

	/deep/ .u-dropdown__menu {
		flex-wrap: wrap;
		// height: 166rpx !important;
		box-shadow: none;

		.u-dropdown__menu__item {
			width: 33%;
			// height: 82rpx;
			flex: initial;
		}
	}

	.new-nodata {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		>view {
			width: 300rpx;
			height: 300rpx;
			background-image: url('@/static/staffAttendance/nodata.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
		}

		>text {
			font-size: 22rpx;
			color: #808080;
			margin-top: 60rpx;
		}
	}

	.new-nodata_height {
		min-height: 500rpx;
		position: relative;
	}

	.new-nodata {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		>view {
			width: 300rpx;
			height: 300rpx;
			background-image: url('@/static/staffAttendance/nodata.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
		}

		>text {
			font-size: 22rpx;
			color: #808080;
			margin-top: 60rpx;
		}
	}
</style>