<!--
 * @Title: 
 * @Description: 当前维修任务列表
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
	<view class="wrap">
		<uni-nav-bar dark :fixed="true" backgroundColor="#3F9EFF" statusBar="false" left-icon="left" left-text="返回"
			@clickLeft="toBack">
			<view class="nav-title">
				<text>{{title}}</text>
			</view>
		</uni-nav-bar>
		<view class="container">
			<uni-section title="查询" 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>
			<view class="count-info-line">
				<view class="orderCount">
					<text class="name">任务数量:</text>
					<text class="count">{{ countInfo.orderCount }}</text>
				</view>
				<view class="unconfirmOrderCount">
					<text class="name">未确认任务:</text>
					<text class="count">{{
				   		                                    countInfo.orderUnconfirm }}</text>
				</view>
				<view class="processOrderCount">
					<text class="name">处理中任务:</text>
					<text class="count">{{
				   	                                        countInfo.orderProcessing }}</text>
				</view>

			</view>
			<view class="count-info-line">
				<view class="reportOrderCount">
					<text class="name">已处理任务:</text>
					<text class="count">{{
				   	                                        countInfo.reportOrder }}</text>
				</view>
			</view>
			<view style="padding-left: 0px;padding-right: 0px;">
				<uni-collapse v-model="searchorderstatus">
					<uni-collapse-item title="任务状态筛选条件" :show-animation="true">
						<view class="select-checkbox">
							<uni-data-checkbox v-model="orderStatusOption" :localdata="orderStatusItems"
								@change="orderStatusOptionChange">
							</uni-data-checkbox>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view style="padding-left: 0px;padding-right: 0px;">
				<uni-collapse v-model="searchwarnstatus">
					<uni-collapse-item title="异常类别筛选条件" :show-animation="true">
						<view class="select-checkbox">
							<uni-data-checkbox v-model="warnKindOption" :localdata="warnKindItems"
								@change="warnKindOptionChange">
							</uni-data-checkbox>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<uni-list>
				<uni-list-item v-for="item in list" :key="item.id">
					<template v-slot:body>
						<view class="list-item-block">
							<view class="items-line">
								<image class="item-title-run-status-icon" style="box-shadow:0 0 2px 2px lightblue"
									src="/static/images/list/order_item.png" mode="widthFix">
								</image>
								<text class="item-title-rtu-name">{{item.rtuName}}</text>
								<text class="item-title-rtu-code">[{{item.rtuCode}}]</text>
							</view>
							<view class="items-line">
								<uni-icons class="input-uni-icon" type="list" size="18" color="lightblue" />
								<text class="item-text-lable">维修状态：</text>
								<text v-if="item.orderStatus==1" class="item-text-content"
									style="color: red;">等待确认</text>
								<text v-if="item.orderStatus==2" class="item-text-content"
									style="color: orange;">处理中</text>
								<text v-if="item.orderStatus==3" class="item-text-content"
									style="color: blue;">已处理</text>
								<text v-if="item.orderStatus==4" class="item-text-content"
									style="color: green;">已完成</text>
							</view>
							<view class="items-line">
								<uni-icons class="input-uni-icon" type="bars" size="18" color="lightblue" />
								<text class="item-text-lable">异常类型：</text>
								<text class="item-text-content-ellipsis">{{item.warnKindInfo}}</text>
							</view>
							<view v-if="item.orderStatus==1" class="items-line">
								<uni-icons class="input-uni-icon" type="calendar" size="18" color="lightblue" />
								<text class="item-text-lable">创建时长：</text>
								<text class="item-text-content" style="color: red;">{{item.taskCreateDuration}}</text>
							</view>
							<view v-else class="items-line">
								<uni-icons class="input-uni-icon" type="calendar" size="18" color="lightblue" />
								<text class="item-text-lable">维修时长：</text>
								<text class="item-text-content" style="color: red;">{{item.taskProcessDuration}}</text>
							</view>
							<view class="items-line">
								<uni-icons class="input-uni-icon" type="location" size="18" color="lightblue" />
								<text class="item-text-lable">行政区划：</text>
								<text class="item-text-content">{{item.areaName}}</text>
							</view>
							<view v-if="item.orderStatus==1" class="items-line">
								<uni-icons class="input-uni-icon" type="person" size="18" color="lightblue" />
								<text class="item-text-lable">运维公司管理员：</text>
								<text class="item-text-content">{{item.manageCompanyAdminName}}</text>
							</view>
							<view v-else class="items-line">
								<uni-icons class="input-uni-icon" type="person" size="18" color="lightblue" />
								<text class="item-text-lable">处理人：</text>
								<text class="item-text-content">{{item.contactUserName}}</text>
							</view>
							<view v-if="item.orderStatus==1" class="items-line">
								<uni-icons class="input-uni-icon" type="phone" size="18" color="lightblue" />
								<text class="item-text-lable">管理员联系电话：</text>
								<view v-if="item.manageCompanyAdminPhone != null" @click="call(item.manageCompanyAdminPhone)">
									<text class="item-text-content"
										style="color: skyblue;text-decoration-line: underline;">{{item.manageCompanyAdminPhone}}</text>
								</view>
							</view>
							<view v-else class="items-line">
								<uni-icons class="input-uni-icon" type="phone" size="18" color="lightblue" />
								<text class="item-text-lable">处理人联系电话：</text>
								<view v-if="item.contactPhone != null" @click="call(item.contactPhone)">
									<text class="item-text-content"
										style="color: skyblue;text-decoration-line: underline;">{{item.contactPhone}}</text>
								</view>
							</view>
							<view class="item-button-group">
								<view v-if="item.orderStatus==1" class="item-button" @click="call(item.manageCompanyAdminPhone)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="phone" size="18" color="coral" />
										<text class="button-text">电话</text>
									</view>
								</view>
								<view v-else class="item-button" @click="call(item.contactPhone)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="phone" size="18" color="coral" />
										<text class="button-text">电话</text>
									</view>
								</view>
								<view class="item-button" @click="onLocationClick(item)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="location" size="18" color="coral" />
										<text class="button-text">导航</text>
									</view>
								</view>
								<view v-if="permission.companyAdmin && item.orderClose == 0" class="item-button"
									@click="onCheckOrderChangeClick(item)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="compose" size="18" color="coral" />
										<text class="button-text">任务指派</text>
									</view>
								</view>
								<view v-if="(permission.admin || permission.orgAdmin) && item.orderClose == 0"
									class="item-button" @click="onManuallyCloseOrderClick(item.id)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="close" size="18" color="coral" />
										<text class="button-text">人工关闭</text>
									</view>
								</view>
								<view v-if="permission.companyAdmin && item.orderStatus==1" class="item-button"
									@click="onComfireCheckOrderClick(item.id)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="auth" size="18" color="coral" />
										<text class="button-text">接受任务</text>
									</view>
								</view>
								<view class="item-button" style="background-color:lightblue"
									@click="onDetailViewClick(item.id)">
									<view class="items-line">
										<uni-icons class="input-uni-icon" type="info" size="18" color="coral" />
										<text class="button-text">维修详情</text>
									</view>
								</view>
							</view>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
			<uni-group>
				<view class="pagination-block">
					<uni-pagination show-icon :pageSize="pageSize" :current="pageCurrent" :total="total"
						@change="pageChange" />
				</view>
			</uni-group>
		</view>
	</view>
