<template>
	<view >
		<u-navbar title="出库申请"></u-navbar>
		<!-- 出库单状态 -->
		<view class="container">
	
		<!-- 出库单详情 -->
		<view class="details">
			<p  class="itemp">项目信息
			 
			 <span  @click="show4=true" v-if="approveList.length"
			  style="float: right; color: #45BA45; font-size: 12px;font-weight: normal;">
				<u-icon name="file-text"></u-icon>引用历史项目</span></p>
			
			<view class="item">
				<view class="name">
					所属项目<span style="color: red;">*</span>
				</view>
				<view class="value">
					
					<u-input v-model="form.projectName" placeholder="请输入所属项目" type="text" />
				</view>
			</view>
			<view class="item">
				<view class="name">
					需求时间
				</view>
				<view class="value">
					
						<u-input v-model="needTime" @click="show=true,showText='needTime'"  @clear="showText='needTime',change('')" 	 placeholder="请输入需求时间"  type="select" />
						
				</view>
			</view>
			<view class="item">
				<view class="name">
					使用周期至<span style="color: red;">*</span>
				</view>
				<view class="value">

					<u-input v-model="endTime" @click="show=true,showText='endTime'"  @clear="showText='endTime',change('')"   placeholder="请输入使用周期" 	 type="select" />
				</view>
			</view>
			<view class="item" >
				<view class="name">
					所属项目部<span style="color: red;">*</span>
				</view>
				<view class="value">
					
					<u-input v-model="xmbId" @click="show3=true,showText='xmbId'"  @clear="showText='xmbId',change('')"   placeholder="请选择所属项目部" 	 type="select" />
				</view>
			</view>
			<p  class="itemp">选择审批人</p>
			<view class="item" v-for="(item,i) in approveProcessList"  :key="i" >
				<view class="name">
					{{item.name}}<span style="color: red;">*</span>
				</view>
				<view class="value">
					
					<u-input v-model="item.approveName" @click="show2=true,showIndex=i" 
					 @clear="showIndex=i,change2('')" 
					   :placeholder="'请选择'+item.name+'审批人'" 	 type="select" />
				</view>
			</view>
		

		</view>
		
		<!-- 出库设备清单 -->
		<view class="list">
			<view class="list-head">
				<view class="title">
					出库设备清单
				</view>
				<view class="amount add"  @click="showpopupBtn()" >
					
						<u-icon name="plus-circle"></u-icon>添加设备
				
				</view>
			</view>
			<!-- 清单信息 -->
			<view class="list-infos" v-for="(item,index) in list"  :key="index" >
				<view class="infos-head">
					<view class="name">
						{{item.obj.title}}
					</view>
					<view class="state" @click="del(index)">				
						<u-icon name="minus-circle" ></u-icon>删除
					</view>
				</view>
				
				<view class="infos">
					<view class="infos-1">
						<view class="infos-item">
							<view class="item-name">
								库存：
							</view>
							<view class="item-value">
								{{item.obj.inventory}}
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								单位：
							</view>
							<view class="item-value">
								{{item.obj.unit}}
							</view>
						</view>
					</view>
					<view class="infos-2">
						<view class="infos-item">
							<view class="item-name">
								规格：
							</view>
							<view class="item-value">
								{{item.obj.specifications}}
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								型号：
							</view>
							<view class="item-value">
								{{item.obj.model}}
							</view>
						</view>
					</view>
				
				</view>
				
				<view class="infos" v-if="false">
					<view class="infos-1">
						<view class="infos-item">
							<view class="item-name">
								型号：
							</view>
							<view class="item-value">
								{{item.obj.model}}
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								库存：
							</view>
							<view class="item-value">
								{{item.obj.count}}
							</view>
						</view>
					</view>
					<view class="infos-2" v-if="false">
						<view class="infos-item">
							<view class="item-name">
								单位：
							</view>
							<view class="item-value">
								台
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								库存：
							</view>
							<view class="item-value">
								20
							</view>
						</view>
					</view>
					

				</view>
				<view>
					
					出库数量	
					<u-number-box bg-color="#FFFFFF" :min="1"
					 style="float: right;" v-model="item.count"></u-number-box>

				</view>

			</view>
			<u-divider   v-show="list.length" border-color="#CFD2D5">已经到底了</u-divider>
		
		</view>
	 </view>
	 <ujp-calendar v-model="show" mode="date"  max-date="2099-01-01"  @change="change"></ujp-calendar>
	 
	 <u-select v-model="show3" :list="projectList"   @confirm="confirm" ></u-select>
	 <u-select v-model="show4"  label-name="projectName" value-name="id"
	  
	  :list="approveList"   @confirm="confirm4" ></u-select>
	 
	 <u-select v-model="show2" :list="list2"   @confirm="confirm2" ></u-select>
	 <!-- -sync -sync -->
	 <u-popup  v-model="showpopup"  mode="bottom" 
	 border-radius="14"
	 :closeable="true"  >
	 			<view class="showpopup">添加设备</view>
				
				<!-- 搜索 -->
				<view class="search"  >
					
					<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>
				<scroll-view scroll-y="true" style="height: 800rpx;" @scrolltolower="refscrolltolower()">
					
				
				<equipmentRetrieval  @ckItem="ckItem" :list="list"
				 ref="equipmentRetrievalRef" :iscomponents="1"></equipmentRetrieval>
				</scroll-view>
	 		</u-popup>
	<!-- 确认出库 -->
	<view class="bottom"  >
		
		<u-button type="primary"  @click="submit">提交出库单</u-button>
		
	</view>
	</view>
