<template>
	<!-- 添加维修工单 -->
	<view class="add-form-container">
		<u-form :model="form" ref="formRef" label-width="160rpx">
			<u-form-item label="故障描述" prop="describes" required>
				<u-input v-model="form.describes" placeholder="请输入故障描述" :disabled="form&&form.id?true:false" />
			</u-form-item>
			<u-form-item label="类型" prop="type" required>
				<view class="select" @click="openSingleColumn('type',form.type,appDeviceMoldType)">
					<view class="input" v-if='form.type'>
						{{form.typeName}}
					</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'||type == 'TECH'?'设备' : '模具'}名称`" prop="deviceNumber" required>
				<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'||type == 'TECH'?'设备' : '模具'}名称`}}
					</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'||type == 'TECH'?'设备' : '模具'}编码`" prop="deviceNumber" required>
				<u-input v-model="form.deviceNumber" :placeholder="`请输入${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码`"
					@blur="blur()"  @confirm='blur()'/>
				<view class="right-button" @click="chickRightButton">
					扫描
				</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="factoryAreaNumber" required class="disabled">
				<u-input v-model="form.factoryAreaNumber"
					:placeholder="`根据${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码获得`" disabled />
			</u-form-item>
			<u-form-item :label="`厂区名称`" prop="factoryAreaName" required class="disabled">
				<u-input v-model="form.factoryAreaName"
					:placeholder="`根据${type=='DEVICE'||type == 'TECH'?'设备' : '模具'}编码获得`" 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||form.classes==0)&&maintenanceShift.length>0'>
						{{form.classesName}}
					</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="classes" 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>
		<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 setup lang="ts">
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import * as deviceApi from "@/api/device"
	import * as moldApi from "@/api/mold"
	import * as dictApi from "@/api/dict"
	import * as repairOrderApi from "@/api/repairOrder"
	import { useCountStore } from '@/store'
	
	const { proxy } = getCurrentInstance()
	// 获取自定义的store
	const store = useCountStore()

	const loading = ref(false)
	const appDeviceMoldType = ref([])
	const type = ref('')
	const form = ref({
		id: '',
		describes: "",
		deviceNumber: '',
		deviceName: '',
		factoryAreaName: '',
		factoryAreaNumber: '',
		classes: '',
		type: '',
		faultType: ''
	})

	const deviceList = ref([])
	const singleColumnShow = ref(false)
	const singleColumnDefaultValue = ref([])
	const singleColumnList = ref([])
	const field = ref('')
	const maintenanceShift = ref([])
	const faultType = ref([])
	const choosesingleColumnItem = ref()
	const msg = ref('')
	// 扫描设备条码
	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(() => {
				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(() => {
				form.value.deviceName = ''
				form.value.factoryAreaName = ''
				form.value.factoryAreaNumber = ''
			})
		}
	}
	// 触发提交表单
	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.factoryAreaName) {
			proxy.$modal.showToast('请输入区域')
			return;
		}
		if (!form.value.classes) {
			proxy.$modal.showToast('请选择班次')
			return;
		}
		const data = {
			id: form.value && form.value.id ? form.value.id : '',
			describes: form.value.describes,
			type: form.value.type,
			deviceNumber: form.value.deviceNumber,
			factoryAreaNumber: form.value.factoryAreaNumber,
			classes: form.value.classes,
			faultType: form.value.faultType
		}
		if (form.value.id) {
			proxy.$modal.confirm('是否修改维修工单').then(() => {
				proxy.$modal.loading('加载中')
				loading.value = true
				repairOrderApi.repairOrderUpdate(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
				repairOrderApi.repairOrderCreate(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() {
		if (form.value.id) {
			form.value.classes = undefined;
			form.value.faultType = undefined
		} else {
			form.value = {}
		}
	}
	
	// 单列模式
	function openSingleColumn(fieldName, val, list) {
		if ((fieldName == 'deviceNumber' || fieldName == 'type') && form.value.id) 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.factoryAreaName = choosesingleColumnItem.value[0].factoryAreaName
			form.value.factoryAreaNumber = choosesingleColumnItem.value[0].factoryAreaNumber
			form.value.deviceName = choosesingleColumnItem.value[0].name
		}else if(field.value == 'classes'){
			form.value.classesName =e[0].label
		}else if(field.value == 'faultType'){
			form.value.faultTypeName =e[0].label
		}else if(field.value == 'type'){
			form.value.typeName =e[0].label
		}
		singleColumnShow.value = false
	}
	// 获取设备/模具列表
	async function getDeviceList() {
		if (type.value == 'DEVICE') {
			await deviceApi.deviceList().then(res => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			}).catch(() => { })
		} else if (type.value == 'MOLD') {
			await moldApi.moldList().then(res => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			}).catch(() => { })
		}

	}
	const appDeviceMoldType1 = ref([])
	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,
				deviceNumber: data.deviceNumber,
				deviceName: data.name,
				factoryAreaName: data.factoryAreaName,
				factoryAreaNumber: data.factoryAreaNumber,
				classes: data.classes || data.classes == 0 ? data.classes : undefined,
				faultType: data.faultType,
				type: data.type
			}
		};
		await getDeviceList()
		maintenanceShift.value = await dictApi.getDict('maintenance_shift')
		appDeviceMoldType1.value = await dictApi.getDict('app_device_mold_type')
		faultType.value = await dictApi.getDict('fault_type')
		if (type.value == 'DEVICE') {
			appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value == "DEVICE"))
		}else if (type.value == 'MOLD') {
			appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value == "MOLD"))
		} else if (type.value == 'TECH')  {
			appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value == "TECH"))
			console.log(88,store.roles)
			// if(store.roles.some(item=>item.indexOf('device')>-1)){
			// 	appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value == "DEVICE"))
			// }
			// if(store.roles.some(item=>item.indexOf('tech')>-1)){
			// 	appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value == "TECH"))
			// }
			// if(store.roles.some(item=>item.indexOf('tech')>-1&&item.indexOf('device')>-1)){
			// 	appDeviceMoldType.value = appDeviceMoldType1.value.filter((item => item.value != "MOLD"))
			// }
		}

		form.value.type = appDeviceMoldType.value[0].value
		form.value.typeName = appDeviceMoldType.value[0].label
	})
</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);

		}
	}
</style>