<template>
	<view >
		<u-navbar title="申请归还">
			<view class="details" slot="bottom">
				<view class="list">
					<!-- <view class="list-head" style="margin-bottom: 3px;">
						<view class="title">
							出库设备清单
						</view>
						
					</view> -->
					<view class="list-head">
						<view class="title">
							所属项目部<span style="color: red;">*</span>
						</view>
						<view  class="amountView" >
							<!-- <view class="amount" v-if="approveList.length" >
								<span  @click="show4=true" 
								 style="float: right; color: #45BA45; font-size: 12px;font-weight: normal;">
												<u-icon name="file-text"></u-icon>引用</span>
							</view> -->
							<view class="amount" >
								<!-- <u-input style="border-bottom: 1px solid;"
								 v-model="form.projectName" placeholder="请输入所属项目" type="text" />
								 -->
								 
								 <u-input v-model="xmbId" @click="show3=true,showText='xmbId'"  @clear="showText='xmbId',change('')"   placeholder="请选择所属项目部" 	 type="select" />
								 
							</view>
						</view>
					</view>
					<view class="list-head" >
						<view class="title">
							选择设备
						</view>
						<view  class="amountView" style="margin-top: 5px;" >
							<view class="amount"  >
								<span  @click="show5Btn" 
								 style="float: right; color: #45BA45; font-size: 12px;font-weight: normal;">
												<u-icon name="file-text"></u-icon>添加设备</span>
							</view>
							<view class="amount" style="margin: 0 20px;" >
								或者
							</view>
							<view class="amount"  @click="gotoscan('')" style=" color: #0051ff;font-size: 12px;">
								<u-icon name="scan" color="#0051ff" ></u-icon>扫码识别
							</view>
							
						</view>
						
						
					</view>
					
				</view>
				<!-- 出库单详情 -->
			
					
				
			</view>
			
		</u-navbar>
		
		
		<u-popup  v-model="showpopup" class="scanpopo"  mode="bottom"
		border-radius="14"
		:closeable="true" >
					<view class="scantitle">连续扫码</view>
					<view class="scanlist">
					<view class="scan-list-infos" :style="nowscan.healthStatus&&nowscan.healthStatus!=1?'background-color:#FFC107':''">
						<view class="scan-infos-head" @click="gotoInfo(nowscan.id)">
							<view class="name">
								{{nowscan.title?nowscan.title:'扫码后出当前结果'}}
							</view>
						
							<view class="name">
								编号：{{showCode(nowscan)}}
										<span class="name"  v-show="nowscan&&nowscan.id" style="color:blue;">
											点击查看
										</span>
							</view>
									
						</view>
						<view class="scan-infos-head " style="margin-bottom: 12rpx;">
							<view class="name" v-html="descriptionKey(nowscan)">
								
							</view>
						
						</view>
						<view class="scan-infos">
							
							<!-- <view class="infos-2">
								<view class="infos-item">
									<view class="item-name">
										规格：
									</view>
									<view class="item-value">
										{{nowscan.specifications}}
									</view>
								</view>
								<view class="infos-item">
									<view class="item-name">
										型号：
									</view>
									<view class="item-value">
										{{nowscan.model}}
									</view>
								</view>
							</view> -->
						
						</view>
						</view>
					</view>
							<!--  #ifdef  MP-WEIXIN -->
					<camera mode="scanCode"   resolution='medium' 
					 @scancode="takeCode"  v-if="showpopup"
					 device-position="back" flash="off" @error="error"
					 style="width: 100%; height: 300px;"></camera>
					<!--  #endif -->
				</u-popup>
				
		<!-- 出库设备清单 -->
		<view class="list container" style="padding-bottom: 60px;" v-show="scanList.length" >
			<view class="list-view">
				
				<!-- 清单信息 -->
				<view class="list-infos" v-for="(item,i) in scanList" :key="i"  >
					<view class="infos-head">
						<view class="name">
							{{item.title}} 
						</view>
						<view class="name">
							 编号:{{ showCode(item)}}
						</view>
						<view class="state" @click="delOutListMethod(i)">
							<u-icon name="trash-fill" color="#777777" ></u-icon>删除
						</view>
					</view>
					<view class="infos">
						
							<view class="infos-item">
								<view class="item-name">
									规格：
								</view>
								<view class="item-value">
									{{item.specifications}}
								</view>
							</view>
							<view class="infos-item">
								<view class="item-name">
									型号：
								</view>
								<view class="item-value">
									{{item.model}}
								</view>
							</view>
						

					</view>
					<view class="receive-options">
						<view class="title">
							归还备注
						</view>
						<view class="receive-radio">
							<u-radio-group v-model="item.reStatus" >
								<u-radio  v-for="(radio, index2) in radiolist" :key="index2"
									:name="radio.value" :disabled="radio.disabled">
									{{radio.name}}
								</u-radio>
							</u-radio-group>
						</view>
					</view>

				</view>
			</view>
			
		</view>
		
		<u-divider   :isnone="scanList.length==0" v-show="scanList.length==0"
		
		nonetext="选择设备归还"  nonetop="150" border-color="#CFD2D5">已经到底了</u-divider>
				
		<u-select v-model="show4"  label-name="projectName" value-name="id"
		:list="approveList"   @confirm="confirm4" ></u-select>
		
		<u-popup  v-model="show5"  mode="bottom"
		border-radius="14"
		:closeable="true"  >
					<view class="showpopup">添加设备</view>
						<view class="search"   >
							只查自己借出设备<u-switch v-model="queryStatus"
							  :active-value="0" :inactive-value="1"
							 @change="switchchange"></u-switch>
						</view>
						<!-- 搜索 -->
						<view class="search"  v-if="false" >
							
							<view class="search-box">
								<u-search placeholder="搜索设备名称或型号" 
								  @custom="ckQueryContent"
								   @search="ckQueryContent"
								 v-model="queryContent" :show-action="false">
						
								</u-search>
								<view class="search-btn"  @click="ckQueryContent"  >
									<view class="text">
										搜索
									</view>
								</view>
							</view>
						</view>
						<view  @scrolltolower="refscrolltolower()">
							
						
						<equipmentRetrieval  :departmentId="form.xmbId"
						  :queryStatus="queryStatus"
						  @ckItem="ckItem" :list="list" :scanList="scanList"
						
						 ref="equipmentRetrievalRef" :iscomponents="1" :isreturn="1" ></equipmentRetrieval>
						</view>
				</u-popup>
				 <u-select v-model="show3" :list="projectList"   @confirm="confirm" ></u-select>
				 
		<!-- 确认出库 -->
		<view class="bottom"  >
			
			<u-button  type="primary"  @click="gotoOut()" >确认归还</u-button>
			
		</view>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/projectDepartment.js'
	import equipmentRetrieval from "@/components/equipmentRetrieval"
	
	export default {
		data() {
			return {
				scanFunctionIsUseable:true,
				nowscan:{
					"title": "",
					"specifications": "",
					"experimentLastTime":"",
					"manufactor": "",
					"unit": "",
					"model": "",
					"code": "",
				
				},
				address:"",
				showpopup:false,
				deviceposition:"back",
				queryStatus:true,
				form:{
					projectName:"",
					addressId:"",
					recordId:"",
					deviceIds:"",
						xmbId:"",
				},
					show3:false,//审批人 步骤1
				list:[],
				list2:[],
					showText:"",
				xmbId:"",
				queryContent:'',
				scanList:[],
				approveList:[],
				show4:false,
				show5:false,
				projectList:[
					
				],
				radiolist: [{
						name: '正常',
						value: "0",
						disabled: false
					},
					{
						name: '损坏,需要维修',
						value: "1",
						disabled: false
					},
					
				],
			}
		},
		onReady(){
			//this.getApproveList();
			this.createOutRecord();
		},
		components:{
			equipmentRetrieval
		},
		methods: {
			switchchange(e){
				console.log(e)
				
				this.$refs.equipmentRetrievalRef.init()
			},
			show5Btn(){
				if(this.form.xmbId){
					this.show5=true
				}else{
					uni.showToast({
						title:"请先选择所属项目部"
					})
				}
				
			},
			confirm(e) {
				if(e==''){
					this.form[this.showText]='';
					this[this.showText]=''
				}else{
					this.form[this.showText]=e[0].value;
					this[this.showText]=e[0].label
				}
			},
			createOutRecord(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.createOutRecord({
					
				}).then((res) => {
					uni.hideLoading();
					if(res.data.departmentList){
						this.projectList=res.data.departmentList.map(item=>{
							return {
								value: item.id,
								label: item.name
							}
						})
						if(this.projectList.length==1){
							this.form.xmbId=this.projectList[0].value;
							this.xmbId=this.projectList[0].label
						}
					}
					
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			ckItem(item){
				if(item.code){
					
					var c = this.scanList.findIndex(op => {
						return op.code == item.code
					})
					if (c==-1) {
						this.getscan(item.code,true)
						
					}else{
						this.delOutListMethod(c)
					}
					
					
					
				}else{
					this.list2.push(item)
				}
			},
			refscrolltolower(){
				this.$refs.equipmentRetrievalRef.myLoadmoreonReachBottom()
			},
			ckQueryContent(){
				this.$refs.equipmentRetrievalRef.queryContent=this.queryContent
				this.$refs.equipmentRetrievalRef.getList()
			},
			confirm4(e) {
				this.form.projectName=e[0].label;
			},
			getApproveList(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.outApplyList({
					pageIndex: 1,
					pageSize: 20,
					recordsTotal: 1,
					status: "1",
				}).then((res) => {
				
					var data = res.data.data;
					var sz=[];
					this.approveList=[]
					data.forEach(item=>{
						if(sz.indexOf(item.projectName)==-1){
							sz.push(item.projectName)
							this.approveList.push(item)
						}
					})
					//this.approveList = res.data.data;
					//console.log(this.approveList)
					uni.hideLoading();
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			delOutListMethod(c) {
			
				if (c > -1) {
					this.scanList.splice(c, 1)
					uni.showToast({
						title: "删除成功",
						icon: "none"
					})
				}
			},
			gotoOut() {
				// if(!this.form.projectName){
					
				// 	uni.showToast({
				// 		title: "请输入项目名称",
				// 		icon: "none"
				// 	})
				// 	return
				// }
				if(this.scanList.length==0){
					uni.showToast({
						title: "请扫码添加出库设备",
						icon: "none"
					})
					return
				}
				var _this=this;
				uni.showModal({
					title: '提示',
					content:"确认清单无误",
					//content: '这是一个模态弹窗',
					success: function(res) {
						if (res.confirm) {
							_this.gotoOutMethod(res.content)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			gotoOutMethod() {
				//recordData
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var list=this.scanList.map(item=>{
					return item.id
				});
				var listStatus=this.scanList.map(item=>{
					return item.reStatus
				});
				this.form.deviceIds=list.join()
				this.form.deviceStatus=listStatus.join()
			
				
				API.saveInRecord(this.form).then((res) => {
					uni.hideLoading();
					const eventChannel = this.getOpenerEventChannel();
					  eventChannel.emit('refreshData');
					  uni.hideLoading()
					uni.showModal({
						title: '提示',
						content: '操作成功',
						showCancel:false,
						success: function (res) {
							if (res.confirm) {
								
								uni.navigateBack()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getscan(val,k) {
				if(k){
					
				}else{
					this.showpopup=true;
				}
				
				var c = this.scanList.find(item => {
					return item.code == val
				})
				if (c) {
					
					return
				}
				
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.queryDeviceByCode({
					code: val,
			
				}).then((res) => {
					uni.hideLoading();
					if (res.data.deviceInfo) {
						console.log(res.data.deviceInfo)
						this.nowscan=res.data.deviceInfo;
						var c = this.scanList.find(item => {
							return item.id == res.data.deviceInfo.id
						})
						if (c) {
							uni.showToast({
								title: "已存在,添加失败",
								icon: "none"
							})
						} else {
							var obj=res.data.deviceInfo;
							obj.reStatus="0"
							this.scanList.push(obj)
							uni.showToast({
								title: "添加成功",
								icon: "none"
							})
						}
			
			
			
					} else {
						uni.showToast({
							title: "二维码无效",
							icon: "none"
						})
						this.nowscan={}
					}
			
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			error(e) {
			           console.log(e);
			},
			gotoscan(val) {
				
				// #ifdef H5
					this.gotoscanH5(val)
				// #endif
				
				// #ifdef MP-WEIXIN
					this.gotoscanMP(val)
				// #endif
			},
			takeCode(e){
				 console.log(e);
				  if(this.scanFunctionIsUseable) {
				         this.scanFunctionIsUseable = false;
				         // 对扫码结果进行处理
				         console.log(e.detail.result);
						this.getscan(this.takeCodeJp(e.detail.result))
				         // 扫码间隔两秒
				         setTimeout(() => {
				         this.scanFunctionIsUseable = true;
						
				         }, 1000)
				     }
				
			 },
			 gotoscanMP(val){
				 this.showpopup=true;
				  // const ctx = uni.createCameraContext();
				  //            ctx.takePhoto({
				  //                quality: 'high',
				  //                success: (res) => {
				  //                    this.src = res.tempImagePath
				  //                }
				  //            });
			 },
			gotoscanH5(val) {
				console.log('111')
				
				var _this = this;
				uni.showModal({
					title: '提示',
					editable: true,
					//content: '这是一个模态弹窗',
					success: function(res) {
						if (res.confirm) {
							_this.getscan(res.content)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			
			
			},
		}
	}
</script>

<style scoped lang="scss">
	.showpopup{
		font-size: 36rpx;
		text-align: center;
		margin: 32rpx;
	}
	// 搜索
	.search {
		padding: 12rpx 24rpx;
		display: flex;flex-wrap: wrap;
		border-radius: 8px;
		overflow: hidden;
	
		.search-box {
			display: flex;
			border-radius: 8px;
			overflow: hidden;
		}
		
		/deep/.u-search {
			width: 630rpx;
			margin: auto !important;
			position: relative;
			flex: 0;
			margin: 0 !important;
			height: 64rpx !important;
		}
	
		/deep/.u-content {
			width: 430px;
			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;
			}
		}
	}
	
	
	.scantitle{
		text-align: center;
		font-size: 44rpx;
		 padding-top: 20px;
	}
	page {
		padding-bottom: 200rpx;
	}

	// 接收选项
	.receive-options {
		display: flex;flex-wrap: wrap;
		align-items: center;
		padding: 24rpx 0;
	
		.title {
			color: #333333;
			margin-right: 24rpx;
			font-weight: bold;
		}
	
		.receive-radio {
			flex: 1;
	
			/deep/.u-radio-group {
				width: 100%;
	
			}
	
			/deep/.u-radio {
				margin-right: 48rpx;
			}
		}
	
	}
	// 出库单状态
	.head {
		background-color: #fff;
		padding: 24rpx 32rpx;
		display: flex;flex-wrap: wrap;
		align-items: center;
		color: #101010;
		font-size: 32rpx;

		.photo {
			width: 84rpx;
			height: 84rpx;
			border-radius: 100rpx;

			img {
				width: 100%;
				height: 100%;
				overflow: hidden;
			}
		}

		.name {
			color: #101010;
			font-size: 32rpx;
			margin-right: 8rpx;
			margin-left: 24rpx;
			max-width: 200rpx;
			overflow: hidden;
			 text-overflow: ellipsis;
			 white-space: nowrap
		}

		.submit {
			text {
				color: #3385FF;
			}
		}

		.state {
			line-height: 48rpx;
			padding: 0 24rpx;
			border-radius: 50px;
			background-color: rgba(255, 255, 255, 1);
			color: rgba(51, 133, 255, 1);
			font-size: 12px;
			text-align: center;
			font-family: Arial;
			border: 1px solid rgba(51, 133, 255, 1);
			margin-left: auto;
		}
	}
	.container{
		margin-top: 80px;
		
	}

	// 出库单详情
	.details {
		background-color: #fff;
		margin-top: 24rpx;
		
		padding: 16rpx 32rpx;
		.item {
			display: flex;flex-wrap: wrap;
			justify-content: space-between;
			padding: 12rpx 0;
			//border-bottom: 1px solid rgba(244, 244, 244, 1);
			
			.name {
				line-height: 30px;
				color: #333333;
				font-size: 32rpx
				
			}

			.value {
				color: #101010;
			}
		}
	}

	// 出库单列表
	.list {	
		//margin-top: 24rpx;
		.list-view{
			padding: 16rpx 32rpx;
			background-color: #fff;
		}
		.list-head {
			display: flex;flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;
			
			.title {
				padding: 8rpx 0;
				color: #333333;
				font-size: 32rpx
			}
			.amountView{
				display: flex;flex-wrap: wrap;
				justify-content: space-between;
				align-items: center;
				.amount {
					color: #777777;
					font-size: 24rpx;
				
					text {
						color: #3385FF;
					}
				
					/deep/.u-icon--right {
						margin-left: 8rpx;
					}
				}
			}
			
		}

		// 清单信息
		.list-infos {
			border-radius: 8px;
			background-color: #F5F6F9;
			margin-top: 24rpx;
			padding: 24rpx;

			.infos-head {
				display: flex;flex-wrap: wrap;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 24rpx;

				.name {
					color: rgba(51, 51, 51, 1);
					font-size: 32rpx;
				}

				.state {
					color:#777777;
				}
			}

			.infos {
				margin-top: 16rpx;
				display: flex;flex-wrap: wrap;
				color: #777777;
				font-size: 24rpx;
				justify-content: space-around;
				.infos-1 {
					margin-right: 68rpx;
				}

				.infos-item {
					width: 50%;
					display: flex;flex-wrap: wrap;
					margin-bottom: 8rpx;
					.item-value{
						max-width: 200rpx;
					}
				}
			}
		}
	}

	// 流程
	.process {
		background-color: #fff;
		margin-top: 24rpx;
		padding: 24rpx 32rpx;

		.title {
			color: rgba(51, 51, 51, 1);
			font-size: 32rpx;
		}
        .u-time-axis-item{
			//margin-bottom: 76rpx;
		}
		.u-time-axis {
			//padding: 18rpx 40rpx;
		}

		.u-node {
			width: 18rpx;
			height: 18rpx;
			border-radius: 100rpx !important;
			display: flex;flex-wrap: wrap;
			justify-content: center;
			align-items: center;
			background: #d0d0d0;
		}
		.u-order{
			margin-bottom: 32rpx;
		}
		.u-order-title {
			color: #333333;
			font-weight: bold;
			font-size: 32rpx;
		}

		.u-order-desc {
			margin-bottom: 16rpx;
			display: flex;flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;

			.level {
				color: #333333;
				font-size: 32rpx;
				font-weight: bold;
			}

			.date {
				color: rgba(119, 119, 119, 1);
			}

			.state {
				color: rgba(255, 121, 0, 1);
			}
		}

		.u-order-time {
			width: 90rpx;
			color: #777777;
			font-size: 28rpx;
			margin-top: 16rpx;
			display: flex;flex-wrap: wrap;
			flex-direction: column;
			align-items: center;

			.photo {
				width: 56rpx;
				height: 56rpx;
				border-radius: 100px;
				margin-bottom: 4rpx;

				img {
					width: 100%;
					height: 100%;
					overflow: hidden;
					position: relative;
				}
			}

			.confirm {
				position: absolute;
				top: 46rpx;
				left: 56rpx;
			}

			.name {
				color: rgba(51, 51, 51, 1);
				font-size: 24rpx;
			}

		}
      
	}
	
	// .u-time-axis{
	// 	.u-time-axis-item:last-of-type{
	// 		margin-bottom: 0;
	// 	}
	// }
	// 确认出库
	.bottom{
		background-color: #fff;
		margin-top: 44rpx;
		padding: 16rpx 32rpx;
		z-index: 999;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		uni-button{
			border-radius: 8px;
			background: linear-gradient(180deg, rgba(22,119,255,1) 0%,rgba(16,98,213,1) 100%);
			color: rgba(255, 255, 255, 1);
			font-size: 32rpx;
			line-height: 88rpx;
			
		}
	}
</style>
