<template>
	<view class="site-wrapper">
		<!-- 筛选框start -->
		<view style="height:200rpx"></view>
		<view class="ding">
			<view class="cu-bar search bg-gray filter-section" style="padding-top:10rpx">
				<view class="search-form round bg-white" style="position:relative;overflow:auto">
					<input name="siteName" v-model="formMess.siteName" @click="toggleShow" @focus="focusfns"
						@input="handleInput()" placeholder="请输入或选择所属线路"></input>
				</view>
			</view>
			<view class="cu-bar search bg-gray filter-section">
				<view class="search-form round bg-white">
					<text class="cuIcon-search"></text>
					<input class="" type="text" placeholder="请输入站点名称" v-model="siteName"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-blue round" @click="searchSiteList">查询</button>
				</view>
			</view>
		</view>

		<view class="siteItems" v-if="isShow" style="max-height:60vh;overflow:auto;position:absolute;z-index:999; margin: 0 32rpx;
    width: calc(100% - 64rpx);top:100rpx">
			<view class=" site-item" v-if="!siteItemData.length">暂无结果</view>
			<view class="site-item" v-for="(item,index) in siteItemData" @click="changeModel(item,index)" :key="index" style="padding:16rpx 22rpx">
				{{item.route_name}}
			</view>
		</view>
		<!-- 筛选框end -->

		<!-- 站点列表start -->
		<view class="site-items" style="margin-top:0;height: calc(100vh - 380rpx)">
			<view class="cu-list menu-avatar">
				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''"
					v-for="(item,index) in siteList" :key="index" @touchstart="ListTouchStart"
					@touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index">
					<view class="cu-avatar round lg" style="background-image: url(../../static/site-icon.png);"></view>

					<view class="content" @tap="goDeviceManage(item)">
						<view class="text-grey site-tit inOneLine" style="display:block">{{item.siteName}}</view>
					</view>

					<view class="nav-right num">
						<view class="text-grey text-xs">
							<image @click="mapLocation(item)" src="../../static/nav-icon.png"
								style="width:25rpx;height:25rpx;margin-right:26rpx"></image>
						</view>
					</view>
					<view class="move">
						<view class="bg-grey" @click.stop="editItem(item)">编辑</view>
						<view class="bg-red" @click.stop="deleteItem(item)">删除</view>
					</view>
				</view>
			</view>
			<view v-if="!siteList.length&&siteListRes==1" class="text-center margin-top"> 暂无数据</view>
			<view v-show="isLoadMore&&this.currentPage>1" style="padding-bottom:60px">
				<uni-load-more :status="loadStatus"></uni-load-more>
			</view>
		</view>
		<!-- 站点列表end -->

		<!-- 新增按钮start -->
		<view style="width: 100%;
    position: fixed;
    bottom: 0px;
    right: 0px;
    height: 64px;
    background: #fff;">
			<view class="plus">
				<image src="../../static/plus.png" style="width:100rpx;height:100rpx" @tap="goAddPage()"></image>
			</view>
		</view>
		
		<!-- 新增按钮end -->
	</view>

</template>
<!-- <script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
<script>

