<template>
	<view  style="background-color: #fff;" :class="{
		iscomponents:iscomponents==1
	}">
		
		<u-navbar title="设备检索"  ref="navbar"  v-if="iscomponents!=1" ></u-navbar>
		
		<!-- 搜索 -->
		<view class="search" v-if="iscomponents!=1"  >
			<!-- <u-icon name="arrow-left" @click="back()"  size="36"></u-icon> -->
			<view class="search-box">
				<u-search   :show-action="true" class="u-search"   @custom="form.pageIndex=1,getList()"
				v-model="queryContent" @search="form.pageIndex=1,getList()"
				 placeholder="搜索设备名称或型号"
				 action-text="搜索" :animation="true"></u-search>


				<!-- <u-search class="u-search" placeholder="搜索设备名称或型号" v-model="queryContent" :show-action="false">

				</u-search>
				 -->
				 
			</view>
		</view>
		<view class="main">
			<!-- 分类 -->
			<view class="classify calc10080 " v-if="!queryContentBl&&!isreturn">
				<scroll-view scroll-y="true" :class="iscomponents!=1?'calc10082':'calc10081'" >
				
				<view class="item " v-for="(item,index) in menuList" :key="index" @click="toggle(index)"
					:class="{'checked':index ==checkindex }">
					{{item.name}}<br/>
				</view>
				</scroll-view>
			</view>
			<!-- 类型 -->
			<view class="type calc10080"  :class="queryContentBl||isreturn?'typeAll':''"  >
			<view class="type-item">
			<!-- 	<view class="title"  v-for="item in 4">
					设备类型一
				</view> -->
				<scroll-view scroll-y="true" :class="iscomponents!=1?'calc10082':'calc10081'"  @scrolltolower="myLoadmoreonReachBottom()">
				
				<view class="content"  >
					<view class="item" v-for="(item,i) in indexList"  @click="viewItem(item)" :key="i">
						<view class="picture">
							<img :src="item.imgUrl"  alt="">
							

						</view>
						<view class="infos" :class="{
							infosQuery:queryContentBl||isreturn
						}" >
							<view class="name">
								{{item.title}}
							</view>
							<view class="model" >
								<view class="infos-title">
									型号：
								</view>
								<view class="value">
									{{item.model}}
								</view>
							</view>
							<view class="model" v-if="item.specifications">
								<view class="infos-title">
									规格：
								</view>
								<view class="value">
									{{item.specifications}}
								</view>
							</view>
							
							<view class="inventory" v-if="!item.code">
								<view class="infos-title">
									库存：
								</view>
								<view class="value">
									{{item.count}}
								</view>
							</view>
							<view class="inventory" v-if="item.code" >
								
								<view class="value">
									{{showCode(item)}}
								</view>
							</view>
							<view class="inventory"  v-if="queryContentBl">
								<view class="infos-title">
									分类：
								</view>
								<view class="value">
									{{getMenuListName(item.typeId)}}
								</view>
							</view>
						</view>
						<view class="option" v-if="iscomponents==1" >
							
								<u-button v-if="deterItem(item)"   @click="ckItem(item)"
								 style="height: 48rpx;" :custom-style="customStyle"
								 type="error" ><u-icon name="trash-fill"></u-icon>删除</u-button>
								
								<u-button v-else  @click="ckItem(item)"
								 style="height: 48rpx;" :custom-style="customStyle"
								 type="primary" ><u-icon name="plus-circle"></u-icon>添加</u-button>
								
								<u-button   @click="viewInfo(item)"
								 style="height: 48rpx;margin-top: 6px;" :custom-style="customStyle"
								 type="success" ><u-icon name="file-text"></u-icon>详情</u-button>
						</view>
					</view>
				</view>
				
				<u-divider  v-if="form.recordsTotal==indexList.length"
				 :isnone="form.recordsTotal==0" nonetext="没有找到相关内容" 
				border-color="#CFD2D5" bg-color="#fff">已经到底了</u-divider>
				
				</scroll-view>
				
    </view>
			</view>

		</view>

	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/index.js'
	
	export default {
		props:{
			departmentId:{
				default: ""
			},
			queryStatus:{
				default: 0
			},
			iscomponents:{
				default: ""
			},
			isreturn:{
				default: ""
			},
			list:{
			
				 default: () => [],
			},
			scanList:{
			
				 default: () => [],
			},
		},
		data() {
			return {
				customStyle:{
					    height: "60rpx",
						lineHeight: "60rpx",
						    marginTop: "3px"
						
				},
				styleheight:"",
				queryContent:"",
				queryContentBl:false,
				checkindex: 0,
				indexList:[],
				form:{
					pageIndex:1,
					pageSize:20,
					list:[]
				},
				menuList: [
					
				]

			}
		},
		computed:{
			listcurrentlist(){
				if(this.menuList.length==0){
					return  []
				}
				var 	list=this.menuList[this.checkindex].list
			
				if(list){
					return list
				}else{
					return  []
				}
				
			}
		},
		mounted() {
			if(this.isreturn){
				if(this.isreturn){
					var sz=[
						{
							typeId:"-$jp%-0",
							name:"未归还设备",
							
							list:[],
							pageIndex:1,
							pageSize:10,
							recordsTotal:0
						},
						// {
						// 	typeId:"-$jp%-1",
						// 	name:"借出历史",
							
						// 	list:[],
						// 	pageIndex:1,
						// 	pageSize:10,
						// 	recordsTotal:0
						// },
					]
					
					
					this.menuList = [
						...sz,
						
					] 
					
					uni.hideLoading();
					if(this.menuList.length>0){
						this.getList()
					}
					
					//this.getList()
				}
			}else{
				this.getDeviceTypeList();
			}
		},
		onReady() {
			//this.getList()
		},
		onReachBottom() {
				console.log(1)
			this.myLoadmoreonReachBottom()
			
		},
		methods: {
			init(){
				this.menuList[this.checkindex].pageIndex=1
				this.getList()
			},
			myLoadmoreonReachBottom(){
			
				var obj=this.menuList[this.checkindex]
				if(this.queryContentBl){
					obj=this.form;
				}
				console.log(obj)
				if (this.indexList.length < obj.recordsTotal) {			
					this.myLoadmore();
				}
			},
			getMenuListName(id){
				var obj =this.menuList.filter(item=>{
					
					return id==item.typeId;
				})
				if(obj.length){
					return obj[0].name
				}
				
			},
			getDeviceTypeList(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.deviceTypeList({}).then((res) => {
					
					var menuList = res.data.deviceTypeList.map(item=>{
						return {
							typeId:item.id,
							name:item.name,
							
							list:[],
							pageIndex:1,
							pageSize:10,
							recordsTotal:0
						}
					});
					var sz=[]
					//
					
					this.menuList = [
						...sz,
						...menuList
					] 
					
					uni.hideLoading();
					if(this.menuList.length>0){
						this.getList()
					}
					
				}).catch(error => {
					uni.showToast({
						title: error
					})
				})
			},
			viewItem(item){
				if(this.iscomponents==1){
					
				}else{
					uni.navigateTo({
						url:"./equipmentDetail?name="+item.title+"&model="+item.model+"&specifications="+item.specifications
					})
				}
			},
			viewInfo(item){
				if(item.code){
					uni.navigateTo({
						url:"/pages/otherFunctions/equipmentRetrieval/equipmentInfo?id="+item.id
					})
				}else{
					uni.navigateTo({
						url:"/pages/otherFunctions/equipmentRetrieval/equipmentDetail?name="+item.title+"&model="+item.model+"&specifications="+item.specifications
					})
				}
				
			},
			deterItem(item){
				var c=""
				if(item.code){
					c=this.scanList.find(it=>{
						
						return it.id==item.id
					})
				}else{
					c=this.list.find(it=>{
						
						return it.obj.id==item.id
					})
				}
				
				if(c){
					return true
				}else{
					return false
				}
			},
			ckItem(item){
				console.log(this.scanList)
				this.$emit('ckItem',item)
				
			},
			myLoadmore(){
				if(this.queryContentBl){
					this.form.pageIndex += 1;
				}else{
					this.menuList[this.checkindex].pageIndex += 1;
					
				}
				this.getList();
			},
			getList(){
				
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				 this.datainit()
				 var list=this.indexList
				 var data = this.menuList[this.checkindex];
				
				 if(this.queryContent){
					 //this.queryContentBl=true
					// 删除类型  delete data.list;
					data=this.form
					data.queryContent=this.queryContent;
					
				 }else{
					 
					  //this.queryContentBl=false
				 }
				 delete data.list;
				if(["-$jp%-0","-$jp%-1"].indexOf(data.typeId)!=-1){
					if(this.queryContent){
						
										
					}else{
										 
						this.queryContentBl=false
					}
					if(data.typeId=="-$jp%-0"){
						data.departmentId=this.departmentId;
						data.queryStatus=!this.queryStatus?1:0
						API.outDeviceByUser(data).then((res) => {
							if(data.pageIndex==1){
								list = res.data.outDeviceList.data;
							}else{
								list = [
									...list,
									...res.data.outDeviceList.data
								];
							}
							this.menuList[this.checkindex].list=list
										
							this.menuList[this.checkindex].recordsTotal = res.data.outDeviceList.recordsTotal;
							
							this.form.recordsTotal = res.data.outDeviceList.recordsTotal;
							this.indexList=list;
							
							uni.hideLoading();
							
						}).catch(error => {
							uni.showToast({
								title: error
							})
						})
						
					}
					if(data.typeId=="-$jp%-1"){
						//废弃
					}	
						
										 
										 
				}else{
					if(this.isreturn){
						data.queryAll=1
					}
					API.deviceList(data).then((res) => {
						if(data.pageIndex==1){
							list = res.data.data;
						}else{
							list = [
								...list,
								...res.data.data
							];
						}
						if(this.queryContent){
							this.queryContentBl=true
						
						}else{
							this.queryContentBl=false
							this.menuList[this.checkindex].list=list
									
							this.menuList[this.checkindex].recordsTotal = res.data.recordsTotal;
						}
						this.form.recordsTotal = res.data.recordsTotal;
						this.indexList=list;
						//console.log(this.form.recordsTotal ,list.length)
						uni.hideLoading();
						
					}).catch(error => {
						uni.showToast({
							title: error
						})
					})
				} 
				
			},
			datainit(){
				// var data = this.menuList[this.checkindex];
				//  if(!data.pageIndex){
				// 	  this.menuList[this.checkindex].pageIndex=1;
				// 	 this.menuList[this.checkindex].pageSize=10
				// 	 this.menuList[this.checkindex].recordsTotal=0;
				// 	  this.menuList[this.checkindex].list=[]
				//  }
			},
			toggle(index) {
				this.checkindex = index
				
				this.datainit()
				
				var list=this.menuList[this.checkindex].list
						
				this.indexList=list;
				if(list.length==0){
					this.getList()
				}
				
				
			},
			back(){
				this.$refs.navbar.goBack()
			}
			
		},
		
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #fff;
	}
	
	.iscomponents /deep/.u-search{
		width: 630rpx !important;
	}
	 
	// 搜索
	.search {
		
		padding: 12rpx 24rpx;
		
		border-radius: 8px;
		overflow: hidden;

		.search-box {
			
			border-radius: 8px;
			overflow: hidden;
		}
		
		.u-search {
		
			margin: auto !important;
			position: relative;
			flex: 0;
			margin: 0 !important;
			height: 64rpx !important;
		}

		/deep/.u-content {
			
			border-radius: 0px !important;
			height: 64rpx !important;
		}

		/deep/.u-icon--right {
			margin-right: 16rpx;
		}

		.search-btn {
			width: 100rpx;
			height: 64rpx;
			background-color: #f2f2f2;

			.text {
				height: 40rpx;
				margin: 12rpx 0;
				border-left: 1px solid #999999;
				font-family: Microsoft Yahei;
				text-align: center;
				color: rgba(16, 98, 213, 1);
				font-size: 28rpx;
			}
		}
	}
	.calc10080{
			//height: calc(95vh - 90px);
	}
	.calc10081{
			//height: calc(95vh - 90px);
			height:800rpx
	}
	.calc10082{
			height: calc(95vh - 90px);
			//height:750rpx
	}
	.main {
		display: flex;

		// 分类
		.classify {
			width: 170rpx;
		
			background-color: #F0F0F0;
			overflow-y: scroll;
			font-size: 28rpx;

			.item {
				height: 120rpx;
				line-height: 120rpx;
				text-align: center;
			}

			.checked {
				background-color: #fff;
				color: #1062D5
			}
		}
	// 类型
	.typeAll{
		width: 750rpx !important;
	}
	.type{
		width: 580rpx;
		padding: 24rpx 24rpx;
		
		overflow-y: scroll;
		.type-item{
			//margin-bottom: 80rpx;
		}
		.title{
			color: rgba(51, 51, 51, 1);
			font-size: 32rpx;
			margin-bottom: 24rpx;
		}
		.content{
			.item{
				display: flex;
				margin-bottom: 28rpx;
				border-bottom: 1px dashed;
				.option{
					padding-left: 20rpx;
				}
				.picture{
					width: 120rpx;
					height: 120rpx;
					border-radius: 8rpx;
					overflow: hidden;
					img{
						width: 100rpx;
						height: 100%;
						
					}
				}
				.infosQuery{
					width:400rpx !important;
				}
				.infos{
					width:200rpx ;
					margin-left: 16rpx;
					.name{
						color: rgba(51, 51, 51, 1);
						font-size: 32rpx;
						margin-bottom: 8rpx;
					}
					.model{
						color: rgba(153, 153, 153, 1);
						font-size: 24rpx;
						display: flex;
						//margin-bottom: 8rpx;
					}
					.inventory{
						color: rgba(153, 153, 153, 1);
						font-size: 24rpx;
						display: flex;
						margin-bottom: 8rpx;
					}
					.infos-title{
						width: 88rpx;
						min-width: 88rpx;
					}
				}
			}
		}
	}
	
	
	}
</style>
