<template>
	<!-- 添加维修工单 -->
	<view class="add-form-container">
		<u-form :model="form" ref="formRef" label-width="160rpx">
			<u-form-item label="转办类型" prop="status" required>
				<view class="select" @click="openSingleColumn('status',form.status,tansferType)">
					<view class="input" v-if='form.status || form.status == 0'>
						{{form.statusTxt}}
					</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="receiverType" required>
				<view class="select" @click="openSingleColumn('receiverType',form.receiverType,deviceMoldType)">
					<view class="input" v-if='form.receiverType'>
						{{form.receiverTypeTxt}}
					</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="factoryAreaNumber" required v-if="form.status==1">
				<view class="select"
					@click="openSingleColumn('factoryAreaNumber',form.factoryAreaNumber,factoryAreaList)">
					<view class="input" v-if='form.factoryAreaNumber'>
						{{form.factoryAreaName}}
					</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="`${form.receiverType=='DEVICE'||form.receiverType == 'TECH'?'设备' : '模具'}编码`" prop="deviceNumber" required
				v-if="form.status==0">
				<u-input v-model="form.deviceNumber" :placeholder="`请输入${form.receiverType=='DEVICE'||form.receiverType == 'TECH'?'设备' : '模具'}编码`"
					@blur="blur()" />
				<view class="right-button" @click="chickRightButton">
					扫描
				</view>
			</u-form-item>
			<u-form-item :label="`${form.receiverType=='DEVICE'||form.receiverType == 'TECH'?'设备' : '模具'}名称`" prop="deviceNumber" required
				class="disabled" v-if="form.status==0">
				<u-input v-model="form.deviceName" :placeholder="`根据${form.receiverType=='DEVICE'||form.receiverType == 'TECH'?'设备' : '模具'}编码获得`"
					disabled />
			</u-form-item>
			<u-form-item label="维修人员" prop="describes" required>
				<view class="select" @click="openSelecUser('receiverUserId')">
					<view class="input" v-if='form.receiverUserId'>
						{{selectFormatRadin(form.receiverUserId,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>
		<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-popup v-model="isShowSelecUser" mode="center" border-radius="14">
			<view class="popup-title">选择维修人员</view>
			<view class="popup">
				<u-radio-group v-model="form.receiverUserId"
					@change="radioGroupChange('receiverUserId',form.receiverUserId,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>
		<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 deviceApi from "@/api/device.js"
	import * as moldApi from "@/api/mold.js"
	import * as dictApi from "@/api/dict.js"
	import * as repairOrderApi from "@/api/repairOrder.js"
	import * as deptApi from "@/api/dept.js"
	const { proxy } = getCurrentInstance()

	const loading = ref(false)
	const type = ref('')
	const deviceMoldType = ref([])
	const deviceMoldType1 = ref([])
	const form = ref({
		id: '',
		receiverType: "",
		receiverTypeTxt: '',
		deviceNumber: '',
		deviceName: '',
		receiverUserId: '',
		status: 0,
		statusTxt: '设备',
		factoryAreaNumber: '',
		factoryAreaName: ''
	})
	const deviceList = ref([])
	const singleColumnShow = ref(false)
	const singleColumnDefaultValue = ref([])
	const singleColumnList = ref([])
	const field = ref('')
	//维修人员
	const selecUserList = ref([])//维修人员
	const isShowSelecUser = ref(false)
	// 转办类型
	const tansferType = ref([{
		value: 0,
		label: '设备'
	}, {
		value: 1,
		label: '人员'
	}])
	const deviceNumber = ref('')//主页面设备编码

	const factoryAreaList = ref([])
	const choosesingleColumnItem = ref([])
	const chooseUserInfo = ref({})
	// 触发提交表单
	function submit() {
		// 校验
		if (!form.value.receiverType) {
			proxy.$modal.showToast('请选择类型')
			return;
		}
		if (form.value.status == 0) {
			if (!form.value.deviceNumber) {
				proxy.$modal.showToast(`请选择${form.value.receiverType == 'DEVICE' || form.value.receiverType == 'TECH' ? '设备' : '模具'}编码`)
				return;
			}
		}

		if (!form.value.factoryAreaNumber) {
			proxy.$modal.showToast('请选择厂区')
			return;
		}
		if (!form.value.receiverUserId) {
			proxy.$modal.showToast('请选择维修人员')
			return;
		}
		const paramsData = {
			id: form.value && form.value.id ? form.value.id : '',
			receiverType: form.value.receiverType,
			deviceNumber: form.value.status == 0 ? form.value.deviceNumber : deviceNumber.value,
			receiverUserId: form.value.receiverUserId,
			factoryAreaNumber: form.value.factoryAreaNumber,
			status: form.value.status,
		}
		proxy.$modal.confirm('是否转办维修工单').then(() => {
			proxy.$modal.loading('加载中')
			loading.value = true
			repairOrderApi.transfer(paramsData).then((res) => {
				proxy.$modal.closeLoading()
				if (res.data) {
					proxy.$modal.showToast('转办成功')
					setTimeout(() => {
						proxy.$tab.navigateBack(2)
						loading.value = false
					}, 1500)
				} else {
					proxy.$modal.showToast('转办失败')
					loading.value = false
				}
			}).catch(() => {
				proxy.$modal.closeLoading()
				loading.value = false
			})
		})

	}
	// 重置
	function reset() {
		if (form.value.id) {
			form.value = {
					receiverType: "",
					deviceNumber: '',
					deviceName: '',
					receiverUserId: '',
					status: 0,
					statusTxt: '设备',
					factoryAreaNumber: '',
					factoryAreaName: '',
				}
			form.value.receiverType = deviceMoldType.value[0].value
			form.value.receiverTypeTxt = deviceMoldType.value[0].label
					selecUserList.value = []
		} else {
			form.value = {
				id: '',
				receiverType: "",
				deviceNumber: '',
				deviceName: '',
				receiverUserId: '',
				status: 0,
				statusTxt: '设备',
				factoryAreaNumber: '',
					factoryAreaName: '',
			}
		form.value.receiverType = deviceMoldType.value[0].value
		form.value.receiverTypeTxt = deviceMoldType.value[0].label
					selecUserList.value = []
		}
	}
	// 扫描设备条码
	function chickRightButton(field) {
		if (!form.value.receiverType) {
			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 (form.value.receiverType == 'DEVICE' || form.value.receiverType == 'TECH') {
			deviceApi.getDeviceDetailsByNumber({
				number: form.value.deviceNumber,
				type: form.value.receiverType
			}).then(async (res) => {
				if (!res.data) {
					form.value.deviceName = ''
					form.value.factoryAreaName = ''
					form.value.factoryAreaNumber = ''
					selecUserList.value = []
					return;
				}
				form.value.deviceName = res.data.name
				form.value.factoryAreaName = res.data.factoryAreaName
				form.value.factoryAreaNumber = res.data.factoryAreaNumber
				await getSelecUser()
			}).catch(() => {
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
				selecUserList.value = []
				form.value.receiverUserId = ''
			})
		} else if (form.value.receiverType == 'MOLD') {
			moldApi.getMoldDetailsByNumber({
				number: form.value.deviceNumber,
			}).then(async (res) => {
				if (!res.data) {
					form.value.deviceName = ''
					form.value.factoryAreaName = ''
					form.value.factoryAreaNumber = ''
					selecUserList.value = []
				form.value.receiverUserId = ''
					return;
				}
				form.value.deviceName = res.data.name
				form.value.factoryAreaName = res.data.factoryAreaName
				form.value.factoryAreaNumber = res.data.factoryAreaNumber
				await getSelecUser()
			}).catch(() => {
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
				selecUserList.value = []
				form.value.receiverUserId = ''
			})
		}
	}

	// 单列模式
	function openSingleColumn(fieldName, val, list) {
		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 == 'receiverType') {
			form.value.deviceName = ''
			form.value.deviceNumber = ''
			form.value.receiverUserId = undefined
			form.value.receiverTypeTxt = e[0].label
			selecUserList.value = []
				form.value.receiverUserId = ''
			getDeviceList()
		} else if (field.value == 'factoryAreaNumber') {
			form.value.factoryAreaName =  e[0].label
				form.value.receiverUserId = ''
			
			getSelecUser()
		} else if (field.value == 'status') {
			form.value.statusTxt = e[0].label
			if (form.value.status == 0) {
				deviceMoldType.value = deviceMoldType1.value.filter(item => item.value != type.value)
			} else {
				deviceMoldType.value = deviceMoldType1.value.filter(item => item.value == type.value)
			}
			console.log(deviceMoldType.value)
			form.value.receiverType = deviceMoldType.value[0].value
			form.value.receiverTypeTxt = deviceMoldType.value[0].label
			selecUserList.value = []
				form.value.receiverUserId = ''
		}
		singleColumnShow.value = false
	}

	// 获取设备/模具列表
	async function getDeviceList() {
		if (form.value.receiverType == 'DEVICE' || form.value.receiverType == 'TECH') {
			await deviceApi.deviceList().then(res => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			})
		} else if (form.value.receiverType == 'MOLD') {
			await moldApi.moldList().then(res => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			})
		}

	}
	// 获取人员列表
	async function getSelecUser() {
		let data = {
			classType: form.value.receiverType,
			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 openSelecUser(fieldName) {
		field.value = fieldName
		isShowSelecUser.value = true
	}
	function radioGroupChange(field, e, list) {
		chooseUserInfo.value = e
	}
	// 多选选择框回显
	function selectFormatRadin(val, array) {
		let str = array.filter(item => item.id == val)[0].name
		return str
	}
	// 选择人员
	function chooseUser() {
		form.value[field.value] = chooseUserInfo.value
		isShowSelecUser.value = false
	}
	// 获取厂区列表
	function getFactoryAreaList() {
		deptApi.getFactoryAreaList().then((res) => {
			if (res.data && res.data.length > 0) {
				res.data.map(item => {
					item.value = item.id
					item.label = item.name
				})
				factoryAreaList.value = res.data
			} else {
				factoryAreaList.value = []
			}
		}).catch(() => { })
	}
	onLoad(async (option) => {
		if (option.type) type.value = option.type;
		if (option.id) form.value.id = option.id;
		if (option.deviceNumber) deviceNumber.value = option.deviceNumber;

		deviceMoldType1.value = await dictApi.getDict('app_device_mold_type')
		if (form.value.status == 0) {
			deviceMoldType.value = deviceMoldType1.value.filter(item => item.value != type.value)
		} else {
			deviceMoldType.value = deviceMoldType1.value.filter(item => item.value == type.value)
		}
		form.value.receiverType = deviceMoldType.value[0].value
		form.value.receiverTypeTxt = deviceMoldType.value[0].label
		getFactoryAreaList()
	})
</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-radio-group {
		display: grid !important;
	}
</style>