</script>
<script>
	//引入js sdk的封装
	import * as jwx from '../../util/jssdk.js'

	export default {
		data() {
			return {
				modalName: null,
				listTouchStart: 0,
				listTouchDirection: null,

				siteName: '',
				siteList: [],
				siteListRes: 0,

				router_id: 0,
				routeListData: [],


				route_name: '',
				isShow: false,
				formMess: {
					"siteName": "",
				},

				siteItemDataOrigin: [],
				siteItemData: [],
				
				currentPage: 1,
				size: 10,
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				isLoadMore: false, //是否加载中
				
				deleteStatus:false

			};
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.currentPage += 1;
				this.loadData()
			}
		},

		onLoad: function(option) {

			//站点列表渲染
			this.loadData()
			this.getRoutrBox()

		},

		methods: {
			
			//筛选
			searchSiteList() {
				this.siteList=[];
				this.currentPage = 1;
				this.siteListRes = 0;
				this.loadData()
			},
			loadData(){
				this.getSiteList({
					"siteName": this.siteName,
					"route_name": this.formMess.siteName,
					currentPage: this.currentPage,
					size: this.size,
				})
			},

			//线路名称下拉数据请求
			async getRoutrBox(params = {}) {
				const res = await this.$myRequest({
					url: 'Archives/getRoutrBox',
					showLoading: true,
					data: params
				})
				// this.routeListData = res.data.data;
				// console.log(this.routeListData)

				this.siteItemData = res.data.data;
				this.siteItemDataOrigin = res.data.data;
				this.siteItemData = this.siteItemDataOrigin;
			},
			//选中下拉选项，隐藏框，赋值
			changeModel: function(item, index) {
				this.isShow = false;
				this.formMess.siteName = item.route_name;
			},

			// 输入框点击 显示隐藏下拉框
			toggleShow: function() {
				this.isShow = !this.isShow;
			},
			//点击下拉筛选
			handleInput() {
				//将元素根据搜索内容过滤出来  这里改变了siteItemData
				this.siteItemData = this.siteItemDataOrigin;
				this.siteItemData = this.siteItemData.filter(item => item.route_name.indexOf(this.formMess.siteName) > -1);
			},
			focusfns: function(e) {
				//将元素根据搜索内容过滤出来  这里改变了siteItemData
				this.siteItemData = this.siteItemDataOrigin;
				this.siteItemData = this.siteItemData.filter(item => item.route_name.indexOf(this.formMess.siteName) >
					-1);
			},

			

			//编辑
			editItem(item) {
				uni.redirectTo({
					url: '/pages/siteAdd/siteAdd?id=' + item.id + '',
				});
			},

			//删除
			deleteItem(item) {
				this.deleteStatus=true
				uni.showModal({
					title: '确认删除吗？',
					content: '',
					success: function(result) {
						if (result.confirm) {
							this.setDelSite({
								"id": item.id
							})
						} else if (result.cancel) {
							// console.log('用户点击取消');
						}
					}.bind(this)
				});
			},

			// 打开导航
			mapLocation(item) {

				//h5 地图导航
				// window.location.href =  'http://apis.map.qq.com/uri/v1/marker?marker=coord:'+parseFloat(item.latitude)+','+parseFloat(item.longitude)+'' 

				//app导航
				// uni.openLocation({
				// 	latitude: parseFloat(item.latitude),
				// 	longitude: parseFloat(item.longitude),
				// 	success: function() {
				// 		// console.log('success');
				// 	}
				// });

				// 微信公众号导航
				jwx.configWeiXin(jweixin => {
					let shareInfo = {
						latitude: parseFloat(item.latitude), // 纬度，浮点数，范围为90 ~ -90
						longitude: parseFloat(item.longitude), // 经度，浮点数，范围为180 ~ -180。
						name: item.siteName, // 位置名
						address: item.address, // 地址详情说明
						scale: 15, // 地图缩放级别,整型值,范围从1~28。默认为最大
					};
					jweixin.openLocation(shareInfo);
				});
			},

			async setDelSite(ming = {}) {
				const res = await this.$myRequest({
					url: 'SiteManagement/setDelSite',
					data: ming
				})
				if (!res.data.flag) {
					uni.showToast({
						title: "删除失败",
						icon: "none"
					});
				} else {
					uni.showToast({
						title: "删除成功",
					});
					setTimeout(() => {
						console.log(this.siteList)
						// this.siteList=[]
						this.loadData()
					}, 1000);
				}

			},

			async getSiteList(ming = {}) {
				const res = await this.$myRequest({
					url: 'SiteManagement/getSiteList',
					showLoading: true,
					data: ming
				})
				this.siteListRes = 1;
				
				
				if(this.deleteStatus){
					this.siteList=[];
					this.deleteStatus=false;
				}
				if (res.data.total) {
					this.siteList = this.siteList.concat(res.data.data)
					console.log('this.siteList')
					console.log(this.siteList)
					if (res.data.data.length < this.size) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
						this.isLoadMore = true
						this.loadStatus = 'nomore'
					} else {
						this.isLoadMore = false
					}
				} else {
					this.isLoadMore = true
					this.loadStatus = 'nomore'
				}
			},

			// 新增跳转
			goAddPage() {
				uni.redirectTo({
					url: '/pages/siteAdd/siteAdd',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},

			//跳转到设备管理页面
			goDeviceManage(item) {
				uni.navigateTo({
					url: '/pages/deviceManage/deviceManage?siteId=' + item.id + '',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},

			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},

			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart < -80 ? 'left' : 'right'
			},

			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			}
		}

	}
</script>

<style>

</style>
