<template>
	<view class="addPlan">
		<headers class="fixedheader" :themeType="true" :showBack="true">
			<view class="headerName">
				{{title}}
			</view>
		</headers>
		<view :style="{ 'padding-top': statusBarHeight + 45 + 'px' }">
			<view class="plan-form">
				<!-- <view class="form-item b-bottom">
					<view class="form-lable"><text class="tag">*</text>验收类型</view>
					<picker @change="bindPickerChange" :value="typeIndex" range-key="name" :range="typeList">
						<view style="display: flex;align-items: center;">
							<view class="uni-input" style="margin-right: 10px;">{{typeList[typeIndex] ? typeList[typeIndex].name : ""}}</view>
							<uni-icons2 type="arrowright" size="20"></uni-icons2>
						</view>
					</picker>
				</view> -->
				<view class="form-item b-bottom">
					<view class="form-lable"><text class="tag">*</text>责任区域</view>
					<!-- <input class="from-input" type="text" :value="formData.acceptanceRegion" @blur="bindInputBlur"
						placeholder="请输入验收部位" /> -->
					<view class="from-input" @click="onConstructionAreasType(1)">
						<view>{{formData.regionName}}</view>
						<u-icon name="arrow-right" color="#A2A4AF" size="32"></u-icon>
					</view>
				</view>
				<!-- <view class="form-item b-bottom" style="justify-content: flex-start;">
					<view class="form-lable"><text class="tag">*</text>验收结果</view>
					<view class="form-result">
						<ul class="form-radio">
							<li :class="{'active-radio': formData.acceptancePass == 1}" @click="checkRadio(1)">合格</li>
							<li :class="{'active-radio': formData.acceptancePass == 0}" @click="checkRadio(0)">不合格
							</li>
						</ul>
					</view>
				</view>
				<view class="form-item b-bottom">
					<view class="form-lable"><text class="tag">*</text>验收时间</view>
					<dateTimePiccker :placeholder="'请选择'" :start="startTime" fields="minute"
						:default-value="formData.acceptanceTime" @change="bindPickerChange($event,'startTime')"
						class="dateTimePiccker">
					</dateTimePiccker>
					<u-icon name="arrow-right" color="#A2A4AF" size="32"></u-icon>
				</view> -->

				<view class="form-item b-bottom a-none">
					<view class="form-lable"><text class="tag">*</text>进度描述</view>
					<textarea class="form-textarea" @input="bindLength" @blur="bindTextAreaBlur"
						:value="formData.progressDesc" placeholder="请输入进度描述" />
					<!-- <view class="txt-length">{{textareaLengh}}/50</view> -->
				</view>
				<view class="form-item a-none">
					<view class="form-lable">现场照片</view>

					<view class="uni-form-input imgBox_wrap">
						<view class="imgBox" v-show="formData.imgFileList.length>0"
							v-for="(item,index) in formData.imgFileList" :key="index">
							<image :src="url_config+'image/'+item.url" class="img"
								@click="previewImage(url_config+'image/'+item.url)">
							</image>
							<view @click="deleteImg(item,1)" style="margin: 5px;" class="deleteImg"></view>
						</view>
						<view class="addImgBox" @click="uploadImg" v-if="formData.imgFileList.length<1">
							<image src="/static/icon-add.png" class="icon-add"></image>
						</view>
					</view>
				</view>

			</view>
		</view>
		<view class="save-box">
			<view class="save-btn" @click="saveForm">保存</view>
		</view>
		<u-popup :mask-close-able="false" v-model="constructionAreasShow" :closeable="false" mode="bottom">
			<view class="content-popup">
				<view class="content-popup_header">
					验收部位
				</view>
				<scroll-view v-if="checkPointList.length > 0 && constructionAreasType == 1" scroll-y="true">
					<TreeView :defaultExpanded="true" @node-click="onConstructionAreasClick" :multiple="false"
						checked-bg-color="rgba(81,129,246,0.1)" :data="checkPointList" :children-key="'children'"
						:label-key="'regionName'" :id-key="'id'" :value="constructionAreasList" />
				</scroll-view>
				<view class="new-nodata_height" v-else>
					<view class="new-nodata">
						<view></view>
						<text>暂无数据...</text>
					</view>
				</view>
				<view class="confrim-btn">
					<view @click="onCancelClick">取消</view>
					<view @click="onSubmitType">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import dateTimePiccker from '@/components/dateTimePicker/index.vue'
	import TreeView from '@/components/tree-view/TreeView.vue';
	export default {
		components: {
			dateTimePiccker,
			TreeView,
		},
		data() {
			return {
				statusBarHeight: 0,
				title: "新增进度记录",
				formData: {
					acceptanceRegion: "",
					progressDesc: "",
					imgFileList: [],
					acceptancePass: 1,
					acceptanceType: "",
					dangerousEngineeringId: "",
					acceptanceTime: "",
					regionId: [],
					regionName: "",
				},
				textareaLengh: 0,
				typeIndex: "",
				typeList: [{
					id: 1,
					name: '施工条件验收'
				}, {
					id: 2,
					name: '危大工程验收'
				}],
				startTime: this.getDate({
					format: true,
				}),
				checkPointList: [],
				// 区域
				constructionAreasList: [],
				constructionAreasNameList: [],
				constructionAreasShow: false,
				constructionAreasType: "",
				projectSn: "",
			}
		},
		onShow() {
			this.statusBarHeight = uni.getStorageSync('systemInfo').statusBarHeight;
		},
		onLoad(val) {
			this.projectSn = JSON.parse(uni.getStorageSync('userInfo')).sn
			this.formData.dangerousEngineeringId = val.id;
			this.formData.acceptanceType = val.acceptanceType;
			this.getCheckPointList();
		},
		methods: {
			getCheckPointList() {
				let that = this;
				let data = {
					projectSn: this.projectSn
				};
				this.sendRequest({
					url: 'xmgl/qualityRegion/list',
					method: 'POST',
					data: data,
					success: res => {
						if (res.code == 200) {
							that.checkPointList = res.result;
						}
					}
				})
			},
			// 多选下拉 1区域 2单位 3人员
			onConstructionAreasType(type) {
				this.constructionAreasType = type;
				if (type == 1) {
					this.constructionAreasList = this.formData.regionId.map(item => item);
					this.constructionAreasNameList = this.formData.regionName ? this.formData
						.regionName.split(',') : [];
				}
				this.constructionAreasShow = true;
			},
			// 区域
			onConstructionAreasClick(id, checked, label) {
				this.constructionAreasList = [id];
				this.constructionAreasNameList = [label];
			},
			onSubmitType() {
				if (this.constructionAreasType == 1) {
					this.formData.regionId = [...this.constructionAreasList];
					this.formData.regionName = this.constructionAreasNameList.join(',');
				}
				this.constructionAreasShow = false;
			},
			onCancelClick() {
				if (this.constructionAreasType == 1) {
					this.constructionAreasList = [...this.formData.regionId];
				}
				this.constructionAreasShow = false;
			},
			//下拉选择回显
			bindPickerChange(e, type) {
				console.log(888, e)
				if (type == 'startTime') {
					this.formData.acceptanceTime = `${e.f3}`;
				}
			},
			checkRadio(val) {
				this.formData.acceptancePass = val
			},
			bindTextAreaBlur(e) {
				console.log(e)
				this.formData.progressDesc = e.detail.value
			},
			bindInputBlur(e) {
				console.log(e)
				this.formData.acceptanceRegion = e.detail.value
			},
			uploadImg() {
				var that = this
				this.$chooseImage({
					count: 5 - that.formData.imgFileList.length,
					success(res) {
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: that.url_config + 'upload/image', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'files',
							success: (uploadFileRes) => {
								let data = {
									name: JSON.parse(uploadFileRes.data).data[0].filename,
									url: JSON.parse(uploadFileRes.data).data[0].imageUrl
								};
								that.formData.imgFileList.push(data)
							}
						});
					}
				})
			},
			//删除附件
			deleteImg(val, type) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该附件吗?',
					success: function(res) {
						if (res.confirm) {
							if (type == 1) {
								let fileArr = JSON.parse(JSON.stringify(that.formData.imgFileList));
								fileArr.forEach((item, index) => {
									if (item.url == val.url) {
										fileArr.splice(index, 1)
									}
								})
								that.formData.imgFileList = fileArr;
							} else if (type == 2) {
								let fileArr = JSON.parse(JSON.stringify(that.videoFileList));
								fileArr.forEach((item, index) => {
									if (item.url == val.url) {
										fileArr.splice(index, 1)
									}
								})
								that.videoFileList = fileArr;
							}

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},

			//预览图片
			previewImage(url) {
				uni.previewImage({
					urls: [url]
				})
			},
			bindLength(e) {
				// console.log(e)
				this.textareaLengh = e.detail.value.split('').length
			},
			saveForm() {
				// console.log(this.formData)
				const userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				let data = {
					progressDesc: this.formData.progressDesc,
					regionId: this.formData.regionId.join(','),
					// acceptancePass: this.formData.acceptancePass,
					// acceptanceType: this.formData.acceptanceType,
					// acceptanceTime: this.formData.acceptanceTime,
					recordTime: this.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
					recordedBy: userInfo.realName,
					recordId: this.formData.dangerousEngineeringId,
					projectSn: this.projectSn,
				}
				// data.createUser = JSON.parse(uni.getStorageSync('userInfo')).userId;
				data.imageUrl = this.formData.imgFileList.map(item => item.url).join(',');
				if (data.progressDesc == "") {
					uni.showToast({
						title: '请输入进度描述！',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (data.regionId == "") {
					uni.showToast({
						title: '请选择责任区域！',
						icon: "none",
						duration: 2000
					});
					return;
				}
				console.log(data)
				this.sendRequest({
					url: 'xmgl/bigDangerBuildProgress/add',
					method: 'post',
					data: data,
					success: res => {
						uni.hideLoading()
						if (res.code == 200) {
							uni.showToast({
								title: '保存成功'
							})
							uni.navigateBack({})
						}
						console.log(res)
					}
				})
			},
			// bindPickerChange(e) {
			// 	this.typeIndex = e.target.value
			// 	this.formData.acceptanceType = this.typeList[e.target.value].id
			// },
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year;
				} else if (type === 'end') {
					// year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content-popup {
		border-radius: 16rpx 16rpx 0rpx 0rpx;

		.content-popup_main {
			padding: 0 26rpx;

			>view {
				display: flex;
				// align-items: center;
				justify-content: space-between;
				line-height: 72rpx;

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

				>view:last-child {
					font-size: 28rpx;
					color: #808080;
					display: flex;
					justify-content: flex-end;
					width: 70%;

					.u-input {
						text-align: right !important;
					}

					.u-icon {
						margin-left: 10rpx;
						transform: rotate(-90deg);
					}
				}
			}

			>.view-box {
				display: flex;
				flex-direction: column;
				line-height: 72rpx;
				// margin-bottom: 20rpx;

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

				>view:last-child {
					font-size: 28rpx;
					color: #808080;
					display: flex;
					justify-content: flex-start;
					flex-wrap: wrap;
					width: 100%;

					>view:not(:nth-child(4n + 1)) {
						margin-left: 16rpx;
					}

					>view {
						min-width: calc(160rpx);
						padding: 0 12rpx;
						height: 80rpx;
						background: #F4F5F7;
						border-radius: 8rpx;
						font-size: 28rpx;
						color: #A2A4AF;
						display: flex;
						align-items: center;
						justify-content: center;
					}

					>.active {
						background: #498CEC;
						color: #FFFFFF;
					}
				}
			}
		}

		.content-popup_header {
			height: 86rpx;
			box-shadow: 0rpx 8rpx 10rpx -8rpx rgba(81, 129, 246, 0.42);
			display: flex;
			align-items: center;
			justify-content: center;
			font-weight: 800;
			font-size: 30rpx;
			color: #1A1A1A;
		}

		uni-scroll-view {
			margin: 26rpx 0;
		}

		.confrim-btn {
			padding: 18rpx 26rpx;
			background-color: #FFFFFF;
			box-shadow: 0rpx -8rpx 8rpx 0rpx rgba(0, 0, 0, 0.05);
			display: flex;

			>view {
				width: 50%;
				height: 76rpx;
				font-weight: 500;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			>view:first-child {
				background-color: rgba(81, 129, 246, 0.1);
				border-radius: 6rpx 0rpx 0rpx 6rpx;
				color: #5181F6;
			}

			>view:last-child {
				background-color: #5181F6;
				border-radius: 0rpx 6rpx 6rpx 0rpx;
				color: #FFFFFF;
			}
		}
	}

	.dateTimePiccker {
		// border-radius: 10rpx;
		// width: 67%;
		padding-left: 20rpx;
		width: calc(100% - 204rpx);
		font-size: 28rpx;
		height: 44rpx;
		line-height: 44rpx;
		color: #4D4D4D;

		/deep/ .content {
			text-align: right;
		}
	}

	.addPlan {
		min-height: 100%;
		background: #ffffff;

		.fixedheader {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			z-index: 2;
		}
		
		.save-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 104rpx;
			background: #FFFFFF;
			box-shadow: 0px -4px 4px 0px rgba(0,0,0,0.05);
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 18rpx 26rpx;
			> view {
				width: 100%;
				height: 100%;
				background: #5181F6;
				border-radius: 3px;
				font-weight: 500;
				font-size: 14px;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		// .save-btn {
		// 	background: #2b8df3;
		// }

		.imgBox_wrap {
			display: flex;
			flex-wrap: wrap;
			margin-top: 10px;
			width: 75%;
		}

		.imgBox {
			width: 112rpx;
			height: 120rpx;
			display: inline-flex;
			position: relative;
			flex-wrap: wrap;
			border-radius: 8rpx;
			margin-right: 20rpx;

			.img {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}

			.deleteImg {
				position: absolute;
				right: -20rpx;
				top: -20rpx;
				color: #fff;
				width: 20rpx;
				height: 20rpx;
				background-image: url('@/static/workTicketManage/index-icon9.png');
				background-repeat: no-repeat;
				background-size: 100% 100%;

			}

		}

		.addImgBox {
			width: 118rpx;
			height: 120rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			border: 2rpx solid #D8DBE8;
			display: inline-flex;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;

			.icon-add {
				width: 40rpx;
				height: 40rpx;
			}
		}

		.b-bottom {
			// border-bottom: 1px solid #F6F6F6;
		}

		.f-column {
			flex-direction: column;
		}

		.plan-form {
			padding-top: 20rpx;
			padding-bottom: 96rpx;

			.form-item {
				padding: 28rpx 24rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				background: #fff;

				.u-icon--right {
					margin-left: 20rpx;
				}

				.form-lable {
					width: 184rpx;
					margin-right: 20rpx;
					white-space: nowrap;

					.tag {
						color: #DD524D;
					}
				}

				.from-input {
					text-align: right;
					padding-left: 20rpx;
					width: calc(100% - 204rpx);
					font-size: 28rpx;
					height: 44rpx;
					// line-height: 68rpx;
					font-size: 14px;
					color: #4D4D4D;
					display: flex;
					align-items: center;
					justify-content: flex-end;
				}

				.form-result {
					.form-radio {
						display: flex;
						// margin-left: 30rpx;
						// border: 1px solid #E5E5E5;
						padding: 0;
						// border-radius: 12rpx;
						overflow: hidden;

						>li:not(:first-child) {
							margin-left: 16rpx;
						}

						li {
							font-size: 28rpx;
							list-style: none;
							width: 160rpx;
							height: 80rpx;
							background: #F4F5F7;
							border-radius: 8rpx;
							display: flex;
							align-items: center;
							justify-content: center;
						}

						.active-radio {
							background: #498CEC;
							color: #FFFFFF;
						}
					}
				}
			}

			.txt-length {
				color: #888;
				text-align: right;
				margin-top: 20rpx;
				font-size: 14px;
			}

			.form-textarea {
				// margin-top: 30rpx;
				width: calc(100% - 204rpx);
				// background: #F8F8F8;
				// border: 1px solid #f0f0f0;
				height: 160rpx;
				// padding: 20rpx;
				box-sizing: border-box;

				// overflow: auto;
				/deep/uni-textarea {
					width: 100%;
					height: 100%;
					font-size: 28rpx;

					.uni-textarea-compute {
						width: 100%;
						height: 100%;
						overflow: auto;
					}
				}
			}

			.a-none {
				align-items: initial;
			}
		}
	}
</style>