<template>
	<view class="container">
		<view class="page">
			<view class="search">
				<uni-search-bar placeholder="请输入设备管理员" bgColor="#EEEEEE" @confirm="search" @cancel="cancel" />
				<view class="list">
					<view class="item" @click="onChangeUser(item)" v-for="item in houseList" :key="item.platformCode">
						<image class="avatar" :src="item.employeeImage"></image>
						<text class="name">{{item.employeeName}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getEmployeeListApi,
		editCommunityComplaintApi
	} from '@/config/api.js'
	let maintenanceId = ''
	let houseAllList = []
	import serverConfig from "@/config/server_config.js";
	export default {
		data() {
			return {
				houseList: []
			}
		},
		methods: {
			async getPersonList() {
				let phone = uni.getStorageSync('user_phone')
				let {result} = await getEmployeeListApi()
				let houseList = result.filter(item=>item.employeePhone !== phone)
				houseList = houseList.map(item=> {
					return {
						...item,
						employeeImage: `${serverConfig.imageURL}${item.employeeAvatar}`
					}
				})
				houseAllList = [...houseList]
				this.houseList = [...houseList]
			},
			search(value) {
				this.houseList = houseAllList.filter(item=> {
					return item.employeeName.includes(value.value)
				})
			},
			cancel() {
				this.houseList = [...houseAllList]
			},
			onChangeUser(row) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: `确认转单给${row.employeeName}嘛？`,
					success: function (res) {
						if (res.confirm) {
							_this.onTransferOrder(row)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			async onTransferOrder(row) {
				await editCommunityComplaintApi({
					id: maintenanceId,
					maintainerId: row.id,
					maintenanceName: row.employeeName,
					maintenancePhone: row.employeePhone
				})
				setTimeout(()=> {
					uni.showToast({
						title: '转单成功！',
						icon: 'none'
					});
					setTimeout(()=> {
						uni.navigateBack();
					}, 1500)
				}, 30)
				var pages = getCurrentPages();
				var page = pages[pages.length - 2];
				page.$vm.onChangeTab('waitReceive')
			}
		},
		onLoad(options) {
			maintenanceId = options.id
			this.getPersonList()
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 15rpx;
		background-color: #ffffff;
	}

	.search {
		.list {
			padding:20rpx 30rpx;
			.item {
				height: 84rpx;
				line-height: 84rpx;
				border-bottom: 1px solid #F8F6F9;
				vertical-align: middle;
				.avatar {
					width: 48rpx;
					height: 48rpx;
					margin-right: 20rpx;
					vertical-align: middle;
				}
				.name {
					font-size: 28rpx;
					font-weight: 400;
					color: #373737;
					vertical-align: middle;
				}
			}
		}
	}
</style>