<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">
			<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.title" placeholder="输入借用人" />
				<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" v-if="op == '设备借用'">
			<view class="lable">借用人部门</view>
			<view class="tag">
				<text class="span">{{ item.UNIT }}</text>
				<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.__b5UmDDfLaCJJdn5F4JF.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">设备名称</view>
				<view class="tag">
					<text class="span">{{ item.items.__FwAgiHdxI4bCZaKNs4A.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">型号规格</view>
				<view class="tag">
					<text class="span">{{ item.items.__drInpQcmkMf2iWseaRR.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">测量范围</view>
				<view class="tag">
					<text class="span">{{ item.items.__XcfZD8dzEQb5qURydpP.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">最大允许误差</view>
				<view class="tag">
					<text class="span">{{ item.items.__ARvSmuzLwNLg6TlB4tV.value }}</text>
				</view>
			</view>
			<view class="form_group">
				<view class="lable">内部编号</view>
				<view class="tag">
					<text class="span">{{ item.items.__BEViJOuGBdPWSxES5Z5.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">提交</button></view>
				<view class="f"><button class="btn" @click="validate()">保存</button></view>
			</view>
		</view>
	</view>
</template>

<script>
let qrcode = require('@/common/code.js');
export default {
	data() {
		return {
			array1: ['类型1', '类型2', '类型3', '类型4'], //实验类型1
			bitem: {
				applicationId: '__gAPYBW4YxB3UePM3lqO',
				exparams: {},
				formId: '__0FLGNncOS1lz0T9jsFn',
				id: '__79Wtw03ya4dJUDGPyAN',
				isRelate: 'undefined',
				parentId: '',
				sign: '',
				subDocuments: [],
				templateForm: '',
				versions: '0'
			},
			geteq: {
				DEVID: '202209211603170016',
				allow: 'false',
				appId: '__gAPYBW4YxB3UePM3lqO',
				datetime: '1670485443880',
				detp: '',
				fieldid: '__iLwKGJTxD89ZKnFzo2Y',
				formId: '__w9J1gxcIWgXmNw6pE1F',
				isEdit: 'true',
				mutil: 'true',
				parentid: '__NYI0rVs0LXbiW21z6Nv',
				searchForm: 'false',
				selectOne: 'false',
				viewId: '__WLVxKX6e0Pq0cTwdNUQ'
			},
			item: {},
			op: '设备归还',
			current: 0,
			code: '',
			list: []
		};
	},
	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: {
		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;
						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.eq_list +
					'parentId=' +
					this.geteq.parentid +
					'&sortCol=&sortStatus=&_currpage=1&lines=10&treedocid=&parentNodeId=&_docid=&_fieldid=false&isRelate=&startDate=&endDate=&parentParam=&isQueryButton=false&newTime=1670485908904&_=0.5036780865403778',
				data: this.geteq,
				method: 'POST',
				success: res => {
					console.log('res2:' + JSON.stringify(res));
					if (res.data.errcode == 0) {
						console.log('res2:' + JSON.stringify(res.data.data.data));
						res.data.data.data.forEach(item => {
							this.list.push(item);
						});
					}
				}
			});
		},
		empty() {
			var url = '';
			if (this.op == '设备借用') {
				url = this.http.urls.borr_empty;
			} else {
				url = this.http.urls.return_empty;
			}
			this.http.request({
				url: url,
				data: this.item,

				success: 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;
					//console.log('res2:' + JSON.stringify(res.data.data));
				}
			});
		},
		//移除借用设备信息
		del(item){
			this.list.splice(this.list.indexOf(item),1);
		},
		//提交
		validate() {
			this.bitem.items = this.item;
			this.bitem.items.ghz = '20221208测试app保存';
			this.http.request({
				url: this.http.urls.validate,
				data: this.bitem,
				method: 'POST',
				success: res => {
					//console.log('res:' + JSON.stringify(res));

					uni.showModal({
						title: '提示',
						content: res.data.errmsg,
						showCancel: false,
						success: res => {
							if (res.confirm) {
								uni.navigateBack();
							}
						}
					});
				}
			});
		},
		//提交
		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>