</template>

<script>
	import equipmentRetrieval from "@/components/equipmentRetrieval"
	import * as API from '@/apis/pagejs/projectDepartment.js'
	import ujpCalendar from '@/pages/projectDepartment/components/u-calendar.vue'
	
	export default {
		data() {
			return {
				recordId:"",
				form:{
					recordId:"",
					needTime:"",
					endTime:"",
					projectName:"",
					xmbId:"",
				},
				
				show:false,
				xmbId:"",
				showText:"",
				showIndex:0,
				showpopup:false,
				endTime:'',
				needTime:'',
				approve1:"",
				approve2:"",
				queryContent:'',
				list:[],
				
				show2:false,//审批人 步骤1
				show3:false,//审批人 步骤1
				show4:false,//审批人 步骤1
				
				//list2:[],
				approveList:[
					
				],
				
				approveProcessList:[],
				projectList:[
					
				],
			}
		},
		components:{
			equipmentRetrieval,ujpCalendar
		},
		computed:{
			 list2(){
				 var list=[];
				 if(this.approveProcessList.length){
					 list=this.approveProcessList[this.showIndex].approveUserList.map(item=>{
					 	return {
					 		value: item.id,
					 		label: item.name
					 	}
					 })
				 }
				
				 return list
			 }
		},
		onReady() {
			this.createOutRecord();
		},
		methods:{
			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"
					})
				})
			},
			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
						}
					}
					if(res.data.approveProcessList.length){
						this.approveProcessList=res.data.approveProcessList;
						for(var i in this.approveProcessList){
							var obj=this.approveProcessList[i];
							if(obj.approveUserList.length==1){
								var e=obj.approveUserList
								obj.approveId=e[0].id;
								obj.approveName=e[0].name;
								// this.approveProcessList[i]=obj
								// console.log("ok",obj)
							}
							
						}
						this.getApproveList()
					}else{
						uni.showToast({
							title: "审批流未配置,请联系管理员",
							icon: "none"
						})
					}
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			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
				}
			},
			confirm2(e) {
				if(e==''){
					
				}else{
					var obj=this.approveProcessList[this.showIndex];
					obj.approveId=e[0].value;
					obj.approveName=e[0].label;
				
				}
			},
			getInfo(id){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.outRecordDetail({
					id:id,
				}).then((res) => {
					//this.outRecordDetailObj=res.data;
					var info=res.data.outRecord;
					var approveList=res.data.approveList;
					//this.outUser=res.data.outUser;
					uni.hideLoading();
					
					this.form.projectName=info.projectName
					this.projectList.forEach(item=>{
						if(item.value==info.departmentId){
							this.form.xmbId=item.value;
							this.xmbId=item.label
						}
					})
					if(approveList.length==this.approveProcessList.length){
						for(var i in approveList){
							var obj1=approveList[i].approveUserList[0]
							var obj2=this.approveProcessList[i].approveUserList
							
							
							for(var j in obj2){
								
								if(obj2[j].id==obj1.id){
									
									this.approveProcessList[i].approveId=obj1.id
									this.approveProcessList[i].approveName=obj1.name
								}
							}
							
							
						}
					}
					
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			confirm4(e) {
				//obj.approveId=e[0].value;
				//obj.approveName=e[0].label;
				this.getInfo(e[0].value)
			},
			showpopupBtn(){
				
				
				this.showpopup=true
			},
			
			refscrolltolower(){
				this.$refs.equipmentRetrievalRef.myLoadmoreonReachBottom()
			},
			ckQueryContent(){
				this.$refs.equipmentRetrievalRef.queryContent=this.queryContent
				this.$refs.equipmentRetrievalRef.getList()
			},
			change(e){
				if(e==''){
					this.form[this.showText]='';
					this[this.showText]=''
				}else{
					this.form[this.showText]=e.result;
					this[this.showText]=e.year+"年"+e.month+"月"+e.day+"日"
				}
			},
			change2(e){
				if(e==''){
					this.form[this.showText]='';
					this[this.showText]=''
				}else{
					this.form[this.showText]=e.result;
					this[this.showText]=e.year+"年"+e.month+"月"+e.day+"日"
				}
			},
			submit(){
				var sprData=[]
				if(this.approveProcessList.length){
					var bl=false;
					var k =0
					for(var i in this.approveProcessList){
						k++;
						var obj=this.approveProcessList[i]
						if(obj.approveId){
							sprData.push({
								no:k,
								userId:obj.approveId
							})
						}else{
							bl=true;
							uni.showToast({
								title: '请选择'+obj.name+'审批人',
								icon: "none"
							})
							break
						}
						
					}
					if(bl){
						return
					}
				}else{
					uni.showToast({
						title: "审批流未配置,请联系管理员",
						icon: "none"
					})
					return
				}
				
				
				if(!this.form.projectName){
					uni.showToast({
						title: "请填写所属项目"
					})
					return
				}
				if(!this.form.endTime){
					uni.showToast({
						title: "请输入使用周期"
					})
					return
				}
				
				if(this.list.length==0){
					uni.showToast({
						title: "请添加设备"
					})
					return
				}
				var list=this.list.map(item=>{
					var obj={
						...item
					}
					delete obj.obj
					return obj
				})
				this.form.deviceData=list;
				this.form.sprData=sprData;
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.saveOutRecord({
					content:JSON.stringify(this.form)
				}).then((res) => {
					  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
					})
				})
			},
			del(val){
				this.list.splice(val,1)
			},
			deviceDetailsByNameApi(val,item){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.deviceDetailsByName({
					name:val,
					model:item.model
				}).then((res) => {
					
					  uni.hideLoading()
					 var deviceInfo= res.data.deviceInfo;
					 deviceInfo.inventory=res.data.count;
					this.list.push({
						deviceName:val,
						model:deviceInfo.model,
						obj:deviceInfo,
						count:1
					})
				}).catch(error => {
					
					// uni.showToast({
					// 	title:"添加失败"
					// })
					this.list.push({
						deviceName:item.title,
						model:item.model,
						obj:item,
						count:1
					})
				})
			},
			ckItem(item){
				
				
				var c=this.list.findIndex(it=>{
					
					return (it.obj.title==item.title&&it.obj.model==item.model)
				})
				
				if(c!=-1){
					console.log(c)
					this.del(c)
				}else{
					
					this.deviceDetailsByNameApi(item.title,item);
					
				}
				
				
			},
		}
		
	}
