<template>
	<!-- 添加维修工单 -->
	<view class="add-form-container">
		<u-form :model="form" ref="formRef" label-width="160rpx">
			<u-form-item label="检修描述" prop="describes" required :class="form.id?'disabled':''">
				<u-input v-model="form.describes" placeholder="请输入检修描述" :disabled="form&&form.id?true:false" />
			</u-form-item>
			<u-form-item label="维修工单" prop="maintenanceNumber" :class="form.id?'disabled':''">
				<view class="select" @click="openOrder">
					<view class="input" v-if='form.maintenanceNumber'>
						{{form.maintenanceNumber}}
					</view>
					<view class="placeholder" v-else>
						{{`请选择维修工单`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28" v-if="!form.id"></u-icon>
				</view>
			</u-form-item>
			<!-- <u-form-item :label="`${type=='DEVICE'?'设备' : '模具'}名称`" prop="factoryAreaName" required :class="isDisabled?'disabled':''">
				<view class="select" @click="openSingleColumn('deviceNumber',form.deviceNumber,deviceList)">
					<view class="input" v-if='form.deviceName'>
						{{form.deviceName}}
					</view>
					<view class="placeholder" v-else>
						{{`请输入${type=='DEVICE'?'设备' : '模具'}名称`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28" v-if="!form.id"></u-icon>
				</view>
			</u-form-item> -->
			<u-form-item label="类型" prop="deviceNumber" required class="disabled">
				{{type =='DEVICE'||type == 'TECH'?'设备' : '模具'}}
			</u-form-item>
			<u-form-item :label="`${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码`" prop="deviceNumber" required
				:class="isDisabled?'disabled':''">
				<u-input v-model="form.deviceNumber" :placeholder="`请输入${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码`"
					@blur="blur()"  @confirm='blur()'/>
				<view class="right-button" @click="chickRightButton" v-show="!isDisabled">
					扫描
				</view>
			</u-form-item>
			<u-form-item :label="`${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}名称`" prop="deviceNumber" required
				class="disabled">
				<u-input v-model="form.deviceName" :placeholder="`根据${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码获得`"
					disabled />
			</u-form-item>
			<u-form-item label="所属厂区" prop="factoryAreaName" class="disabled">
				<u-input v-model="form.factoryAreaName" placeholder="请输入所属厂区" disabled />
			</u-form-item>
			<u-form-item label="故障类型" prop="faultType" required>
				<view class="select" @click="openSingleColumn('faultType',form.faultType,faultType)">
					<view class="input" v-if='form.faultType'>
						{{form.faultTypeName}}
					</view>
					<view class="placeholder" v-else>
						{{`请选择故障类型`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
				</view>
			</u-form-item>
			<u-form-item label="维修人员" prop="maintenance" required>
				<view class="select" @click="openSelecUser('maintenance')">
					<view class="input" v-if='form.maintenance'>
						{{selectFormatRadin(form.maintenance,selecUserList)}}
					</view>
					<view class="placeholder" v-else>
						请选择维修人员
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
				</view>
			</u-form-item>
		</u-form>
		<!-- 选择维修人员 -->
		<u-popup v-model="isShowSelecUser" mode="center" border-radius="14">
			<view class="popup-title">选择维修人员</view>
			<view class="popup">
				<u-radio-group v-model="form.maintenance"
					@change="radioGroupChange('maintenance',form.maintenance,selecUserList)">
					<u-radio v-for="(item, index) in selecUserList" :key="index" :name="item.id">
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</view>
			<view class="popup-footer">
				<view @click="isShowSelecUser = false">取消</view>
				<view class="sure" @click="chooseUser">确认</view>
			</view>
		</u-popup>
		<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" @cancle='singleColumnShow = false'></u-select>
	</view>
</template>

<script setup lang="ts">
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import * as overhaulOrderApi from "@/api/overhaulOrder.js"
	import * as moldApi from "@/api/mold.js"
	import * as dictApi from "@/api/dict.js"
	import * as repairOrderApi from "@/api/repairOrder.js"
	import * as deviceApi from "@/api/device.js"
	import * as deptApi from "@/api/dept.js"
	const { proxy } = getCurrentInstance()

	const loading = ref(false)
	const type = ref('')
	const form = ref({
		id: '',
		describes: "",
		maintenanceNumber: '',
		factoryAreaNumber: '',
		factoryAreaName: '',
		faultType: '',
		deviceName: '',
		deviceNumber: '',
		faultTypeName: '',
		maintenance:''
	})


	const singleColumnShow = ref(false)
	const singleColumnDefaultValue = ref([])
	const singleColumnList = ref([])
	const field = ref('')
	const faultType = ref([])
	const isDisabled = ref(false)
	const choosesingleColumnItem = ref([])
	const msg = ref('')
	//维修人员
	const selecUserList = ref([])//维修人员
	const isShowSelecUser = ref(false)
	const chooseUserInfo = ref(null)
	function submit() {
		// 校验
		if (!form.value.describes) {
			proxy.$modal.showToast('请输入故障描述')
			return;
		}
		
		if (!form.value.deviceNumber) {
			proxy.$modal.showToast(`请选择${type.value == 'DEVICE' || type.value == 'TECH' ? '设备' : '模具'}名称`)
			return;
		}
		if (!form.value.deviceName) {
			proxy.$modal.showToast(msg.value)
			return;
		}
		if (!form.value.faultType) {
			proxy.$modal.showToast('请选择故障类型')
			return;
		}
		if (!form.value.maintenance) {
			proxy.$modal.showToast('请选择维修人员')
			return;
		}
		const data = {
			id: form.value && form.value.id ? form.value.id : '',
			describes: form.value.describes,
			maintenanceNumber: form.value.maintenanceNumber,
			deviceNumber: form.value.deviceNumber,
			factoryAreaNumber: form.value.factoryAreaNumber,
			type: type.value,
			faultType: form.value.faultType,
			maintenance:form.value.maintenance
		}
		if (form.value.id) {
			proxy.$modal.confirm('是否修改检修工单').then(() => {
				proxy.$modal.loading('加载中')
				loading.value = true
				overhaulOrderApi.overhaulOrderUpdate(data,type.value).then((res) => {
					proxy.$modal.closeLoading()
					if (res.data) {
						proxy.$modal.showToast('修改成功')
						setTimeout(() => {
							proxy.$tab.navigateBack()
							loading.value = false
						}, 1500)
					} else {
						if(res.msg){
							proxy.$modal.showToast(res.msg)
						}else{
							proxy.$modal.showToast('添加失败')
						}
						loading.value = false
					}
				}).catch(() => {
					proxy.$modal.closeLoading()
					loading.value = false
				})
			})
		} else {
			proxy.$modal.confirm('是否添加检修工单').then(() => {
				proxy.$modal.loading('加载中')
				loading.value = true
				overhaulOrderApi.overhaulOrderCreate(data,type.value).then((res) => {
					proxy.$modal.closeLoading()
					if (res.data) {
						proxy.$modal.showToast('添加成功')
						setTimeout(() => {
							proxy.$tab.navigateBack()
							loading.value = false
						}, 1500)
					} else {
						if(res.msg){
							proxy.$modal.showToast(res.msg)
						}else{
							proxy.$modal.showToast('添加失败')
						}
						loading.value = false
					}
				}).catch(() => {
					proxy.$modal.closeLoading()
					loading.value = false
				})
			})
		}

	}
	// 重置
	function reset() {
		form.value = {
			id: '',
			describes: "",
			maintenanceNumber: '',
			factoryAreaNumber:'',
			factoryAreaName:'',
			faultType: '',
			deviceName:'',
			deviceNumber:'',
			faultTypeName:'',
			maintenance:''
		}
		isDisabled.value = false
	}
	// 单列模式
	function openSingleColumn(fieldName, val, list) {
		if (fieldName == 'maintenanceNumber' && form.value.id) return;
		if (fieldName == 'deviceNumber' && isDisabled.value) return;
		singleColumnList.value = list
		field.value = fieldName
		if (val) {
			singleColumnDefaultValue.value = [list.findIndex(item => item.value == val)]
		} else {
			singleColumnDefaultValue.value = []
		}
		singleColumnShow.value = true
	}
	// 单列模式点击确定之后
	function chooseSingleColumn(e) {
		form.value[field.value] = e[0].value

		if (field.value == 'deviceNumber') {
			choosesingleColumnItem.value = singleColumnList.value.filter(item => item.number == e[0].value)
			form.value.deviceName = choosesingleColumnItem.value[0].name
			form.value.factoryAreaName = choosesingleColumnItem.value[0].factoryAreaName
			form.value.factoryAreaNumber = choosesingleColumnItem.value[0].factoryAreaNumber
		} else if (field.value == 'faultType') {
			form.value.faultTypeName = e[0].label
		}
		singleColumnShow.value = false
	}
	
	// 扫描设备条码
	function chickRightButton(field) {
		if (!form.value.type) {
			proxy.$modal.showToast('请先选择类型')
			return;
		}
		uni.scanCode({
			success: function (res) {
				form.value.deviceNumber = res.result
				getDetailsByNumber()
			}
		});
	}
	function blur() {
		if (form.value.deviceNumber) {
			getDetailsByNumber()
		} else {
			form.value.deviceName = ''
			form.value.factoryAreaName = ''
			form.value.factoryAreaNumber = ''
		}
	}
	// 根据设备/模具号查询信息
	function getDetailsByNumber() {
		if (type.value == 'DEVICE' || type.value == 'TECH') {
			deviceApi.getDeviceDetailsByNumber({
				number: form.value.deviceNumber,
				type: type.value
			}).then((res) => {
				if (!res.data) {
					msg.value = res.msg
					form.value.deviceName = ''
					form.value.factoryAreaName = ''
					form.value.factoryAreaNumber = ''
					return;
				}
				form.value.deviceName = res.data.name
				form.value.factoryAreaName = res.data.factoryAreaName
				form.value.factoryAreaNumber = res.data.factoryAreaNumber
			}).catch((error) => {
				msg.value = error.msg
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
			})
		} else if (type.value == 'MOLD') {
			moldApi.getMoldDetailsByNumber({
				number: form.value.deviceNumber
			}).then((res) => {
				if (!res.data) {
				msg.value = res.msg
					form.value.deviceName = ''
					form.value.factoryAreaName = ''
					form.value.factoryAreaNumber = ''
					return;
				}
				form.value.deviceName = res.data.name
				form.value.factoryAreaName = res.data.factoryAreaName
				form.value.factoryAreaNumber = res.data.factoryAreaNumber
			}).catch((error) => {
				msg.value = error.msg
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
			})
		}
	}
	function openOrder() {
		proxy.$tab.navigateTo(`/pages/overhaulOrder/reqairOrderList?type=` + type.value)
	}
	onLoad(async (option) => {
		if (option.type) type.value = option.type;
		if (option.data) {
			let data = JSON.parse(decodeURIComponent(option.data))
			form.value = {
				id: data.id,
				describes: data.describes,
				maintenanceNumber: data.maintenanceNumber,
				deviceName: data.name,
				deviceNumber: data.deviceNumber,
				factoryAreaName: data.factoryAreaName,
				faultType: data.faultType,
			}
			isDisabled.value = form.value.maintenanceNumber || form.value.id ? true : false
		};
		faultType.value = await dictApi.getDict('fault_type')
		uni.$on('chooseOrder', data => {
			if (data) {
				form.value.maintenanceNumber = data.number
				form.value.deviceName = data.name
				form.value.deviceNumber = data.deviceNumber
				form.value.deviceName = data.name
				form.value.factoryAreaName = data.factoryAreaName
				form.value.factoryAreaNumber = data.factoryAreaNumber
				isDisabled.value = true
			} else {
				form.value.maintenanceNumber = ''
				form.value.deviceName = ''
				form.value.deviceNumber = ''
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
				isDisabled.value = false
			}

		})
	})
	
	// 打开人员列表
	function openSelecUser(fieldName) {
		field.value = fieldName
		getSelecUser();
		isShowSelecUser.value = true
	}
	function radioGroupChange(field, e, list) {
		form.value.maintenance = e;
		chooseUserInfo.value = e
	}
	// 选择人员
	function chooseUser() {
		if(!chooseUserInfo.value){
			proxy.$modal.showToast('请选择人员')
			return;
		}
		form.value[field.value] = chooseUserInfo.value
		isShowSelecUser.value = false
	}
	// 获取人员列表
	async function getSelecUser() {
		let data = {
			classType: type.value,
			factoryAreaNumber: form.value.factoryAreaNumber,
		}
		let arr = []
		if (form.value.maintenances) {
			arr = form.value.maintenances.split(',')
		}
		await deptApi.getSelecUser(data).then(res => {
			res.data.forEach(item => {
				item.checked = false
				arr.forEach(cur => {
					if (item.id == cur) {
						item.checked = true
					}
				})
			})
			selecUserList.value = res.data
		})
	}
	
	// 多选选择框回显
	function selectFormatRadin(val, array) {
		let str = array.filter(item => item.id == val)[0].name
		return str
	}
</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-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;
			}
		}
	}

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

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

		}
	}
	.popup-title {
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #409eff;
		padding: 30rpx 30rpx 0px
	}
	
	.popup {
		width: 600rpx;
		padding: 30rpx 60rpx 30rpx;
	
	}
	
	.popup-footer {
		display: flex;
		border-top: 1px solid #e4e4e4;
	
		view {
			line-height: 100rpx;
			flex: 1;
			text-align: center;
	
			&.sure {
				color: #409eff;
			}
		}
	}
	
	::v-deep .u-checkbox-group {
		display: grid !important;
	}
	.item-name{
		font-size: 30rpx;
		font-weight: bold;
		padding: 20rpx 0px 0px;
	}
	.item-dec{
		color: #8f8f8f;
	}
</style>