<template>
	<view class="choose-network-container">
		<!-- 筛选弹框 -->
		<search-modal v-if="openScreen" :visible="openScreen" :defaultParams="searchForm" @refresh="refresh" @close="openScreen=false"></search-modal>
		<view class="choose-network-header">
			<view class="checked-all">
				<u-checkbox
					v-if="listdata.length>0"
					class="checked-all-c"
					active-color="#01c9b2"
					@change="checkedAll" 
					v-model="isCheckedAll"
					label-size="26"
					name="checkedAll"
				>全选</u-checkbox>
			</view>
			<view class="choose-checked" @tap="openScreen=true">
				已选：<text>{{checkedList.length}}</text>
				<image class="filter-icon" src="@/static/filter.png"></image>
			</view>
		</view>
		<scroll-view class="scroll-con" scroll-y :scroll-top="scrollTop" @scrolltolower="onreachBottom">
			<!-- 列表数据 -->
			<u-checkbox-group class="cell-item-con">
				<view class="cell-item" v-for="(item, index) in listdata" :key="index">
					<u-checkbox
						class="cell-item-checkbox"
						@change="checkboxChange($event, index)"
						active-color="#01c9b2"
						v-model="item.checked"
						:name="item.id">
					</u-checkbox>
					<view class="cell-item-c" @tap="goPage(item)">
						<view class="cell-item-info">
							<view class="cell-item-info-network">
								{{item.stationName || '--'}}
								<u-badge type="success" bgColor="#c90101" :count="item.unCleanNum" :offset="[-10,-32]" size="mini"></u-badge>
							</view>
							<view class="cell-item-info-weight">
								<view class="cell-item-weight-bar">
									{{ item.totalWeight }}kg
									<u-badge class="badge" type="primary" bgColor="#01c9b2" count="总" :offset="[0,0]"></u-badge>
								</view>
								<view class="cell-item-weight-bar">
									{{ item.maxWeight }}kg
									<u-badge class="badge" type="warning" bgColor="#05695d" count="最大" :offset="[0,0]"></u-badge>
								</view>
							</view>
						</view>
						<u-icon name="arrow-right" class="arrow-right" :size="28" color="#999"></u-icon>
					</view>
				</view>
			</u-checkbox-group>
			<u-empty class="nodata" :text="noDataText" v-if="listdata.length==0 && status!='loading'" mode="list"></u-empty>
			<view class="loadmore" v-if="listdata.length!=0">
				<u-loadmore v-if="total>pageSize || status=='loading'" :status="status" />
			</view>
		</scroll-view>
		<view class="footer-bar">
			<u-button class="footer-handle-btn handle-btn" hover-class="none" :custom-style="btnStyle" shape="circle" @click="handleAdd" size="medium" type="primary">
				<text>加入待清运清单</text>
			</u-button>
		</view>
	</view>
</template>

