<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="作业编码" :labelWidth='90' name="pwoJobCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.pwoJobCode" type="text"
							@confirm="scanBarCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="当前设备" :labelWidth='90' name="equipmentCode">
						<uni-easyinput disabled v-model="formData.equipmentCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="切换设备" :labelWidth='90' name="newEquipmentCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar1" v-model="formData.newEquipmentCode"
							type="text" />
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		getPwoJob,
		updatePwoJob,
		getEquipment,
		listEquipment
	} from "@/api/mes/jobReport.js";

	export default {
		mounted() {
			// this.test()
		},
		data() {
			return {
				formData: {
					pwoJobCode: null,
					equipmentCode: null,
					newEquipmentCode: null,
					eqpId: null
				},
				rules: {
					pwoJobCode: {
						rules: [{
							required: true,
							errorMessage: '请输入作业编码'
						}]
					},
					newEquipmentCode: {
						rules: [{
								required: true,
								errorMessage: '请输入要切换的设备编号'
							},
							{
								pattern: '^[A-Z0-9]+$',
								errorMessage: '请输入正确格式的设备编码！',
								trigger: 'blur',
							},
						],
					}
				}
			}
		},
		methods: {

			test() {
				let ip = null;
				uni.setStorageSync('appversion', plus.runtime.version)
				// IP地址
				let Context = plus.android.importClass('android.content.Context')
				let main = plus.android.runtimeMainActivity()
				let cm = main.getSystemService(Context.CONNECTIVITY_SERVICE)
				plus.android.importClass(cm)
				let linkProperties = cm.getLinkProperties(cm.getActiveNetwork())
				let linkAddrs = plus.android.invoke(linkProperties, 'getLinkAddresses')
				plus.android.importClass(linkAddrs)
				for (var i = 0; i < linkAddrs.size(); i++) {
					let inetAddr = plus.android.invoke(linkAddrs.get(i), 'getAddress')
					ip = plus.android.invoke(inetAddr, 'getHostAddress')
				}

				// let baseUrl = uni.getStorageSync("base_url").slice(0, 20)
				console.log(ip)
				uni.request({
					url: 'http://' + ip + ':8080/api/1.0/alarm/success_start',
					success: (res) => {
						console.log(res);
					}
				});
			},
			scanBarCode() {
				if (this.formData.pwoJobCode) {
					let obj = Number(this.formData.pwoJobCode.slice(4));
					console.log(Number(this.formData.pwoJobCode.slice(4)));
					getPwoJob(obj).then(async res => {
						console.log(res);
						if (res) {
							let eqpId = res.data.eqpId;
							//判断扫描的作业编码是否绑定设备
							if (eqpId && eqpId != 0) {
								this.formData.eqpId = eqpId;
								getEquipment(eqpId).then(async res => {
									this.formData.equipmentCode = res.data.equipmentCode;
								});
							} else {
								this.$modal.msg("该作业编码未绑定设备!");
							}
						} else {
							this.formData.equipmentCode = "无";
						}
					});
				}
			},
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.newEquipmentCode = res.result;
					}
				});
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.pwoJobCode = res.result;
						_this.scanBarCode(_this.formData.pwoJobCode);
					}
				});
			},
			submit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定使用该设备上工吗？',
						success: function(res) {
							if (res.confirm) {
								let query = {
									equipmentCode: _this.formData.newEquipmentCode
								}
								//根据设备编码来获取设备id
								listEquipment(query).then(async res => {
									let eqpId1 = res.rows[0].id;
									let data = {
										id: _this.formData.pwoJobCode.slice(4),
										eqpId: res.rows[0].id
									}
									_this.$modal.loading('提交中')
									updatePwoJob(data).then(res => {
										_this.$modal.closeLoading();
										_this.$modal.msgSuccess("作业上机成功！");
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									});
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},

		}
	}
</script>

<style>


</style>