<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">

					
					<select name="" id="" filterable clearable v-model="router_id" style="padding-left:40rpx;background-size: 4.6%;">
						<option value="0">请选择线路名称</option>
						<option :value=item.route_name v-for="item in routeListData">{{ item.route_name}}</option>
					</select>
				</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="请输入站点名称" confirm-type="search" v-model="siteName"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-blue round" @click="searchSiteList">查询</button>
				</view>
			</view>
		</view>
		<!-- 筛选框end -->

		<!-- 站点列表start -->
		<view class="site-items" style="margin-top:0;height: calc(100vh - 500rpx);">
			<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">{{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>
		<!-- 站点列表end -->

		<!-- 新增按钮start -->
		<view class="plus">
			<image src="../../static/plus.png" style="width:125rpx;height:125rpx" @tap="goAddPage()"></image>
		</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:[]

			};
		},

		onLoad: function(option) {

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

		},

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

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

			//删除
			deleteItem(item) {
				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(() => {
						this.getSiteList()
					}, 1000);
				}

			},

			async getSiteList(ming = {}) {
				const res = await this.$myRequest({
					url: 'SiteManagement/getSiteList',
					showLoading: true,
					data: ming
				})
				this.siteListRes = 1;
				this.siteList = res.data.data
				console.log(res.data.data);
			},

			// 新增跳转
			goAddPage() {
				uni.navigateTo({
					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>
