<template>
	<view class="main">
		<view class="top_row">
			<view class="row selectAll" @click="chooseAll">
				<view class="icon ">
					<image src="@/static/images/icon (22).png" mode="" v-if="isAll"></image>
					<image src="@/static/images/icon (20).png" mode="" v-else></image>
				</view>
				<view class="txt">全选</view>
			</view>
			<view class="searchBox">
				<searchRow placeholder="名称/IP过滤" @searchFn="searchFn"/>
			</view>
		</view>
		<view class="card">
			<view class="row" v-for="(item,index) in list" :key="index" @click="chooseItem(item)">
				<view class="icon">
					<image src="@/static/images/icon (20).png" mode="" v-if="(listId.findIndex((val)=>item.id==val))==-1"></image>
					<image src="@/static/images/icon (22).png" mode="" v-else></image>
				</view>
				<view class="txt">{{item.name}}</view>
			</view>
		</view>
		<view class="btnPoz">
			<view class="btnBg" @click="chooseMachine">确 定</view>
		</view>
	</view>
</template>

<script>
	import { machineList } from '@/config/api.js'
	export default {
		data() {
			return {
				list: [],
				listId: [],
				list2: []
			}
		},
		onLoad() {
			this.machineListFn()
		},
		computed: {
			isAll() {
				if(!this.listId.length) return false
				if(this.listId.length==this.list2.length) return true
				return false
			}
		},
		methods: {
			async machineListFn() {
				let obj = {
					limit: 10000,
					status: 1
				}
				const {data: res} = await machineList(obj)
				this.list = res.rows||[]
				this.list2 = res.rows||[]
				console.log(res)
			},
			searchFn(val) {
				this.list = this.list2.filter(item=>{
				   return item.name.includes(val)
				})
			},
			chooseAll() {
				if(this.isAll) {
					this.listId = []
				}else {
					this.listId = this.list2.map(item=>item.id)
				}
			},
			chooseItem(val) {
				let index = this.listId.findIndex((item)=>item==val.id)
				if(index==-1) {
					this.listId.push(val.id)
				}else {
					this.listId.splice(index, 1)
				}
				console.log(index)
				console.log(this.listId)
			},
			chooseMachine() {
				uni.$emit('chooseMachine', this.listId)
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.btnPoz {
		position: fixed;
		bottom: 30rpx;
		left: 0;
		width: 100%;
		padding: 0 32rpx;
	}
	.top_row {
		display: flex;
		align-items: center;
		padding: 14rpx 30rpx;
		background-color: #fff;
		font-size: 26rpx;
		.selectAll {
			width: 130rpx;
		}
		.searchBox {
			flex: 1;
		}
	}
	.row {
		align-items: center;
		display: flex;
		padding: 24rpx 0;
		.icon {
			width: 32rpx;
			height: 32rpx;
			
		}
		.txt {
			padding: 0 16rpx;
			font-size: 26rpx;
		}
		
	}
	.card {
		margin-top: 8rpx;
		padding: 30rpx;
	}
</style>