<template>
	<view >
		<u-navbar title="出库单详情"></u-navbar>
		<!-- 出库单状态 -->
		<view class="container">
		<view class="head">
			<view class="photo">
				<u-avatar size="84" :src="outUser.headImg"></u-avatar>
				
			</view>
			<view class="name">
				{{outUser.name}}
			</view>
			<view class="submit">
				提交了<text>出库申请</text>
			</view>
			<view class="state" :style="{
				color:recordStatusColor(info.status),
				borderColor:recordStatusColor(info.status)
			}">
				{{info.statusN}}
			</view>
		</view>
		<!-- 出库单详情 -->
		<view class="details">
			
			<view class="item">
				<view class="name">
					所属项目部
				</view>
				<view class="value">
					{{info.departmentName}}
				</view>
			</view>
			
			<view class="item">
				<view class="name">
					所属项目
				</view>
				<view class="value">
					{{info.projectName}}
				</view>
			</view>
			<view class="item">
				<view class="name">
					需求时间
				</view>
				<view class="value">
					{{info.startTime?info.startTime:'无'}}
				</view>
			</view>
			<view class="item">
				<view class="name">
					使用周期至
				</view>
				<view class="value">
					{{info.endTime}}
				</view>
			</view>
			
			
			<view class="item">
				<view class="name">
					申请时间
				</view>
				<view class="value">
					{{info.createTime}}
				</view>
			</view>

		</view>
		<!-- 出库设备清单 -->
		<view class="list">
			<view class="list-head">
				<view class="title">
					出库设备清单
				</view>
				<view class="amount" @click="gotoUrl('pages/projectDepartment/exportApplication/deliveriedList?id='+id)">
					全部 <text>{{recordsTotal}}</text> 类设备
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<!-- 清单信息 -->
			<view class="list-infos" v-for="(item,i) in outRecordDeviceList" :key="i" >
				<view class="infos-head">
					<view class="name">
						{{item.deviceName}}
					</view>
					<view class="state">
						{{item.statusN}}： <text v-if="item.status!=0">
							{{item.realDeviceList.length}}
						</text>
						 <text v-else >{{item.needCount}}</text>
					</view>
				</view>
				<view class="infos">
					<view class="infos-1">
						<view class="infos-item">
							<view class="item-name">
								厂家：
							</view>
							<view class="item-value">
								{{item.deviceInfo?item.deviceInfo.manufactor:''}}
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								单位：
							</view>
							<view class="item-value">
								{{item.deviceInfo?item.deviceInfo.unit:''}}
							</view>
						</view>
					</view>
					<view class="infos-2">
						<view class="infos-item">
							<view class="item-name">
								规格：
							</view>
							<view class="item-value">
								{{item.deviceInfo?item.deviceInfo.specifications:''}}
							</view>
						</view>
						<view class="infos-item">
							<view class="item-name">
								型号：
							</view>
							<view class="item-value">
								{{item.deviceInfo?item.deviceInfo.model:''}}
							</view>
						</view>
					</view>
				
				</view>
				

			</view>
			<u-divider @click="gotoUrl('pages/projectDepartment/exportApplication/deliveriedList?id='+id)" border-color="#CFD2D5">仅显示部分数据</u-divider>
			
		</view>
		<approveList ref="approveList"
		:outRecordDetailObj="outRecordDetailObj"
		 :approveList="approveList"></approveList>
    </view>
	 <u-popup  v-model="showpopup"  mode="bottom"
	border-radius="14"
	:closeable="true"  >
		<p  style=" margin-top: 30px;">请在下方空白区域使用正楷字体进行电子签名</p>
			
		<view class="signature">
			<l-signature 
			 width="100%"
			 disableScroll ref="signatureRef" :penColor="penColor" :penSize="penSize"
				:openSmooth="openSmooth"></l-signature>
		</view>
		<view class="clean-save">
			<view class="clean" @click="onClick('clear')">
				
				<view class="">
					清除
				</view>
			</view>
			<view class="save" @click="onClick('save')">
				
				<view class="">
					保存并提交
				</view>
			</view>
			
		</view>
		
	</u-popup> 
	<!-- 确认出库 -->
		<view class="bottom"  v-if="info.status!=3&&info.status!=9">
			<!-- <button>确认出库</button> -->
			<u-button  v-if="info.status==0" type="revoke" @click="revokeBtn" >撤销</u-button>
			<u-button   v-else-if="info.status==8" type="primary"  @click="saveRecordConfirm" >签字验收</u-button>
			<u-button   v-else type="primary" @click="getInfo" >刷新</u-button>
			
		</view>
	</view>
</template>

