<template>
	<view>
		<uni-forms ref="form" :model="formData" :rules="rules">
			<uni-row>
				<uni-forms-item label="刀柄编码" :labelWidth='90' name="cutterShankCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.cutterShankCode"
						type="text" />
				</uni-forms-item>
				<uni-forms-item label="设备编码" :labelWidth='90' name="equipmentCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarequipmentCode" v-model="formData.equipmentCode"
						type="text" />
				</uni-forms-item>
				<uni-forms-item label="安装员" :labelWidth='90' name="installCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarinstallBy" @change="selectinstallBy"
						v-model="installCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="操作员" :labelWidth='90' name="operatorCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBaroperator" @change="selectoperator"
						v-model="operatorCode" type="text" />
				</uni-forms-item>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listUseRecord,
		getUseRecord,
		delUseRecord,
		addStartUse,
		updateEndUse,
		listEntity,
		listMaterial,
		listEquipment,
		getEntity,
		installShank
	} from "@/api/basic/cutter.js";
	import {
		listEmployee
	} from "@/api/mes/jobIn.js";

	export default {
		data() {
			return {
				code: null,
				formData: {
					cutterShankCode: null,
					equipmentCode: null,
					installBy: null,
					operator: null
				},
				installCode: null,
				operatorCode: null,
				value: 0,
				range: [],
				entityOptions: [],
				materialOptions: [],
				rules: {
					equipmentCode: {
						rules: [{
								required: true,
								errorMessage: '请输入设备编码！',
								trigger: 'blur'
							},
							{
								pattern: '^[A-Z0-9]+$',
								errorMessage: '请输入正确格式的设备编码！',
								trigger: 'blur',
							},
						]
					},
					cutterShankCode: {
						rules: [{
							required: true,
							errorMessage: '请输入刀柄编码！'
						}]
					},
				}
			}
		},
		mounted() {},
		methods: {
			//扫描物料编码带出绑定的刀具信息
			scanBarCode() {
				if (this.formData.materialCode) {
					console.log(this.formData.materialCode)
					this.range = [];
					//判断是否扫描二维码
					if (typeof(this.formData.materialCode) === 'string') {
						// this.formData.materialCode = this.code;
						this.formData.materialName = this.materialOptions.find(item => item.materialCode == this.formData
							.materialCode).materialName;
						if (this.formData.materialCode != "") {
							listEntity({
								cutterMaterialCode: this.formData.materialCode,
								cutterMaterialName: this.formData.materialName,
							}).then((resp) => {
								this.range = resp.rows;
								for (var i = 0; i < resp.rows.length; i++) {
									this.range[i].value = resp.rows[i].id;
									this.range[i].text = resp.rows[i].entityCode + ':' + (!resp.rows[i].pieceNo ?
										'' : resp.rows[i].pieceNo) + ':' + (!resp.rows[i].batchNo ? '' : resp
										.rows[i].batchNo)
								}
								this.entityOptions = resp.rows;
							});
						} else {
							this.entityOptions = [];
						}
					} else if (typeof(this.formData.materialCode) === 'object') {
						if (this.formData.materialCode.id) {
							this.formData.cutterEntityId = this.formData.materialCode.id;
							getEntity(this.formData.materialCode.id).then(async res => {
								this.range.push(res.data);
								this.entityOptions = this.range;
								this.formData.cutterEntityCode = this.range[0].entityCode;
								this.range[0].value = this.range[0].id;
								this.range[0].text = this.range[0].entityCode + ':' + (!this.range[0].pieceNo ?
									'' : this.range[0].pieceNo) + ':' + (!this.range[0].batchNo ? '' : this
									.range[0].batchNo)
							})
						}
						this.formData.materialName = this.formData.materialCode.cutterMaterialName;
						this.formData.materialCode = this.formData.materialCode.cutterMaterialCode;

					}

				}

			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.cutterShankCode = res.result;
					}
				});
			},
			//根据设备编码获取设备id
			getEquipmentId() {
				if (this.formData.equipmentCode) {
					listEquipment({
						equipmentCode: this.formData.equipmentCode
					}).then((resp) => {
						if (resp.rows.length > 0) {
							this.formData.equipmentId = resp.rows[0].id;
						}
					});
				}
			},

			scanBarequipmentCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.equipmentCode = res.result;
						// _this.getEquipmentId();
					}
				});
			},

			selectoperator(code) {
				listEmployee({
					empCode: code
				}).then(async res => {
					if (res.rows.length > 0) {
						this.formData.operator = res.rows[0].name;
					} else {
						this.$modal.msg("未查询到该人员，请重新输入！")
					}
				})
			},
			selectinstallBy(code) {
				listEmployee({
					empCode: code
				}).then(async res => {
					if (res.rows.length > 0) {
						this.formData.installBy = res.rows[0].name;
					} else {
						this.$modal.msg("未查询到该人员，请重新输入！")
					}
				})
			},
			scanBarinstallBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.installCode = res.result;
						_this.selectinstallBy(res.result);
					}
				});
			},
			scanBaroperator() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.operatorCode = res.result;
						_this.selectoperator(res.result);
					}
				});
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					if (this.formData.equipmentCode && this.formData.cutterShankCode) {
						console.log(this.formData);
						//判断安装员是否填入
						setTimeout(() => {
							if (this.formData.installBy) {
								this.$modal.loading('提交中')
								installShank(this.formData).then(res => {
									this.$modal.closeLoading();
									this.$modal.msgSuccess("刀柄上机成功！")
									setTimeout(() => {
										this.$tab.switchTab("/pages/work/index");
									}, 500);
								})
							} else {
								this.$modal.msg("请输入安装员！")
							}
						}, 500);
					}
				})
			}
		}
	}
</script>

<style>
</style>