<template>
	<!-- 设备 -->
	<view class="add-form-container">
		<Form :form='form' :data='data' ref="form1" @chickRightButton='chickRightButton' @blur='blur'></Form>
		<view class="footer">
			<view class="btns">
				<button class="reset" @click="reset">重置</button>
				<button class="sure" @click="submit">确定</button>
			</view>
			<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
		</view>
	</view>
</template>

<script>
	import Form from '@/components/form/index.vue'
	import * as deviceApi from "@/api/device.js"
	export default {
		components: {
			Form
		},
		data() {
			return {
				type:'',
				form: {
					disabled: false,
					visible: true,
					loading: false,
					labelWidth: '180rpx',
					border: true,
					errorType: ['toast'],
					formData: [{
						type: 'text',
						field: 'code',
						label: '报修主题',
						disabled: false,
						required: true,
						value: ''
					}, {
						type: 'text',
						field: 'name',
						label: '设备名称',
						disabled: false,
						required: true,
						value: '',
						isRightButton: '扫描'
					}, {
						type: 'text',
						field: 'location',
						label: '存放位置',
					}, {
						type: 'singleColumn',
						field: 'type',
						label: '紧急程度',
						required: true,
						list: [{
							label: '程度1',
							value: 1
						}, {
							label: '程度2',
							value: 2
						}, {
							label: '程度3',
							value: 3
						}]
					}, {
						type: 'text',
						field: 'location',
						label: '故障描述',
					}],
				},
				data: {
					name: '',
					sex: 2,
					deviceNumber:'',
					deviceName: '',
					factoryName:  '',
					time: '2023-12-12 08:00:00'
				}
			}
		},
		methods: {
			// 扫描设备条码
			chickRightButton(field) {
				// 允许从相机和相册扫码
				if(field == 'deviceNumber'){
					uni.scanCode({
						success: function (res) {
							this.data.deviceNumber = res.result
							this.getDetailsByNumber()
						}
					});
				}
			},
			blur(val,field){
				console.log(field)
				this.data.deviceNumber = val
				if(field == 'deviceNumber'){
					this.getDetailsByNumber()
				}
			},
			// 根据设备/模具号查询信息
			getDetailsByNumber(){
				const data = {
					number :this.data.deviceNumber,
					type:this.type,
					id:''
				}
				console.log(data)
				deviceApi.getDetailsByNumber(data).then((res)=>{
					this.data.deviceName = res.data.name
					this.data.factoryName = res.data.factoryName
					this.$forceUpdate()
				})
			},
			// 触发提交表单
			submit() {
				// 校验
				if(this.type == 1){
					
				}
				// for (let i = 0; i < this.form.formData.length; i++) {
				// 	if (this.form.formData[i].required === true) {
				// 		if (!this.data[this.form.formData[i].field]) {
				// 			let placeholder = '请输入'
				// 			if (this.form.formData[i].type == 'text' || this.form.formData[i].type == 'textarea') {
				// 				placeholder = '请输入'
				// 			} else {
				// 				placeholder = '请选择'
				// 			}
				// 			this.$modal.showToast(placeholder + this.form.formData[i].label)
				// 			return;
				// 		}
				// 	}
				// }
				console.log('表单数据信息1：', this.data);
			},
			// 重置
			reset() {
				console.log('表单数据信息1：', this.data);
				this.data = {}
			},

		},
		onLoad(option) {
			this.type = option.type
			if(option.type == 1){
				uni.setNavigationBarTitle({
					title:'添加报修'
				})
				this.type = 'DEVICE'
				this.form= {
					disabled: false,
					visible: true,
					loading: false,
					labelWidth: '180rpx',
					border: true,
					errorType: ['toast'],
					formData: [{
						type: 'text',
						field: 'describes',
						label: '故障描述',
						disabled: false,
						required: true,
						value: ''
					}, {
						type: 'text',
						field: 'deviceNumber',
						label: '设备编码',
						disabled: false,
						required: true,
						value: '',
						isRightButton: '扫描'
					}, {
						type: 'text',
						field: 'deviceName',
						label: '设备名称',
						required: true,
					}, {
						type: 'text',
						field: 'factoryName',
						label: '所属厂区',
						required: true,
					}],
				}
			}else if(option.type == 2){
				uni.setNavigationBarTitle({
					title:'添加维修工单'
				})
			}else if(option.type == 3){
				uni.setNavigationBarTitle({
					title:'添加检修工单'
				})
			}else if(option.type == 4){
				uni.setNavigationBarTitle({
					title:'添加点检工单'
				})
			}else if(option.type == 5){
				uni.setNavigationBarTitle({
					title:'添加保养工单'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add-form-container {
		min-height: calc(100vh - 140rpx);
		background: white;
		padding: 0px 30rpx 140rpx;
	}

	.list {
		padding-bottom: 20rpx;

		.item {
			margin-top: 20rpx;
			background: white;
			padding: 30rpx;
			display: flex;
			align-items: center;

			image {
				width: 160rpx;
				height: 160rpx;
				margin-right: 20rpx;
			}

			.title {
				font-size: 32rpx;
				font-weight: bold;
			}

			.dec1 {
				font-size: 28rpx;
				margin-top: 16rpx;
				color: #acacac;
			}

			.dec2 {
				font-size: 28rpx;
				margin-top: 6rpx;
				color: #acacac;
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 0px;
		left: 0px;
		width: 100%;
	}

	.btns {
		display: flex;


		button {
			flex: 1;
		}

		.sure {
			background: #409eff;
			color: white;
			border-radius: 0px;

			&::after {
				border: 1px solid #409eff;
				border-radius: 0px;
			}
		}

		.reset {
			background: #F5F5F5;
			border-radius: 0px;

			&::after {
				border-radius: 0px;
			}
		}
	}
</style>