<template>
	<view class="container">
		<u-popup :show="showpopup" @close="showpopup=false" :safeAreaInsetBottom='false' :round="10">
			<view class="popupmain">
				<view class="popup_title">
					<text>筛选</text>
				</view>
				<view class="line">
				</view>
				<u-datetime-picker :show="show" @confirm='confirm1' @cancel='show=false'
					mode="date"></u-datetime-picker>
				<u-datetime-picker :show="show2" @confirm='confirm2' @cancel='show2=false'
					mode="date"></u-datetime-picker>
				<view class="project">
					<view class="project_box">
						<view class="title">
							创建开始时间
						</view>
						<view @click="show=true" class='main'>
							<input style='margin-left:10%' type="text" disabled placeholder="请选择"
								v-model="popupData.timeStart" />
						</view>
					</view>
					<view class="project_box">
						<view class="title">
							创建结束时间
						</view>
						<view @click="show2=true" class='main'>
							<input style='margin-left:10%' type="text" disabled placeholder="请选择"
								v-model="popupData.timeEnd" />
						</view>
					</view>
					<view class="project_box" v-for="(item,index) in columns" :key="index">
						<view class="title">
							{{item.title}}
						</view>
						<view class="main">
							<view class='btns_main' :class="popupData[item.key]===k.id?'primary':''"
								@click="changetype(item,k)" style="border-radius: 0.7rem;" v-for="k in item.project"
								:key="k.id">{{k.label}}</view>
						</view>
					</view>
				</view>
				<view class="popup_btns">
					<u-button text="重置" @click="repect"></u-button>
					<u-button type="primary" text="确定" @click="surepopup">
					</u-button>
				</view>
			</view>
		</u-popup>
		<view style="position: relative;">
			<view style="display: flex;align-items: center;">
				<input placeholder='管线名称'
					style="background-color: #F2F2F2;border: none;padding: 3px 10px;border-radius: 1rem;"
					:style="'width: 100%;'" v-model="dataForm.lineName" type="text" confirm-type="search"
					@confirm="searchs">
				<u-icon @click="showpopup = true" style="margin-left: 0.3rem;" size="26" name="list-dot"
					color="#C6C7CB"></u-icon>
			</view>
			<view style="position: absolute;right: 2.5rem;top: 0.4rem;z-index:10"
				@click="dataForm.lineName='',getList()">
				<u-icon v-if="dataForm.lineName" name="close-circle-fill" color="#C6C7CB"></u-icon>
			</view>
		</view>
		<scroll-list ref="list" style="margin-top: 10rpx;" :option="option" @refresh="refresh"
			@scrolltolower="loadMore">
			<view class="list-item" :class="item.isHandle=='1'?'red':'green'" v-for="(item, index) in list" :key="index"
				@click="handleTest(item)">
				<view class="main_box">
					<view class="text">
						管线名称 ：{{item.lineName}}
					</view>
					<view class="text">
						管线单元名称 ：{{item.lineUnitName}}
					</view>
					<view class="text">
						风险类型 ：{{item.riskCategory}}
					</view>
					<view class="text">
						风险状态：
						<span :style="{ color: item.isHandle=='1'?'red':'green' }">
							{{ item.isHandle== 1 ? "未处理" : "已处理"}}
						</span>
					</view>
				</view>
			</view>
		</scroll-list>
	</view>
</template>