</script>

<style scoped lang="scss">
	
	
	// 搜索
	.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;
			}
		}
	}
	
	.showpopup{
		font-size: 36rpx;
		text-align: center;
		margin: 32rpx;
	}
	page {
		padding-bottom: 200rpx;
	}

	// 出库单状态
	.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;
		}
	}

	// 出库单详情
	.details {
		background-color: #fff;
		margin-top: 24rpx;
		padding: 0 32rpx;
		.itemp{
			    margin: 3px 0px;
				padding-top: 6px;
				font-size: 16px;
			    font-weight: bold;
		}
		.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: rgba(119, 119, 119, 1);
			}

			.value {
				color: #101010;
			}
		}
	}

	// 出库单列表
	.list {
		background-color: #fff;
		margin-top: 24rpx;
		padding: 22rpx 32rpx;
		padding-bottom: 60px;

		.list-head {
			display: flex;flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;

			.title {
				color: #333333;
				font-size: 32rpx
			}
			.amount.add{
				color:#45BA45 ;
			}
			.amount {
				
				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: #999999;
				}
			}

			.infos {
				margin-top: 16rpx;
				display: flex;flex-wrap: wrap;
				color: #777777;
				font-size: 24rpx;

				.infos-1 {
					margin-right: 68rpx;
				}

				.infos-item {
					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>
