<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="" @focus="InputFocus" v-model="siteName" @blur="InputBlur" :adjust-position="false"
						type="text" placeholder="请输入站点"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-blue round" @click="searchData()">查询</button>
				</view>

			</view>
			
			

		</view>
		
		<view class="siteItems" v-if="isShow2" 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 -->

		<view style="height: calc(100vh - 290rpx);">
			<view class="cu-list menu-avatar">
				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''"
					v-for="(item,index) in siteArchiveData" :key="index" :data-target="'move-box-' + index"
					@tap="goArchiveDetail(item.id)" @touchstart="ListTouchStart" @touchmove="ListTouchMove"
					@touchend="ListTouchEnd">
					<view class="cu-avatar round lg" style="background-image:url(../../../static/archiveIcon.png)">
					</view>
					<view class="content">
						<view class="pro-title">
							<view class="cut">{{item.site_name}} （现场档案）</view>
						</view>
					</view>
					<view class="nav-right num">
						<view class="text-grey">
							<text class="icon iconfont margin-right-sm margin-left-lg">&#xe629;</text>
						</view>
					</view>

					<view class="move" style="width: 66px;">
						<view class="bg-grey" @click.stop="editItem(item)">编辑</view>
					</view>
				</view>
			</view>
			<view v-if="!siteArchiveData.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>

		<!-- 新增按钮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>
	export default {
		data() {
			return {
				modalName: null,
				siteArchiveData: [],

				siteName: '',
				siteListRes: 0,

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

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

			this.loadData()
			this.getRoutrBox()
		},

		methods: {
			//新线名称下拉数据请求
			async getRoutrBox(params = {}) {
				const res = await this.$myRequest({
					url: 'Archives/getRoutrBox',
					showLoading: true,
					data: params
				})
				this.siteItemData = res.data.data;
				this.siteItemDataOrigin = res.data.data;
				this.siteItemData = this.siteItemDataOrigin;
			},
			//选中下拉选项，隐藏框，赋值
			changeModel: function(item, index) {
				this.isShow2 = false;
				this.formMess.siteName = item.route_name;
			},
			
			// 输入框点击 显示隐藏下拉框
			toggleShow: function() {
				this.isShow2 = !this.isShow2;
			},
			//点击下拉筛选
			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);
			},
			
			
			
			

			searchData() {
				this.currentPage=1,
				this.siteArchiveData=[],
				this.siteListRes = 0;
				this.loadData()
				
			},
			loadData(){
				this.getDataList({
					"site_name": this.siteName,
					"route_name": this.formMess.siteName,
					currentPage: this.currentPage,
					size: this.size,
				})
				
			},

			InputFocus(e) {
				this.InputBottom = e.detail.height
			},
			InputBlur(e) {
				this.InputBottom = 0
			},

			//编辑
			editItem(item) {
				uni.redirectTo({
					url: '/pages/siteArchive/siteArchiveAdd/siteArchiveAdd?id=' + item.id + '',
				});
			},
			//数据请求
			async getDataList(params = {}) {
				const res = await this.$myRequest({
					url: 'Archives/getArchivesList',
					showLoading: true,
					data: params
				})
			
				this.siteListRes = 1;

				if (res.data.total) {
					this.siteArchiveData = this.siteArchiveData.concat(res.data.data)
					if (res.data.data.length < this.size) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
						this.isLoadMore = true
						this.loadStatus = 'nomore'
					} else {
						this.isLoadMore = false
					}
					console.log('this.siteArchiveData')
					console.log(this.siteArchiveData)
				} else {
					
					this.isLoadMore = true
					this.loadStatus = 'nomore'
				}
				
				
			},

			// 页面跳转
			goArchiveDetail(id) {
				uni.navigateTo({
					url: '/pages/siteArchive/archiveDetail/archiveDetail?id=' + id,
				});
			},
			goAddPage(item) {
				uni.redirectTo({
					url: '/pages/siteArchive/siteArchiveAdd/siteArchiveAdd',
				});
			},


			// 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 lang="scss">
	.cu-item {
		height: 160rpx !important
	}

	.cu-list.menu-avatar>.cu-item .content {
		left: 110rpx;
	}

	.cu-list>.cu-item.move-cur {
		-webkit-transform: translateX(-66px);
	}
</style>