<script>
	import Config from "@/common/Config.js";
	export default {
		data() {
			return {
				showpopup: false,
				show: false,
				scrollTop: 0,
				show2: false,
				columns: [{
					title: '状态',
					key: 'isHandle',
					project: [{
						label: '未处理',
						id: '1'
					}, {
						label: '已处理',
						id: '0'
					}]
				}],
				popupData: {
					timeStart: "",
					timeEnd: "",
					isHandle: ''
				},
				old: {
					scrollTop: 0
				},
				option: {
					size: 5,
					auto: true
				},
				dataForm: {
					page: 1,
					limit: 10
				},
				showType: false,
				templateIdList: ['JzW-GOYebclYExiTgICih8n5LfO38P7AFP_TAK8E2Ms'],
				list: []
			}
		},
		onLoad() {
			// #ifdef MP
			let that = this
			// uni.showModal({
			// 	title: '提示',
			// 	content: '确认进行服务通知提醒系统通知',
			// 	success: function(res) {
			// 		if (res.confirm) {
			// 			that.sureType()
			// 		} else if (res.cancel) {
			// 			console.log('用户点击取消');
			// 		}
			// 	}
			// });
			// #endif
		},
		onShow() {
			this.list = []
			this.dataForm.limit = 10
			this.getList()
		},
		methods: {
			sureType() {
				let that = this
				wx.requestSubscribeMessage({
					tmplIds: that.templateIdList,
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.error('Failed to request subscribe message:', err);
					},
					complete() {
						that.showType = false
					}
				});
			},
			gettemplateIdList() {
				let that = this
				// this.$api.to_http(`/message/WeChat/templateIdList`, "GET").then((res) => {
				// 	if (res.data.code != 0) {
				// 		return uni.$u.toast(res.data.msg)
				// 	}
				// 	that.templateIdList = res.data.data
				// })
				wx.getSetting({
					withSubscriptions: true,
					success(res) {
						that.showType = true
					}
				})
			},
			loadMore() {
				// 在触底时，调用加载更多的 API 方法
				this.dataForm.limit += 10; // 页数加1
				this.getList(); // 获取更多数据
			},
			changetype(x, k) {
				this.popupData[x.key] = k.id
			},
			searchs(e) {
				this.checked = []
				this.dataList = []
				this.list = []
				this.dataForm.limit = 10
				this.getList()
			},
			surepopup() {
				this.showpopup = false
				this.list = []
				this.dataForm.limit = 10
				this.dataForm = {
					...this.dataForm,
					...this.popupData
				}
				this.getList()
			},
			repect() {
				for (let key in this.popupData) {
					this.popupData[key] = ''
				}
			},
			confirm1(x) {
				this.popupData.timeStart = this.formatTimestamp(x.value)
				this.show = false
			},
			confirm2(x) {
				this.popupData.timeEnd = this.formatTimestamp(x.value)
				this.show2 = false
			},
			formatTimestamp(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以加1
				const day = date.getDate().toString().padStart(2, '0'); // 日期补充为两位数
				return `${year}-${month}-${day}`;
			},
			searchs(e) {
				this.checked = []
				this.dataList = []
				this.list = []
				this.dataForm.limit = 10
				this.getList()
			},
			handleTest(x) {
				uni.navigateTo({
					url: '/pages/risk/riskDetail?data=' + JSON.stringify(x)
				})
			},
			getList() {
				this.dataForm.handleUserIds = uni.getStorageSync('userInfo').id
				this.$api.to_http(`/pipe/zhkjwurenjipiperisk/getUserPipeRisk`, this.dataForm, 'get').then((res) => {
					this.list.push(...res.data.data.list)
					this.$refs.list.loadSuccess({
						list: this.list,
						total: res.data.data.total
					});
				})
			},
			// 加载数据
			load(paging) {
				this.$api.to_http(`/pipe/zhkjwurenjipiperisk/getUserPipeRisk`, this.dataForm, 'get').then((res) => {
					let list = [];
					for (var i = 0; i < res.data.data.list.length; i++) {
						list.push(res.data.data.list[i]);
					}
					this.list = [...this.list, ...list];
					// 加载成功  参数对象{list: 当前列表,total: 数据总长度(后端查询的total)}
					this.$refs.list.loadSuccess({
						list: this.list,
						total: res.data.data.total
					});
					// 加载失败
					// this.$refs.list.loadFail()
				})
			},
			// 刷新刷剧
			refresh(paging) {
				this.list = []
				this.dataForm.limit = 10
				this.$api.to_http(`/pipe/zhkjwurenjipiperisk/getUserPipeRisk`, this.dataForm, 'get').then((res) => {
					let list = [];
					for (var i = 0; i < res.data.data.list.length; i++) {
						list.push(res.data.data.list[i]);
					}
					this.list = list;
					// 加载成功  参数对象{list: 当前列表,total: 数据总长度(后端查询的total)}
					this.$refs.list.refreshSuccess({
						list: this.list,
						total: res.data.data.total
					});
					// 加载失败
					// this.$refs.list.loadFail()
				})
			}
		}

	}
</script>
<style lang="less" scoped>
	.list-item {
		font-size: 30rpx;
		padding: 10rpx 20rpx;
		position: relative;

		.main_box {
			border-radius: 15rpx;
			// border: 1px solid #000;
			padding: 30rpx;
			background-color: #fff;

			.text {
				margin: 18rpx 0;
			}
		}
	}

	.popupmain {

		.popup_title {
			padding: 0.5rem 1rem;
			text-align: center;
		}

		.line {
			height: 0.4rem;
			background-color: #F3F4F6;
		}

		.project {
			padding: 0.6rem;
			height: 15rem;
			overflow: auto;

			.project_box {
				margin-bottom: 0.5rem;

				.main {
					margin-top: 0.5rem;
					display: grid;
					grid-template-columns: 1fr 1fr 1fr;
					// grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
					grid-gap: 0.8rem;

					.btns_main {
						height: 40px;
						position: relative;
						align-items: center;
						justify-content: center;
						display: flex;
						flex-direction: row;
						box-sizing: border-box;
						flex-direction: row;
						padding: 0 12px;
						font-size: 14px;
						border-color: #ebedf0;
						border-width: 1px;
						border-style: solid;
					}

					.primary {
						background-color: #3c9cff;
						border-color: #3c9cff;
						color: #fff;
					}
				}
			}
		}

		.popup_btns {
			padding: 0.6rem;
			display: flex;
			margin-top: 1rem;
			border-top: 1px solid #f1f1f1;

			.u-button {
				width: 47%;
			}
		}
	}

	.list-item.green::after {
		display: block;
		content: '';
		width: 6rpx;
		height: 120rpx;
		background-color: #67c23a;
		position: absolute;
		top: 30%;
	}
	.list-item.red::after {
		display: block;
		content: '';
		width: 6rpx;
		height: 120rpx;
		background-color: #f56c6c;
		position: absolute;
		top: 30%;
	}
</style>