<template>
	<!-- 添加设备报修 -->
	<view class="add-form-container">
		<u-form :model="form" ref="form1" label-width="160rpx">
			<u-form-item label="点检描述" prop="describes" required>
				<u-input v-model="form.describes" placeholder="请输入点检描述" />
			</u-form-item>
			<u-form-item :label="`${type=='DEVICE'?'设备' : '模具'}编码`" prop="deviceNumber" required>
				<u-input v-model="form.deviceNumber"
					:placeholder="`请输入${type=='DEVICE'?'设备' : '模具'}编码`" @blur="blur"/>
				<view class="right-button" @click="chickRightButton">
					扫描
				</view>
			</u-form-item>
			<u-form-item :label="`${type=='DEVICE'?'设备' : '模具'}名称`" prop="deviceName" required class="disabled">
				<u-input v-model="form.deviceName" :placeholder="`请输入${type=='DEVICE'?'设备' : '模具'}名称`" disabled />
			</u-form-item>
			<u-form-item label="维修人" prop="maintenances" required class="disabled">
				<u-input v-model="form.maintenances" placeholder="请输入维修人" disabled />
			</u-form-item>
			<u-form-item label="班次" prop="classes" required>
				<view class="select" @click="openSingleColumn('classes',form.classes,maintenanceShift)">
					<view class="input" v-if='form.classes'>
						{{selectFormat(form.classes,maintenanceShift)}}
					</view>
					<view class="placeholder" v-else>
						{{`请选择班次`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
				</view>
			</u-form-item>
		</u-form>
		<view class="list">
			<!-- <view class="title">
				<span>*</span>备件
			</view> -->
			<view class="item " v-for="(item,index) in subList" :key="index">
				<view class="item-box">
					<view class="spare-title">
						<view class="title-txt">
							{{item.name}}
						</view>
					</view>
					<u-row gutter="16">
						<u-col :span="12">
							<view class="dec">
								<view class="">
									设备部位名称:
								</view>
								<view>{{item.equipmentParts}}</view>
							</view>
							<view class="dec">
									<view style="margin-right: 20rpx;">是否符合:</view><u-switch v-model="item.isConform"></u-switch>
							
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btns">
				<button class="reset" @click="reset">重置</button>
				<button class="sure" @click="submit" :loading='loading' :disabled='loading'>确定</button>
			</view>
			<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
		</view>
		<u-select v-model="singleColumnShow" mode="single-column" :default-value='singleColumnDefaultValue'
			:list="singleColumnList" @confirm="chooseSingleColumn"></u-select>
	</view>
</template>

<script>
	import * as deviceApi from "@/api/device.js"
	import * as spotCheckOrderApi from "@/api/spotCheckOrder.js"
	import * as dictApi from "@/api/dict.js"
	export default {
		data() {
			return {
				loading: false,
				type: "",
				form: {
					describes: "",
					deviceNumber: '',
					deviceName: '',
					maintenances:'',
					classes: ''
				},
				singleColumnShow: false,
				singleColumnDefaultValue: [],
				singleColumnList: [],
				maintenanceShift: [], //班次字典项
				subList:[],//二级列表
			}
		},
		methods: {
			// 扫描设备条码
			chickRightButton(field) {
				uni.scanCode({
					success: function(res) {
						this.form.deviceNumber = res.result
						this.getDetailsByNumber()
						this.getSubList()
					}
				});
			},
			blur() {
				if (this.form.deviceNumber) {
					this.getDetailsByNumber()
					this.getSubList()
				}
			},
			// 根据设备/模具号查询信息
			getDetailsByNumber() {
				const data = {
					number: this.form.deviceNumber
				}
				deviceApi.getDeviceDetailsByNumber(data).then((res) => {
					if (res.data) {
						this.form.deviceName = res.data.name
					} else {
						this.$modal.showToast(`找不到该${this.type=='DEVICE'?'设备' : '模具'}`)
					}
				})
			},
			// 根据设备获取二级列表
			getSubList() {
				const data = {
					number: this.form.deviceNumber
					
				}
				deviceApi.getSubList(data).then((res) => {
					if (res.data) {
						this.subList = res.data
					} else {
						this.$modal.showToast(`找不到该${this.type=='DEVICE'?'设备' : '模具'}`)
					}
				})
			},
			// 触发提交表单
			submit() {
				// 校验
				if (!this.form.describes) {
					this.$modal.showToast('请输入点检描述')
					return;
				}
				if (!this.form.deviceNumber) {
					this.$modal.showToast(`请输入${this.type=='DEVICE'?'设备' : '模具'}编码`)
					return;
				}
				if (!this.form.classes) {
					this.$modal.showToast('请选择班次')
					return;
				}
				const data = {
					describes: this.form.describes,
					deviceNumber: this.form.deviceNumber,
					maintenances: this.form.maintenances,
					classes:this.form.classes,
					subList:this.subList,
					type: this.type
				}
				this.$modal.confirm('是否添加点检工单').then(() => {
					this.$modal.loading('加载中')
					this.loading = true
					spotCheckOrderApi.spotCheckOrderCreate(data).then((res) => {
						this.$modal.closeLoading()
						if (res.data) {
							this.$modal.showToast('添加成功')
							setTimeout(() => {
								this.$tab.navigateBack()
								this.loading = false
							}, 1500)
						} else {
							this.$modal.showToast('添加失败')
							this.loading = false
						}
					}).catch(() => {
						this.$modal.closeLoading()
						this.loading = false
					})
				})

			},
			// 重置
			reset() {
				console.log('表单数据信息1：', this.data);
				this.form = {}
			},
			selectFormat(val, array) {
				let str = array.filter(item => item.value == val)[0].label
				return str
			},
			// 单列模式
			openSingleColumn(field, val, list) {
				this.singleColumnList = list
				this.field = field
				if (val) {
					this.singleColumnDefaultValue = [list.findIndex(item => item.value == val)]
				} else {
					this.singleColumnDefaultValue = []
				}
				this.singleColumnShow = true
			},
			// 单列模式点击确定之后
			chooseSingleColumn(e) {
				console.log(e[0])
				this.form[this.field] = e[0].value
				this.singleColumnShow = false
				this.$emit('singleColumn', this.field, this.form[this.field])
				this.$forceUpdate()
			},

		},
		async onLoad(option) {
			if (option.type) this.type = option.type;
			this.maintenanceShift = await dictApi.getDict('maintenance_shift')
			this.form.maintenances =  uni.getStorageSync('user').nickname
		}
	}
</script>

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

	.u-form-item {
		padding: 20rpx 30rpx;
	}

	.disabled {
		background: #f5f5f5;
	}

	.list {
		padding: 30rpx 30rpx;
		.item {
			display: flex;
			margin-bottom: 20rpx;
	
			.item-box {
				background: #F5F5F5;
				border-radius: 12rpx;
				flex: 1;
				width: 0rpx;
			}
	
			.spare-title {
				padding: 20rpx 30rpx;
				border-bottom: 1px solid #e4e4e4;
	
				.title-txt {
					color: #409eff;
					font-size: 30rpx;
					font-weight: bold;
				}
			}
	
			.dec {
				color: #9c9c9c;
				padding: 20rpx 30rpx 20rpx;
				display: flex;
				align-items: center;
			}
	
		}
	}
	

	.select {
		display: flex;
		align-items: center;
		height: 72rpx;
		width: 100%;

		.input {
			flex: 1;
			font-size: 28rpx;
			color: #000000;
		}

		.placeholder {
			flex: 1;
			font-size: 28rpx;
			color: rgb(192, 196, 204);

		}
	}

	.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;
			}
		}
	}

	.right-button {
		background: #409eff;
		color: white;
		padding: 0rpx 30rpx;
		border-radius: 16rpx;
		text-align: center;
		font-size: 28rpx;
	}
</style>