<template>
	<view class="site-wrapper">


		<!-- 筛选框start -->
		<view style="height:90rpx"></view>
		<view class="ding">
			<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" @blur="InputBlur" :adjust-position="false" type="text"
					 v-model="phoneNumber"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-blue round" @click="searchAuthList">查询</button>
				</view>
			</view>
		</view>
		<!-- 筛选框end -->

		<!-- 权限管理start -->
		<view class="site-items" style="margin-top:0;height: calc(100vh - 286rpx);">
			<view class="cu-list menu-avatar">

				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''" v-for="(item,index) in authList" :key="index"
				 @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index">
					<view class="cu-avatar round lg" style="background-image: url(../../static/auth-icon.jpg);"></view>
					<view class="content">
						<view class="text-grey site-tit">{{item.phone}}</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="!authList.length&&authListRes==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(type)"></image>
			</view>
		</view>
		
		<!-- 新增按钮end -->
	</view>

</template>

<script>
	import json from '../../data/json.js';

	export default {
		data() {
			return {
				phoneNumber: '',
				authListRes: 0,
				authList: [],
				type: '0',
				siteListData: json.siteListData,
				modalName: null,
				listTouchStart: 0,
				listTouchDirection: null,
				CustomBar: this.CustomBar,
				csListArrl: [],
				//左滑默认宽度
				delBtnWidth: 160,
				flag: false,
				
				currentPage: 1,
				size: 10,
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				isLoadMore: false, //是否加载中


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

		onLoad: function(option) {
			this.loadData()
		},
		methods: {
			
			searchAuthList() {
				this.authList=[];
				this.currentPage = 1;
				this.authListRes=[]
				this.loadData()
				
			},
			loadData(){
				this.getAuthData({
					"phone": this.phoneNumber,
					currentPage: this.currentPage,
					size: this.size,
				})
				
			},
			
			EditItem(item){
				uni.navigateTo({
					url: '/pages/authAdd/authAdd?phone='+item.phone+'',
				
				});
			},
			  deleteItem(item) {
				 let deleteT=0;
				 uni.showModal({	
				     title: '确认删除吗？',
				     content: '',
				     success: function (result) {
				         if (result.confirm) {
							  this.deleteAuth({
							 	"phone": item.phone
							 });
				         } else if (result.cancel) {
				             console.log('用户点击取消');
				         }
				     }.bind(this)
				 }); 
			},


			// 删除请求接口
			async deleteAuth(ming = {}) {
				let res = await  this.$myRequest({
					url: 'AuthorityManagement/setDelAuthority',
					data: ming
				})
				if (!res.data.flag) {
					alert('删除失败');
					return;
				}
				this.loadData()
				
				return res;
			},


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

			



			// 页面跳转
			goAddPage(type) {
				uni.navigateTo({
					url: '/pages/authAdd/authAdd',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},


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

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

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

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

	}
</script>
