<template>
	<view class="container">
		<!-- 顶部选择校区区域 -->
		<view class="top">
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#6FB6B8"></uni-segmented-control>
		</view>

		<view class="gap"></view>

		<!-- 主体内容区域 -->
		<view class="body">
			<!-- 左边区域 -->
			<view class="body_left">
				<view class="body_left_item" :class="{ active: active_left === index }" v-for="(item, index) in leftList" :key="index" @click="handleChange(index)">
					{{ item }}
				</view>
			</view>

			<!-- 右边区域 -->
			<view class="body_right">
				<view class="body_right_item" :class="{ active: active_right === index }" v-for="(item, index) in rightList" :key="index" @click="handleChange2(index)">
					{{ item.name }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
var QQMapWX = require('../util/qqmap-wx-jssdk1.1/qqmap-wx-jssdk')
var qqmapsdk
export default {
	data() {
		return {
			// 顶部分段器选项数组
			items: [],
			// 分段器当前索引
			current: 0,
			// 校区ID
			schoolId: 0,
			// 楼栋Id
			buildId: null,
			// 地址Id
			addressId: null,
			// 左边分类数组
			leftList: [],
			// 左边分类数组当前索引
			active_left: 0,
			// 右边分类数组
			rightList: [],
			// 右边分类数组当前索引
			active_right: null,
			// 报修区域数组
			areaTreeList: [],
			// 校区数组
			schoolList: [],
			// 用户定位经度
			myLng: 0,
			// 用户定位纬度
			myLat: 0
		}
	},
	onLoad(options) {
		// 实例化API核心类
		qqmapsdk = new QQMapWX({
			// 申请的key
			key: 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
		})
		if (options.schoolId) {
			this.schoolId = options.schoolId
			this.buildId = options.buildId
			this.addressId = options.addressId
		}
		this.getSchoolList()
	},
	methods: {
		// 获取当前定位位置信息
		getLocationData() {
			uni.showLoading({
				title: '定位中，请稍后',
				mask: true
			})
			qqmapsdk.reverseGeocoder({
				success: (res) => {
					// console.log(res)
					if (res.status === 0) {
						let address = res.result.address
						// console.log(address)
						if (address.indexOf('南昌市') !== -1) {
							this.current = 0
						} else {
							this.current = 1
						}
						this.schoolId = this.schoolList[this.current].id
						this.getRepairAreaTree()
					} else {
						uni.showToast({
							title: '请求定位失败',
							icon: 'none'
						})
					}
				},
				fail: (error) => {
					uni.showModal({
						title: '提示',
						content: '请先开启定位权限，否则将无法使用定位功能',
						cancelText: '不授权',
						confirmText: '授权',
						success: function (res) {
							if (res.confirm) {
								uni.openSetting({
									success(res) {
										this.getLocationData()
									}
								})
							} else if (res.cancel) {
							}
						}
					})
				}
			})
		},
		// 获取校区数据
		async getSchoolList() {
			const res = await this.$myRequest_repairs({
				url: '/repairArea/queryRepairSchools'
			})
			// console.log(res)
			if (res.code === '200') {
				this.schoolList = res.data
				// 获取之前选择过的校区
				this.schoolList.forEach((ele, index) => {
					if (ele.id == this.schoolId) {
						this.current = index
					}
				})
				this.items = this.schoolList.map((ele) => ele.name)
				let schoolAddress = uni.getStorageSync('schoolAddress')
				if (schoolAddress) {
					this.items.forEach((ele, index) => {
						if (ele === schoolAddress) {
							this.current = index
						}
					})
					this.schoolId = this.schoolList[this.current].id
					this.getRepairAreaTree()
				} else {
					this.getLocationData()
				}
			}
		},
		// 获取区域数据
		async getRepairAreaTree() {
			const res = await this.$myRequest_repairs({
				url: '/repairArea/queryRepairAreaTree',
				data: {
					schoolId: this.schoolId
				}
			})
			// console.log(res)
			if (res.code === '200') {
				this.areaTreeList = res.data
				// 获取之前选择过的楼栋
				this.areaTreeList.forEach((ele, index) => {
					if (ele.id == this.buildId) {
						this.active_left = index
					}
				})
				this.leftList = this.areaTreeList.map((ele) => ele.name)
				this.rightList = this.areaTreeList[this.active_left].children || []
				// 获取之前选择过的地址
				this.rightList.forEach((ele, index) => {
					if (ele.id == this.addressId) {
						this.active_right = index
					}
				})
			}
		},
		// 顶部分段器切换选项回调
		onClickItem(e) {
			this.active_left = 0
			this.active_right = null
			this.buildId = null
			this.addressId = null
			if (this.current != e.currentIndex) {
				this.current = e.currentIndex
				this.schoolId = this.schoolList[this.current].id
				this.getRepairAreaTree()
			}
		},
		// 左边数组切换回调
		handleChange(index) {
			this.active_left = index
			this.active_right = null
			this.rightList = this.areaTreeList[this.active_left].children || []
		},
		// 右边数组切换回调
		handleChange2(index) {
			this.active_right = index
			const repairsArea = this.items[this.current] + this.leftList[this.active_left] + this.rightList[this.active_right].name
			uni.setStorageSync('schoolAddress', this.items[this.current])
			uni.$emit('addRepairsArea', {
				data: repairsArea,
				schoolId: this.schoolId,
				buildId: this.areaTreeList[this.active_left].id,
				addressId: this.rightList[this.active_right].id
			})
			uni.navigateBack(1)
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	width: 100vw;
	height: 100vh;

	.top {
		height: 100rpx;
	}

	.gap {
		height: 10rpx;
		background-color: #f2f2f2;
	}

	.body {
		display: flex;
		height: calc(100vh - 110rpx);

		.body_left {
			box-sizing: border-box;
			padding: 15rpx;
			width: 198rpx;
			border-right: 1rpx solid #ccc;
			overflow-y: auto;

			.body_left_item {
				width: 100%;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.active {
				color: #6fb6b8;
			}
		}

		.body_right {
			box-sizing: border-box;
			padding: 30rpx 40rpx;
			width: 552rpx;
			overflow-y: auto;

			.body_right_item {
				float: left;
				box-sizing: border-box;
				margin: 0 12rpx 28rpx 0;
				padding: 10rpx 30rpx;
				height: 50rpx;
				line-height: 30rpx;
				text-align: center;
				color: #808080;
				font-size: 28rpx;
				border-radius: 53rpx;
				background-color: #e6e6e6;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.active {
				color: #fff;
				background-color: #6fb6b8;
			}
		}
	}
}

::v-deep .segmented-control {
	height: 100rpx;
}
</style>
