<!--
 * @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">
			<view v-if="permission.orgAdmin || (permission.companyServciePerson && postName=='servicePerson')"
				class="view-flex-inline-center">
				<view style="width: 90%;margin-top: 20px;margin-bottom: 20px;">
					<button type="warn" @click="onCreateOrderClick">立即发起工单</button>
				</view>
			</view>
			<uni-section title="工单查询" type="circle">
				<view class="search-block">
					<view style="margin-top: 10px;padding-left: 0px;padding-right: 0px;">
						<view class="select-checkbox">
							<uni-data-checkbox v-model="orderStatusOption" :localdata="orderStatusItems">
							</uni-data-checkbox>
						</view>
					</view>
					<view class="submit-btn">
						<button type="default" @click="search">查 询</button>
					</view>
				</view>
			</uni-section>
			<uni-section title="工单列表信息" type="circle">
				<uni-list>
					<uni-list-item v-for="item in list" :key="item.id" direction="column">
						<template v-slot:body>
							<view class="list-item-block">
								<view class="line">
									<uni-icons class="input-uni-icon" type="gear" size="18" color="lightblue" />
									<view v-if="item.orderType==1" class="text" style="color: gray;">工单类型:<span
											style="margin-left: 5px;color: cornflowerblue;">维修工单</span></view>
									<view v-else-if="item.orderType==2" class="text" style="color: gray;">工单类型:<span
											style="margin-left: 5px;color: cornflowerblue;">服务工单</span></view>
									<view v-else></view>
								</view>
								<view class="line">
									<uni-icons class="input-uni-icon" type="refresh" size="18" color="lightblue" />
									<view class="text" style="color: gray;">工单状态:<span
											style="margin-left: 10px;color: cornflowerblue;">{{item.orderStatusName}}</span>
									</view>
								</view>
								<view class="line">
									<uni-icons class="input-uni-icon" type="personadd" size="18" color="lightblue" />
									<view class="text" style="color: gray;">工单发起人:<span
											style="margin-left: 10px;color: cornflowerblue;">{{item.createOrderPersonName}}</span>
									</view>
								</view>
								<view class="line">
									<uni-icons class="input-uni-icon" type="calendar" size="18" color="lightblue" />
									<view class="text" style="color: gray;">发起时间:<span
											style="margin-left: 10px;color: cornflowerblue;">{{item.createTime}}</span>
									</view>
								</view>
								<view v-if="item.orderType==1" class="line">
									<uni-icons class="input-uni-icon" type="help" size="18" color="lightblue" />
									<view class="text text-ellipsis" style="color: gray;width: 90%;"
										@click="toOrderProcessDetail(item)">故障问题描述:<span class="text-underline"
											style="margin-left: 10px;color: cornflowerblue;">{{item.orderDesc}}</span>
									</view>
								</view>
								<view v-else class="line">
									<uni-icons class="input-uni-icon" type="help" size="18" color="lightblue" />
									<view class="text text-ellipsis" style="color: gray;width: 90%;"
										@click="toOrderProcessDetail(item)">服务内容描述:<span class="text-underline"
											style="margin-left: 10px;color: cornflowerblue;">{{item.orderDesc}}</span>
									</view>
								</view>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
			</uni-section>
			<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: {},
		data() {
			return {
				title: '运维工单',
				permission: {
					'admin': false,
					'orgAdmin': false,
					'companyAdmin': false,
					'companyServciePerson': false,
				},
				postName: '',
				searchorderstatus: ['0'],
				searchwarnstatus: [],
				orderStatusItems: [{
					'value': 0,
					'text': '全部'
				}, {
					'value': 2,
					'text': '待处理工单'
				}, {
					'value': 4,
					'text': '费用待审批工单'
				}, {
					'value': 6,
					'text': '完结待审批工单'
				}, {
					'value': 7,
					'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: {

		},
		onLoad(option) {
			this.permission.admin = false;
			this.permission.orgAdmin = false;
			this.permission.companyAdmin = false;
			this.permission.companyServciePerson = false;
			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;
				if (this.userInfo.post_id == '1706859505948098562') {
					this.postName = "engineer";
				} else if (this.userInfo.post_id == '1730535542909140993') {
					this.postName = "servicePerson";
				}
			}
			this.pageCurrent = 1;
			this.getPage()
		},
		onShow() {
			this.permission.admin = false;
			this.permission.orgAdmin = false;
			this.permission.companyAdmin = false;
			this.permission.companyServciePerson = false;
			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;
				if (this.userInfo.post_id == '1706859505948098562') {
					this.postName = "engineer";
				} else if (this.userInfo.post_id == '1730535542909140993') {
					this.postName = "servicePerson";
				}
			}
		},
		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);
				}
			},
			onCreateOrderClick() {
				let url = '/pages/check-order/orderadd';
				uni.navigateTo({
					url: url
				})
			},
			toOrderProcessDetail(item) {
				let url = '/pages/check-order/processcheckorderlistview?orderType=' + item.orderType + '&orderId=' + item
					.id + '&orderClose=' + item.orderClose + '&orderRate=' + item.orderRate;
				uni.navigateTo({
					url: url
				})
			},
			//录入查询
			search() {
				this.pageCurrent = 1;
				let params = {};
				params['orderStatusOption'] = this.orderStatusOption;
				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 = {}) {
				const current = this.pageCurrent;
				const size = this.pageSize;
				let postData = Object.assign(params, this.query);
				let that = this;
				http.request({
					url: '/galaxy-business/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;
	}
</style>