<template>
	<!-- 添加维修工单 -->
	<view class="add-form-container">
		<u-form :model="form" ref="form1" label-width="160rpx">
			<u-form-item label="类型" prop="receiverType" required>
				<view class="select" @click="openSingleColumn('receiverType',form.receiverType,deviceMoldType)">
					<view class="input" v-if='form.receiverType'>
						{{selectFormat(form.receiverType,deviceMoldType)}}
					</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="`${type=='DEVICE'||type=='TECH'?'设备' : '模具'}名称`" prop="deviceNumber" required>
				<view class="select" @click="openSingleColumn('deviceNumber',form.deviceNumber,deviceList)">
					<view class="input" v-if='form.deviceNumber'>
						{{selectFormat(form.deviceNumber,deviceList)}}
					</view>
					<view class="placeholder" v-else>
						{{`请选择${type=='DEVICE'||type=='TECH'?'设备' : '模具'}名称`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
				</view>
			</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>
	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"


	export default {
		data() {
			return {
				loading: false,
				type: "",
				deviceMoldType: [], //类型列表字典
				form: {
					id: '',
					receiverType: "",
					deviceNumber: '',
					deviceName: '',
					receiverUserId: '',
				},
				deviceList: [], //设备列表
				singleColumnShow: false,
				singleColumnDefaultValue: [],
				singleColumnList: [],
				field: '',
				// 维修人员
				selecUserList: [], //维修人员
				isShowSelecUser: false,
			}
		},
		methods: {
			// 触发提交表单
			submit() {
				// 校验
				if (!this.form.receiverType) {
					this.$modal.showToast('请选择类型')
					return;
				}
				if (!this.form.deviceNumber) {
					this.$modal.showToast(`请选择${this.type=='DEVICE'||this.type=='TECH'?'设备' : '模具'}名称`)
					return;
				}
				if (!this.form.receiverUserId) {
					this.$modal.showToast('请选择维修人员')
					return;
				}
				const data = {
					id: this.form && this.form.id ? this.form.id : '',
					receiverType: this.form.receiverType,
					deviceNumber: this.form.deviceNumber,
					receiverUserId: this.form.receiverUserId,
				}
				this.$modal.confirm('是否转办维修工单').then(() => {
					this.$modal.loading('加载中')
					this.loading = true
					repairOrderApi.transfer(data).then((res) => {
						this.$modal.closeLoading()
						if (res.data) {
							this.$modal.showToast('转办成功')
							setTimeout(() => {
								this.$tab.navigateBack(2)
								this.loading = false
							}, 1500)
						} else {
							this.$modal.showToast('转办失败')
							this.loading = false
						}
					}).catch(() => {
						this.$modal.closeLoading()
						this.loading = false
					})
				})

			},
			// 重置
			reset() {
				if (this.form.id) {
					this.form.receiverType = "";
					this.form.deviceNumber = '';
					this.form.deviceName = '';
					this.form.receiverUserId = '';
				} else {

					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
				if (this.field == 'receiverType') {
					this.type = this.form[this.field]
					this.form.deviceName = ''
					this.form.deviceNumber = ''
					this.form.receiverUserId = undefined
					this.getDeviceList()
				} else if (this.field == 'deviceNumber') {
					this.choosesingleColumnItem = this.singleColumnList.filter(item => item.number == e[0].value)
					this.form.factoryAreaName = this.choosesingleColumnItem[0].factoryAreaName
					this.form.factoryAreaNumber = this.choosesingleColumnItem[0].factoryAreaNumber
					this.form.receiverUserId = undefined
					this.getSelecUser()
				}
				this.singleColumnShow = false
				this.$emit('singleColumn', this.field, this.form[this.field])
				this.$forceUpdate()
			},
			// 获取设备/模具列表
			async getDeviceList() {
				if (this.type == 'DEVICE') {
					await deviceApi.deviceList().then(res => {
						res.data.map(item => {
							item.value = item.number
							item.label = item.name
						})
						this.deviceList = res.data
					})
				} else if (this.type == 'MOLD') {
					await moldApi.moldList().then(res => {
						res.data.map(item => {
							item.value = item.number
							item.label = item.name
						})
						this.deviceList = res.data
					})
				}

			},
			// 获取人员列表
			async getSelecUser() {
				let data = {
					classType: this.type,
					factoryAreaNumber: this.form.factoryAreaNumber,
					flag: 1
				}
				let arr = []
				if (this.form.maintenances) {
					arr = this.form.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
							}
						})
					})
					this.selecUserList = res.data
				})
			},
			// 打开人员列表
			openSelecUser(field) {
				this.field = field
				this.isShowSelecUser = true
			},
			radioGroupChange(field, e, list) {
				this.chooseUserInfo = e
			},
			// 多选选择框回显
			selectFormatRadin(val, array) {
				let str = array.filter(item => item.id == val)[0].name
				return str
			},
			// 选择人员
			chooseUser() {
				this.form[this.field] = this.chooseUserInfo
				this.isShowSelecUser = false
			},
		},
		async onLoad(option) {
			if (option.type) this.type = option.type;
			if (option.id) this.form.id = option.id;
			this.deviceMoldType = await dictApi.getDict('app_device_mold_type')
			this.deviceMoldType = this.deviceMoldType.filter(item => item.value != this.type)
		}
	}
</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;
			}
		}
	}

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