<!--
 * @Title: 
 * @Description: 墒情数据
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
	<view class="wrap">
		<view class="container">
			<uni-section title="查询" titleFontSize="0.8rem" type="line">
				<view class="search-block">
					<view class="adcd-adnm">
						<uni-data-picker placeholder="请选择地区" popup-title="请选择所在地区" :localdata="regionTree"
							v-model="selectedTreeNode" @change="onTreeChange" @nodeclick="onTreeNodeClick"
							@popupopened="onTreePopupOpened" @popupclosed="onTreePopupClosed" :clear-icon="false">
						</uni-data-picker>
					</view>
					<view class="rtu-code">
						<uni-easyinput :styles="inputStyles" @input="searchRtuCodeValClear" prefixIcon="search"
							v-model="searchRtuCodeVal" placeholder="请输入测站编码">
						</uni-easyinput>
					</view>
					<view class="rtu-name">
						<uni-easyinput :styles="inputStyles" @input="searchRtuNameValClear" prefixIcon="search"
							v-model="searchRtuNameVal" placeholder="请输入测站名称">
						</uni-easyinput>
					</view>
					<view class="submit-btn">
						<button type="default" @click="search">查 询</button>
					</view>
				</view>
			</uni-section>
			<uni-table ref="table" :loading="loading" border stripe type="" emptyText="暂无更多数据">
				<uni-tr>
					<uni-th width="120" align="center">上报时间</uni-th>
					<uni-th align="center">测站名称</uni-th>
					<uni-th width="120" align="center">墒情数据</uni-th>
				</uni-tr>
				<uni-tr v-for="item in tableData" :key="item.id">
					<uni-td>
						<view style="text-align: center;">{{ item.tm }}</view>
					</uni-td>
					<uni-td>
						<view style="text-align: center;">
							{{ item.rtuName }}/{{ item.rtuCode }}
						</view>
				
					</uni-td>
					<uni-td align="center">
						<view class="uni-group">
							<button class="uni-button" size="mini" type="default"
								@click="onGroundInfoClick(item)">墒情</button>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
			<view class="pagination-block">
				<uni-pagination :page-size="pageSize" :current="pageCurrent" :total="total" @change="pageChange"
					prev-text="前一页" next-text="后一页" />
			</view>
		</view>
	</view>
</template>

<script>
	import http from '@/http/api.js';
	export default {
		components: {},
		onLoad(option) {
			this.selectedIndexs = [];
		},
		created() {
			this.getRegionTree();
		},
		data() {
			return {
				title: '墒情实时数据',
				regionTree: [],
				regionCode: '',
				selectedTreeNode: '',
				searchRtuNameVal: '',
				searchRtuCodeVal: '',
				query: {},
				inputStyles: {
					color: '#808080',
					borderColor: '#d3d3d3'
				},
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				selectedIndexs: [],
				tableData: [],
			}
		},
		computed: {
		},
		onShow() {},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			// 分页触发
			pageChange(e) {
				this.$refs.table.clearSelection()
				//this.selectedIndexs.length = 0
				this.pageCurrent = e.current;
				this.getPage()
			},
			onTreeNodeClick(node) {
				console.log(JSON.stringify(node))
			},
			onTreePopupOpened(e) {
				console.log(JSON.stringify(e))
			},
			onTreePopupClosed(e) {
				console.log(JSON.stringify(e))
			},
			onTreeChange(e) {
				console.log(JSON.stringify(e))
				let nodes = e.detail.value;
				if (nodes.length > 0) {
					let node = nodes[nodes.length - 1];
					this.regionCode = node.value;
					this.query = {};
					this.query['adCode'] = this.regionCode;
					this.searchRtuNameVal = '';
					this.searchRtuCodeVal = '';
					this.pageCurrent = 1;
					this.getPage();
				} else {
					this.regionCode = '';
					this.query = {};
					this.searchRtuNameVal = '';
					this.searchRtuCodeVal = '';
					this.pageCurrent = 1;
					this.getPage();
				}
			},
			//录入查询
			search() {
				this.pageCurrent = 1;
				//	console.log(JSON.stringify(res))
				let params = {};
				if (this.searchRtuCodeVal.length > 0) {
					params['rtuCode'] = this.searchRtuCodeVal;
				}
				if (this.searchRtuNameVal.length > 0) {
					params['rtuName'] = this.searchRtuNameVal;
				}
				this.getPage(params);
			},
			searchRtuNameValClear(e) {
				if (e == null || e.length == 0) {
					this.searchRtuNameVal = '';
					if (this.searchRtuNameVal.length == 0) {
						let params = {};
						if (this.searchRtuCodeVal.length > 0) {
							params['rtuCode'] = this.searchRtuCodeVal;
						}
						this.getPage(params);
					}
				}
			},
			searchRtuCodeValClear(e) {
				if (e == null || e.length == 0) {
					this.searchRtuCodeVal = '';
					if (this.searchRtuCodeVal.length == 0) {
						let params = {};
						if (this.searchRtuNameVal.length > 0) {
							params['rtuName'] = this.searchRtuNameVal;
						}
						this.getPage(params);
					}
				}
			},
			// 多选处理
			selectedItems() {
				return this.selectedIndexs.map(i => this.tableData[i])
			},
			// 多选
			selectionChange(e) {
				console.log(e.detail.index)
				this.selectedIndexs = e.detail.index
			},
			//批量删除
			delTable() {
				//console.log(this.selectedItems())
			},
			getRegionTree() {
				var that = this;
				http.request({
					url: '/galaxy-business/baseinfo/region/tree',
					method: 'GET',
				}).then(res => {
					if (res.data != null) {
						that.regionTree = res.data;
						that.selectedTreeNode = that.regionTree[0].value;
						that.regionCode = that.regionTree[0].value;
						that.query = {};
						that.query['adCode'] = that.regionCode;
						//that.orgId = that.projectTree[0].orgId;
						//that.projectId = that.projectTree[0].projectId;
						that.getPage();
						//that.getCountInfo(that.regionCode);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			// 获取数据
			getPage(params = {}) {
				this.loading = true
				const current = this.pageCurrent;
				const size = this.pageSize;
				const isSubmit = '0';
				let postData = Object.assign(params, this.query);
				var that = this;
				http.request({
					url: '/galaxy-business/rtu/data/ground/page',
					method: 'GET',
					params: {
						current,
						size,
						isSubmit
					},
					data: postData,
				}).then(res => {
					if (res.data.records != null) {
						that.tableData = res.data.records;
						that.total = res.data.total;
					}
					this.loading = false
				}).catch(err => {
					console.log(err)
					this.loading = false
				})
			},
			onGroundInfoClick(item) {
				console.log(item.id + " " + item.rtuName)
				uni.navigateTo({
					url: '/pages/realdata/rtugrounddatadetail?id=' + item.id
				})
			},
		}
	}
</script>

<style>
	/* page {
		background-color: rgb(240, 242, 244);
	} */
</style>

<style lang="scss" scoped>
	.wrap {
		padding: 0 0 100rpx;
	}

	.container {
		background-color: #f7f7f7;
		min-height: 100vh;
		overflow: hidden;
	}
</style>