<script>
	
	 import LSignature from '@/pages/projectDepartment/components/l-signature/l-signature.vue'
	
	import * as API from '@/apis/pagejs/projectDepartment.js'
	import {
		recordStatus,recordStatusColor
	} from '@/apis/status.js'
	import approveList from "@/components/ApproveList.vue"
	
	export default {
		components: {
			approveList,LSignature
		},
		data() {
			return {
				id:"",
				info:{
					projectName:"",
					startTime:"",
					endTime:"",
					createTime:"",
				},
				outUser:{
					name:"",
					headImg:""
				},
				outRecordDetailObj:{},
				outRecordDeviceList:[],
				approveList:[],
				recordsTotal:0,
				signUrl:"",
				showpopup:false,
				penColor: '',
				penSize: 5,
				openSmooth: true,
			}
		},
		onLoad(op){
			this.id=op.id
			this.getInfo()
		},
		methods: {
			recordStatusColor,
			recordStatus,
			onClick(type) {
				this.url =""
				 if(type == 'openSmooth') {
					 this.openSmooth = !this.openSmooth
					 return
				 }
				if (type == 'save') {
					this.$refs.signatureRef.canvasToTempFilePath({
						success: (res) => {
							// 是否为空画板 无签名
							if(res.isEmpty){
								this.signUrl ="";
							}else{
								
						
								
								// #ifdef MP-WEIXIN
								
									var _this=this;
									wx.getFileSystemManager()
									  .readFile({ //读取本地文件内容
									    filePath: res.tempFilePath, // 文件路径
									    encoding: 'base64', // 返回格式
									    success: ({data}) => {
										
									      _this.signUrl='data:image/png;base64,' + data;
										  _this.saveRecordConfirmMethod("1");
									    },
									    fail(res) {
									      console.log('fail', res)
									    }
									  });
									  
								// #endif
								// #ifdef H5
									this.signUrl=res.tempFilePath
									this.saveRecordConfirmMethod("1");
								// #endif
								
								
							}
						}
					})
					return
				}
				if (this.$refs.signatureRef)
					this.$refs.signatureRef[type]()
			},
			getList(){
				
				API.outRecordDeviceById({
					recordId:this.id,
					pageSize:3,
					pageIndex:1,
				}).then((res) => {
					uni.hideLoading();
					this.outRecordDeviceList=res.data.data;
					this.recordsTotal=res.data.recordsTotal;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			revokeMethod(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.cancelOutRecord({
					recordId:this.id,
				
				}).then((res) => {
					uni.hideLoading();
					
					this.getInfo(true)
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			revokeBtn(){
				var _this=this
				uni.showModal({
					content:"确认是否撤销?",
					title:"提示",
					success(res) {
					
						if(res.confirm){
							_this.revokeMethod()
						}else{
							
						}
					}
				})
			},
			saveRecordConfirm(){
				this.showpopup=true;
				return
				var _this=this
				uni.showModal({
					content:"确认签收无误?",
					title:"提示",
					success(res) {
					
						if(res.confirm){
							//_this.saveRecordConfirmMethod()
							_this.showpopup=true;
						}else{
							
						}
					}
				})
			},
			saveRecordConfirmMethod(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.saveRecordConfirm({
					recordId:this.id,
					status:1,
					signUrl:this.signUrl
				}).then((res) => {
					uni.hideLoading();
					this.showpopup=false;
					this.getInfo(true)
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			
			getInfo(bl){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.outRecordDetail({
					id:this.id,
				}).then((res) => {
					this.outRecordDetailObj=res.data;
					this.info=res.data.outRecord;
					this.approveList=res.data.approveList;
					this.outUser=res.data.outUser;
					if(bl){
						const eventChannel = this.getOpenerEventChannel();
						eventChannel.emit('refreshData');
						uni.hideLoading();
					}else{
							this.getList()
					}
				
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.signature{
		background-color: #fff;
		margin-top: 8px;
		height: 160px;
		border: #c2c2c2  1px solid;
	}
	.clean-save{
		background-color: #fff;
		display: flex;flex-wrap: wrap;
		.clean,.save{
			width: 50%;
			text-align: center;
			color: #252525;
			line-height: 40px;
		}
		.clean,.save{
			border:#c2c2c2  1px solid;
			display: flex;flex-wrap: wrap;
			justify-content: center;
			img{
				margin-right: 4px;
			}
		}
	}
	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;
			.u-avatar{
				
			}
			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;

		.item {
			display: flex;flex-wrap: wrap;
			justify-content: space-between;
			padding: 12rpx 0;
			border-bottom: 1px solid rgba(244, 244, 244, 1);

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

			.value {
				color: #101010;
			}
		}
	}

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

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

			.title {
				color: #333333;
				font-size: 32rpx
			}

			.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: rgba(0, 81, 255, 1);
				}
			}

			.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;
					}
				}
			}
		}
	}

	
	
	// .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>