</template>

<script>
	import {
		role
	} from "@/api/role.js";
	import http from '@/http/api.js';

	export default {
		components: {

		},
		onLoad(option) {
			if (this.userInfo.role_name === role['admin']) {
				this.permission.admin = true;
			} else if (this.userInfo.role_name === role['orgAdmin']) {
				this.permission.orgAdmin = true;
			} else if (this.userInfo.role_name === role['companyAdmin']) {
				this.permission.companyAdmin = true;
			} else if (this.userInfo.role_name === role['companyServciePerson']) {
				this.permission.companyServciePerson = true;
			}
			this.getRegionTree();
		},
		data() {
			return {
				title: '维修任务信息',
				permission: {
					'admin': false,
					'orgAdmin': false,
					'companyAdmin': false,
					'companyServciePerson': false,
				},
				searchorderstatus: ['0'],
				searchwarnstatus: [],
				orderStatusItems: [{
					'value': 0,
					'text': '全部'
				}, {
					'value': 1,
					'text': '未确认'
				}, {
					'value': 2,
					'text': '处理中'
				}, {
					'value': 3,
					'text': '已处理'
				}],
				orderStatusOption: 0,
				countInfo: {
					orderCount: 0,
					orderUnconfirm: 0,
					orderProcessing: 0,
					reportOrder: 0
				},
				warnKindItems: [{
					'value': 0,
					'text': '全部'
				}, {
					'value': 1,
					'text': '离线'
				}, {
					'value': 2,
					'text': '时钟异常'
				}, {
					'value': 3,
					'text': '雨量漏报'
				}, {
					'value': 4,
					'text': '水位漏报'
				}, {
					'value': 5,
					'text': '5分钟上报延时'
				}, {
					'value': 6,
					'text': '雨量小时上报延时'
				}, {
					'value': 7,
					'text': '水位小时上报延时'
				}, {
					'value': 8,
					'text': '雨量疑似异常值'
				}],
				warnKindOption: 0,
				selectUnconfirm: 0,
				selectProcess: 0,
				pageSize: 10,
				pageCurrent: 1,
				total: 0,
				list: [],
				regionTree: [],
				regionCode: '',
				selectedTreeNode: '',
				searchRtuNameVal: '',
				searchRtuCodeVal: '',
				query: {},
				inputStyles: {
					color: '#808080',
					borderColor: '#d3d3d3'
				}
			}
		},
		computed: {

		},
		onShow() {
			this.pageCurrent = 1;
			this.getPage()
		},
		methods: {
			//返回上一页
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			// 分页触发
			pageChange(e) {
				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();
					this.getCountInfo(this.regionCode);
				} else {
					this.regionCode = '';
					this.query = {};
					this.searchRtuNameVal = '';
					this.searchRtuCodeVal = '';

					this.pageCurrent = 1;
					this.getPage();
					this.getCountInfo(this.regionCode);
				}
			},
			//录入查询
			search() {
				this.pageCurrent = 1;
				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);
					}
				}
			},
			call(phone) {
				console.log(phone)
				uni.showModal({
					content: '是否需要拨打[' + phone + ']?',
					showCancel: true,
					success(res) {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: phone
							});
						}
					}
				});
			},
			onDetailViewClick(id) {
				let url = '/pages/check-order/orderprocessreportdetail?orderId=' + id;
				uni.navigateTo({
					url: url
				})
			},
			onCheckOrderChangeClick(item) {
				let contactUserName = '无';
				if (item.orderConfirm == 1) {
					contactUserName = item.contactUserName;
				}
				let url = '/pages/check-order/orderprocesschange?orderId=' + item.id + "&contactUserName=" +
					contactUserName;
				uni.navigateTo({
					url: url
				})
			},
			onLocationClick(item) {
				if (item.lat && item.lat) {
					let lat = item.lat;
					let lng = item.lng;
					let name = item.rtuName;
					let add = '';
					if (item.locationDesc) {
						add = item.locationDesc;
					}
					uni.openLocation({
						latitude: Number(lat),
						longitude: Number(lng),
						name: name,
						address: "",
						success() {}
					})
				} else {
					uni.showModal({
						content: '经纬度为空，请确认！',
						showCancel: true,
						success(res) {
							if (res.confirm) {

							}
						}
					});
				}
			},
			onManuallyCloseOrderClick(id) {
				uni.navigateTo({
					url: '/pages/check-order/manuallyclosecheckorder?id=' + id
				})
			},
			onComfireCheckOrderClick(id) {
				let url =
					'/pages/check-order/orderconfirm?id=' + id;
				uni.navigateTo({
					url: url
				})
			},
			orderStatusOptionChange: function(evt) {
				console.log(JSON.stringify(evt))
				console.log(this.orderStatusOption)
				this.pageCurrent = 1;
				let params = {};
				this.getPage(params);
			},
			warnKindOptionChange: function(evt) {
				let params = {};
				this.pageCurrent = 1;
				this.getPage(params);
			},
			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.regionCode = that.regionTree[0].id;
						that.selectedTreeNode = that.regionTree[0].id;
						that.query = {};
						that.query['adCode'] = that.regionCode;
						that.getPage();
						that.getCountInfo(that.regionCode);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getCountInfo(adCode) {
				let that = this;
				http.request({
					url: '/galaxy-business/rtu/check/order/count',
					method: 'GET',
					data: {
						adCode
					}
				}).then(res => {
					that.countInfo = res.data;
				})
			},
			getPage(params = {}) {
				if (this.orderStatusOption == 0) {
					params['orderClose'] = 0;
				} else if (this.orderStatusOption == 1) {
					params['orderStatus'] = 1;
					params['orderClose'] = 0;
				} else if (this.orderStatusOption == 2) {
					params['orderStatus'] = 2;
					params['orderClose'] = 0;
				} else if (this.orderStatusOption == 3) {
					params['orderStatus'] = 3;
					params['orderClose'] = 0;
				}
				if (this.warnKindOption == 0) {} else {
					params['warnKind'] = this.warnKindOption;
				}
				const current = this.pageCurrent;
				const size = this.pageSize;
				let postData = Object.assign(params, this.query);
				let that = this;
				http.request({
					url: '/galaxy-business/rtu/check/order/page',
					method: 'GET',
					params: {
						current,
						size,
					},
					data: postData,
				}).then(res => {
					if (res.data.records != null) {
						that.list = res.data.records;
					}
					this.total = res.data.total;
				}).catch(err => {
					console.log(err)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.select-checkbox {
		margin-top: 0px;
		margin-bottom: 5px;
		padding-left: 15px;
	}

	.count-info-line {
		padding-top: 5px;
		padding-bottom: 5px;
		padding-left: 10px;
		padding-right: 10px;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		flex-wrap: wrap;
	}

	.count-info-line .orderCount {

		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: dodgerblue;
			font-size: 0.7rem;
		}
	}

	.count-info-line .unconfirmOrderCount {

		margin-left: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: red;
			font-size: 0.7rem;
		}
	}

	.count-info-line .processOrderCount {
		margin-left: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: orange;
			font-size: 0.7rem;
		}
	}

	.count-info-line .reportOrderCount {
		margin-left: 0px;
		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: green;
			font-size: 0.7rem;
		}
	}
</style>