<template>
	<view class="page">
		<view class="vtitle">归还信息</view>
		<view class="form_group">
			<view class="lable">扫码</view>
			<view class="tag" @click="qr()">
				<text class="span" style="width: 90%;">{{ code }}</text>
				<text class="icon code">&#xe60e;</text>
			</view>
		</view>
		<view class="form_group"  v-if="op == '设备借用11'">
			<view class="lable">设备所属部门</view>
			<view class="tag">
				<view class="span">
					<picker @change="bindChange($event, 'dept')" :range="array1">
						<view class="picks">{{ item.dept ? item.dept : '选择2' }}</view>
					</picker>
				</view>
				<text class="icon more">&#xe631;</text>
			</view>
		</view>
	
		<view class="form_group" v-if="op == '设备归还'">
			<view class="lable">归还者</view>
			<view class="tag">
				<input v-model="item.ghz" placeholder="归还者" />
				<text class="icon more">&#xe631;</text>
			</view>
		</view>
	
		<view class="form_group">
			<view class="lable">手机号</view>
			<view class="tag">
				<input v-model="item.tel" placeholder="输入手机号" />
				<text class="icon more">&#xe631;</text>
			</view>
		</view>
		<view class="vtitle">归还设备信息</view>
		<view v-for="(item, index) in list" :key="index" class="item">
			<view class="form_group">
				<view class="lable">序号</view>
				<view class="tag">
					<text class="span">{{ index + 1 }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">计量编号</view>
				<view class="tag">
					<text class="span">{{ item.items.__Q0YnOcfMqiTpVsTiDEh.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">设备名称</view>
				<view class="tag">
					<text class="span">{{ item.items.__khn9SQnzgWNPt0aHldp.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">型号规格</view>
				<view class="tag">
					<text class="span">{{ item.items.__fpUc5yOsHgSHeHPFHim.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">测量范围</view>
				<view class="tag">
					<text class="span">{{ item.items.__soGLIaAxvM0O0NTVt1c.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">制造商</view>
				<view class="tag">
					<text class="span">{{ item.items.__aPTYvYeakxz299A7Thk.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">内部编号</view>
				<view class="tag">
					<text class="span">{{ item.items.__dRIZk7xLPVCO6rqJbs7.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<button class="btn del" @click="del(item)">移除</button>
			</view>
		</view>
		<u-empty v-if="list.length == 0"></u-empty>
		<view class="footer">
			<view class="flex">
				<view class="f"><button class="btn br" @click="submission()">提交</button></view>
				<!--	<view class="f" v-if="state == 1"><button class="btn br back" @click="back()">回退</button></view>
				<view class="f"><button class="btn" @click="validate()">保存</button></view>-->
			</view>
		</view>

		<u-popup :show="show" mode="bottom" @close="show = false" closeable round="15">
			<view class="popup">
				<u-divider text="提交流程面板"></u-divider>
				<scroll-view scroll-y="true" style="height: 300rpx;">
					<view v-for="(item, index) in nextNodes" :key="index" class="bitem"
						@click="bselect(item, 'nextNodes')">
						<view class="icon select check" v-if="item.check">&#xe600;</view>
						<view class="icon select" v-else>&#xe8bb;</view>
						<view class="title">{{ item.name }}</view>
					</view>
				</scroll-view>
				<view class="confrim-btn"><button class="btn" @click="submit()">确定</button></view>
			</view>
		</u-popup>
		<u-popup :show="bshow" mode="bottom" @close="bshow = false" closeable round="15">
			<view class="popup">
				<u-divider text="回退流程面板"></u-divider>
				<scroll-view scroll-y="true" style="height: 300rpx;">
					<view v-for="(item, index) in backNodes" :key="index" class="bitem"
						@click="bselect(item, 'backNodes')">
						<view class="icon select check" v-if="item.check">&#xe600;</view>
						<view class="icon select" v-else>&#xe8bb;</view>
						<view class="title">{{ item.name }}</view>
					</view>
				</scroll-view>
				<view class="confrim-btn"><button class="btn" @click="workflowsback()">确定</button></view>
			</view>
		</u-popup>




	</view>
</template>

<script>
	let qrcode = require('@/common/code.js');
	export default {
		data() {
			return {
		
				bitem: {
					applicationId: '__gAPYBW4YxB3UePM3lqO',
					exparams: {},
					formId: '__0FLGNncOS1lz0T9jsFn',
					id: '__79Wtw03ya4dJUDGPyAN',
					isRelate: 'undefined',
					parentId: '',
					sign: '',
					subDocuments: [],
					templateForm: '',
					versions: '0'
				},
				btitem: {
					applicationId: '__gAPYBW4YxB3UePM3lqO',
					exparams: {},
					formId: '__0FLGNncOS1lz0T9jsFn',
					id: '__79Wtw03ya4dJUDGPyAN',
					isRelate: 'undefined',
					parentId: '',
					sign: '',
					subDocuments: [],
					templateForm: '',
					versions: '0'
				},
				geteq: {
					DEVID: '',
					allow: 'false',
					appId: '__gAPYBW4YxB3UePM3lqO',
					datetime: '1670485443880',
					detp: '',
					fieldid: '__iLwKGJTxD89ZKnFzo2Y',
					formId: '__w9J1gxcIWgXmNw6pE1F',
					isEdit: 'true',
					mutil: 'true',
					parentId: '__NYI0rVs0LXbiW21z6Nv',
					searchForm: 'false',
					selectOne: 'false',
					viewId: '__ajlFXbohdIXtn7ErzZq'
				},
				item: {},
				op: '设备归还',
				current: 0,
				code: '',
				list: [],
				show: false,
				bshow: false,
				itemt: {},
				up: {},
				content: '标题',
				nextNodes: [],
				backNodes: [],
				items: {
					document: {
						items: {}
					}
				},
				runViewDialogConfirmScript: {
					docId: "__DIl607e1fsiknRgGRQj",
					document: {
						items: {
							group: "",
							depttool: "",
							UNIT: "柳工",
							borrower: "",
							tel: "",
							remarks: ""
						},
						
					},
					fieldId: "__0TQu1UrRbMZVX1UvtBL",
					formId: "__w9J1gxcIWgXmNw6pE1F",
					viewId: "__ajlFXbohdIXtn7ErzZq",
					_selects: "__sIJiL5GlxEYoL095Vo9",
				},
			};
		},
		onLoad(e) {
			this.op = e.op;
			this.empty();
			//选中审批人员监听函数,其它类型的参考这个
			uni.$on('select', res => {
				console.log('asd:' + JSON.stringify(res));
			});
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.op
			});
		},
		methods: {
			//提交或回退
			bselect(e, tag) {
				this[tag].forEach(item => {
					item.check = false;
				});
				e.check = true;
				this.itemt.nextNodeIds = [];
				this.itemt.nextNodeIds.push(e.id);
				this.$forceUpdate();
			}, //详情
			qr() {
				// 调用uni提供的调用相机api
				uni.chooseImage({
					sizeType: ['original'],
					sourceType: ['camera'],
					count: 1,
					success: res => {
						const tempFilePaths = res.tempFilePaths[0]; // 获取到二维码图片的链接
						qrcode.decode(tempFilePaths); // 解析二维码图片
						qrcode.callback = r => {
							this.code = r;

							this.geteq.DEVID = this.code;
							if (this.list.length == 0) {
								this.getEq();
							} else {
								this.list.forEach(item => {

									if (item.items.__O1qcaazksjAzDcrvJLk.value == this.geteq
										.DEVID) {
										uni.showModal({
											title: '提示',
											content: '该设备已经选择过了',
											showCancel: false,
											success: res => {
												if (res.confirm) {
													//	uni.navigateBack();
												}
											}
										});
									} else {
										
								this.getEq();
									}
								});
							}

							console.log('asd:' + r);
							this.$forceUpdate();
						};
					}
				});
			},
			//选择结果形式
			change(e) {
				this.current = e;
				console.log('aa:' + this.radio[this.current].name);
			},
			//选择实验类型
			bindChange(e, type) {
				this.item[type] = this.array1[e.target.value];
				this.$forceUpdate();
			},
			//选择审批人员
			select() {
				uni.navigateTo({
					url: '/pages/index/select'
				});
			},
			getEq() {
				this.http.request({
					url: this.http.urls.re_list +
						'parentId=' +
						this.geteq.parentid,
					data: this.geteq,
					method: 'POST',
					success: res => {
						//console.log('res2:' + JSON.stringify(res));
						if (res.data.errcode == 0) {

							res.data.data.data.forEach(item => {
							
								this.list.push(item);
							});
						}
					}
				});
			},
			empty() {
				
				this.http.request({
					url: this.http.urls.return_empty,
					data: this.item,

					success: res => {
							console.log('res2:' + JSON.stringify(res));
						this.item = res.data.data.items;
						this.bitem.id = res.data.data.id;
						this.bitem.formId = res.data.data.formid;
						this.bitem.versions = res.data.data.versions;
						this.geteq.parentid = res.data.data.id;



						//创建时组建提交流程面板请求数据
						this.iteml = res.data.data;
						this.items.applicationId = '__gAPYBW4YxB3UePM3lqO';
						this.items.docId = this.iteml.id;
						this.items.id = this.iteml.id;
						this.items.formId = this.iteml.formid;
						this.items.parentId = '';
						this.items.subSelects = [];
						this.items.templateForm = '';
						this.items.viewId = '';
						this.items.document.items = this.item;
						this.items.document.applicationId = '__gAPYBW4YxB3UePM3lqO';
						this.items.document.exparams = {};
						this.items.document.formId = this.iteml.formid;
						this.items.document.id = this.iteml.id;
						this.items.document.parentId = '';
						this.items.document.sign = '';
						this.items.document.stateId = this.iteml.stateid;
						this.items.document.subDocuments = [];
						this.items.document.templateForm = '';
						this.items.document.versions = '0';
					
					}
				});
			},
			//移除借用设备信息
			del(item) {
				this.list.splice(this.list.indexOf(item), 1);
			},

			tool_borrowing_empty() {
				this.list.forEach(item => {

                     this.http.request({
						url: this.http.urls.tool_borrowing_empty,
						data: this.item,

						success: res => {
							console.log('tool_borrowing_empty:' + JSON.stringify(res));
							//this.item = res.data.data.items;
							console.log('this.bitem:' + JSON.stringify(this.bitem));
							this.btitem.id = res.data.data.id;
							this.btitem.formId = res.data.data.formid;
							this.btitem.versions = res.data.data.versions;
							this.btitem.parentId = this.bitem.id;
							//this.btitem.parent=this.bitem.id;
							this.btitem.items = res.data.data.items;

							this.btitem.items.HERST = item.items.__SH6ZSHZMqHeGHDFLAaO.value; //
							this.btitem.items.TYPBZ = item.items.__drInpQcmkMf2iWseaRR.value; //
							this.btitem.items.EQKTX = item.items.__FwAgiHdxI4bCZaKNs4A.value; //
							this.btitem.items.FBL = item.items.__Tyyw12PleIHLPjjJWsV.value; //
							this.btitem.items.TPLNR = item.items.__a7cgJzxvCbcNNH0KjCn.value; //
							this.btitem.items.EQUNR = item.items.__b5UmDDfLaCJJdn5F4JF.value; //
							this.btitem.items.NUMBER = item.items.__2sfNVgOh5Bzq6lXRVuM.value;
							this.btitem.items.CLFW = item.items.__XcfZD8dzEQb5qURydpP.value; //
							this.btitem.items.borrowing_date = new Date(); //借用日期
							this.btitem.items.ZQDDJ = item.items.__Rhr5ihIebMolSIr8CKn.value; //
							this.btitem.items.ZDYXWC = item.items.__ARvSmuzLwNLg6TlB4tV.value; //
							this.btitem.items.STTXU = item.items.__SBtfwetYprHFQ5J2MAn.value; //
							this.btitem.items.SERGE = item.items.__qyobrAFN4PtseWXiY5A.value; //
							this.btitem.items.INTNUM = item.items.__BEViJOuGBdPWSxES5Z5.value; //
							this.btitem.items.NPLDA = item.items.__R8QpJNSHAVFaYwHnVCo.value; //
							//this.btitem.items.toolstate=0;//借用日期


							this.btitem.items.DEVID = item.items.__O1qcaazksjAzDcrvJLk.value;
							console.log('this.btitem1:' + JSON.stringify(this.btitem));
							this.validate();


							//console.log('res2:' + JSON.stringify(res.data.data));
						}
					});

				});
			},
			//提交
			validate() {


				this.http.request({
					url: this.http.urls.validate +
						'?parentId=' +
						this.btitem.parentId,
					data: this.btitem,
					method: 'POST',
					success: res => {
						console.log('validate1:' + JSON.stringify(res));
                       this.updateparent(this.btitem.parentId,this.btitem.id);
					}
				});
			},
			updateparent(parent,id) {
			
			
				this.http.request({
					url: this.http.urls.ip2+'liug/app?parent='+parent+'&id='+id+'&table=tlk_tool_borrowing',
					
					method: 'POST',
					success: res => {
						console.log('validate1:' + JSON.stringify(res));
			
					}
				});
			},
			//提交流程面板
			submission() {

				//this.validatet();
				this.http.request({
					url: this.http.urls.documents + this.bitem.id + '/panels/submission',
					data: this.items,

					method: 'POST',
					success: res => {
						//this.item=res.data.data;
						//console.log('zx22:' + JSON.stringify(res));
						this.show = true;

						this.title = res.data.data.name;
						this.nextNodes = res.data.data.nextNodes;
						this.itemt.flowId = res.data.data.flowId;
						this.itemt.flowType = '80';
						this.itemt.currentNodeId = res.data.data.currentNodeId;
						this.itemt.attitude = '';
						this.itemt.signature = '';
						this.itemt.submitTo = '';
						this.itemt.circulatorInfo = '';
						this.itemt.subFlowApproverInfo = '';
						this.itemt.subFlowApproverInfoAll = '';
						this.itemt.document = this.items.document;
						//this.content=res.data.data.formname;
					}
				});
			}, //提交流程
			submit() {
				//this.itemt.document.items.purpose="试验目的5656";
				//this.itemt.document.items.title="标题5656";

				if (this.itemt.document.items.borrower == '' || this.itemt.document.items.borrower == null) {
					uni.showModal({
						title: '提示',
						content: '请填写借用人',
						showCancel: false,
						success: res => {
							if (res.confirm) {
								//uni.navigateBack();
							}
						}
					});

					return;
				}
				if (this.itemt.document.items.tel == '' || this.itemt.document.items.tel == null) {
					uni.showModal({
						title: '提示',
						content: '请填写手机号',
						showCancel: false,
						success: res => {
							if (res.confirm) {
								//uni.navigateBack();
							}
						}
					});

					return;
				}





				this.itemt.applicationId = '__gAPYBW4YxB3UePM3lqO';
				this.itemt.docId = this.iteml.id;
				this.itemt.id = this.iteml.id;
				this.itemt.formId = this.iteml.formid;
				this.itemt.parentId = '';
				this.itemt.subFlowApprover = [];
				this.itemt.subSelects = [];
				this.itemt.submitTo = [];
				this.itemt.templateForm = '';
				this.itemt.viewId = '';
				this.http.request({
					url: this.http.urls.documents + this.id + '/workflows/submit',
					data: this.itemt,

					method: 'PUT',
					success: res => {
						//this.item=res.data.data;
						console.log('zx22:' + JSON.stringify(res));
						if (res.data.errcode == 0) {

							uni.showModal({
								title: '提示',
								content: res.data.data,
								showCancel: false,
								success: res => {
									if (res.confirm) {
										uni.navigateBack();
									}
								}
							});

							this.tool_borrowing_empty();
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.errmsg,
								showCancel: false,
								success: res => {
									if (res.confirm) {
										//uni.navigateBack();
									}
								}
							});

						}


						this.show = false;

						this.title = res.data.data.name;
						this.nextNodes = res.data.data.nextNodes;

					}
				});
			}, //刷新数据
			//提交
			add() {
				this.http.request({
					url: this.http.urls.user_task_add,
					data: this.item,
					method: 'POST',
					success: res => {
						uni.showModal({
							title: '提示',
							content: '提交成功',
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack();
								}
							}
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.page {
		padding-bottom: 80px;
	}

	.item {
		margin-bottom: 10px;
	}

	.del {
		width: 70%;
		background-color: #e64340;
	}
</style>