<script>
	import searchModal from '@/pages/cleared/searchModal.vue'
	import { waitToCleanList } from '@/api/cleared.js'
	import { cleanStationAdd } from '@/api/unclear.js'
	export default{
		components: { searchModal },
		data(){
			return{
				listdata: [],
				pageNo: 1,  //  当前页码
				pageSize: 40,  //  每页条数
				total: 0,  //  数据总条数
				noDataText: '暂无数据',  //  列表请求状态提示语
				status: 'loadmore',  //  加载中状态
				scrollTop: 0,  //  滚动条位置
				isCheckedAll: false,  //  全选状态
				checkedNum: 0,  //  已选个数
				checkedList: [],  //  已选清运网点
				openScreen: false, //  是否打开筛选
				searchForm: {
					//  查询条件
					stationName: '',  //  网点名称
					orderType: 'max', // 排序方式
				},
				btnStyle: {  //  自定义按钮样式
					borderColor: '#07c160',
					backgroundColor: '#07c160',
					width: '100%'
				},
			}
		},
		onShow() {
			this.openScreen = false  //  关闭筛选框
			this.refresh(this.searchForm)
		},
		methods: {
			//  加入清运单
			handleAdd(){
				if(this.checkedList.length > 0){
					uni.showLoading({
						mask: true,
					    title: '加载中'
					})
					cleanStationAdd({ stationNoList: this.checkedList }).then(res => {
						uni.hideLoading()
						if(res.status == 200){
							uni.showToast({ icon:'none', title: res.message })
							setTimeout(()=>{
								uni.navigateBack()
							}, 1000)
						}else{
							uni.showToast({ icon:'none', title: res.message })
						}
					})
				}else{
					uni.showToast({ icon:'none', title: '请勾选需清运的网点' })
				}
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e,ind) {
				this.listdata[ind].checked = e.value
				let len = 0
				this.checkedList = []
				this.listdata.map(item => {
					if(item.checked){
						len++
						this.checkedList.push(item.stationNo)
					}
				})
				this.checkedNum = len
			},
			// 全选
			checkedAll(e) {
				this.isCheckedAll = e
				let len = 0
				this.checkedList = []
				this.listdata.map(val => {
					val.checked = e.value ? true : false
					if(val.checked){
						len++
						this.checkedList.push(val.stationNo)
					}
				})
				this.checkedNum = len
			},
			// 获取查询参数 刷新列表
			refresh(params){
				this.searchForm = params
				this.listdata = []
				this.total = 0
				this.isCheckedAll = false
				this.checkedList = []
				this.searchHandle(1)
			},
			// 搜索查询
			searchHandle (pageNo) {
				uni.showLoading({
					mask: true,
				    title: '加载中'
				})
				this.status = "loading"
				pageNo ? this.pageNo = pageNo : null,
				waitToCleanList({
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					stationName: this.searchForm.stationName,
					orderType: this.searchForm.orderType
				}).then(res => {
					uni.hideLoading()
					if (res.status == 200) {
						res.data.list.map(item => {
							item.checked = false
							item.totalWeight = (item.totalWeight/1000).toFixed(2)
							item.maxWeight = (item.maxWeight/1000).toFixed(2)
						})
						if(this.pageNo>1){
							this.listdata = this.listdata.concat(res.data.list || [])
						}else{
							this.listdata = res.data.list || []
							this.scrollTop = 0
						}
						this.total = res.data.count || 0
						this.noDataText = '暂无数据'
					} else {
						this.noDataText = res.message
						this.listdata = []
						this.total = 0
					}
					this.status = "loadmore"
				})
			},
			//  查看详情
			goPage(record){
				uni.navigateTo({
				    url: '/pages/cleared/network?stationNo=' + record.stationNo
				})
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				if(this.listdata.length!=0 && this.listdata.length < this.total){
					this.pageNo += 1
					this.searchHandle()
				}else{
					uni.showToast({ title: '已经到底了', icon: 'none' })
					this.status = "nomore"
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	.choose-network-container {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		padding-top: 80rpx;
		padding-bottom: 102rpx;
		position: relative;
		// 顶部固定
		.choose-network-header{
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 $uni-spacing-row-base;
			box-sizing: border-box;
			background-color: #fff;
			line-height: 80rpx;
			font-size: 26rpx;
			.checked-all{
				.checked-all-c{
					font-size: 26rpx;
				}
			}
			.choose-checked{
				text{
					color: red;
				}
				.filter-icon{
					width: 36rpx;
					height: 36rpx;
					padding-left: 30rpx;
					vertical-align: text-top;
				}
			}
		}
		//  列表
		.scroll-con{
			flex: 1;
			overflow: auto;
		}
		//  列表样式
		.cell-item-con{
			.cell-item{
				margin: $uni-spacing-col-base $uni-spacing-row-base;
				background-color: #fff;
				border-radius: $uni-border-radius-base;
				color: $uni-text-color;
				font-size: $uni-font-size-base;
				box-shadow: 3rpx 3rpx 5rpx #eee;
				display: flex;
				align-items: center;
				padding: 40rpx 20rpx;
				// 多选框
				.cell-item-checkbox{
					flex-shrink: 0;
				}
				.cell-item-c{
					flex-grow: 1;
					display: flex;
					justify-content: space-around;
					align-items: center;
					.cell-item-info{
						flex-grow: 1;
						.cell-item-info-network{
							word-break: break-all;
							margin-bottom: 26rpx;
							display: inline-block;
							position: relative;
						}
						.cell-item-info-weight{
							font-size: 24rpx;
							display: flex;
							align-items: center;
							justify-content: space-between;
							.cell-item-weight-bar{
								position: relative;
								display: inline-block;
								padding-right: 50rpx;
								&:nth-child(2){
									padding-right: 76rpx;
								}
							}
						}
					}
					.arrow-right{
						flex-shrink: 0;
						margin-left: 64rpx;
					}
				}
			}
		}
		.footer-bar{
			width: 100%;
			height: 102rpx;
			padding: 0 10%;
			position: fixed;
			bottom: 0;
			left: 0;
			text-align: center;
			.footer-handle-btn{
				display: block;
				.btn-icon{
					margin-right: 10rpx;
				}
			}
			.handle-btn{
				margin-top: 18rpx;
				margin-bottom: 20rpx;
			}
		}
		.nodata{
			padding-top: 400rpx;
		}
	}
</style>
