<template>
	<!-- 添加维修工单 -->
	<view class="add-form-container">
		<u-form :model="form" ref="formRef" label-width="160rpx">
			<u-form-item label="申请主题" prop="name" required>
				<u-input v-model="form.name" placeholder="请输入申请主题" />
			</u-form-item>
		</u-form>
		<view class="list">
			<view class="title">
				<span>*</span>申请备件
			</view>
			<view class="item " v-for="(item,index) in form.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="24">
							<view class="dec" v-if="item.type">
								类型:{{`${item.type=='DEVICE'?'设备' :item.type=='MOLD'?'模具' : ''}`}}
							</view>
						</u-col>
						<u-col :span="24">
							<view class="dec" v-if="item.deviceName">
								{{`${item.type=='DEVICE'?'设备' : '模具'}`}}:{{item.deviceName}}
							</view>
						</u-col>
						<u-col :span="24">
							<view class="dec">
								是否以旧换新:{{item.isRadeIn=='TRUE'?'是' :'否'}}
							</view>
						</u-col>
						<u-col :span="24">
							<view class="dec">
								库存:{{item.currentQty}}
							</view>
						</u-col>
						<u-col :span="24">
							<view class="dec">
								数量:{{item.qty}}
							</view>
						</u-col>
					</u-row>
				</view>
				<u-icon name="minus-circle" color="#aaaaaa" size="60" @click="delSpareParts(index)"></u-icon>
			</view>
			<view class="add-btn">
				<u-button type="primary" @click="open"><u-icon name="plus-circle" color="#ffffff"
						size="36"></u-icon>添加备件</u-button>
			</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-popup v-model="isPopupShow" mode="center" border-radius="14">
			<view class="popup-title">添加备件</view>
			<view class="popup">
				<u-form :model="form1" ref="form1Ref" label-width="200rpx">
					<u-form-item :label="`备件`" prop="number" required>
						<view class="select" @click="openSingleColumn('itemNumber',form1.itemNumber,sparePartsList)">
							<view class="input" v-if='form1.itemNumber'>
								{{form1.name}}
							</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="type">
						<view class="select" @click="openSingleColumn('type',form1.type,deviceMoldType)">
							<view class="input" v-if='form1.type'>
								{{form1.typeName}}
							</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="`${form1.type=='DEVICE'?'设备' : '模具'}`" prop="deviceNumber">
						<view class="select" @click="openSingleColumn('deviceNumber',form1.deviceNumber,deviceList)">
							<view class="input" v-if='form1.deviceNumber'>
								{{form1.deviceName}}
							</view>
							<view class="placeholder" v-else>
								{{`请选择${form1.type=='DEVICE'?'设备' : '模具'}`}}
							</view>
							<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label="是否以旧换新" prop="isRadeIn" required class="disabled">
						<u-radio-group v-model="form1.isRadeIn" :key="updataKey" disabled>
							<u-radio :name="'TRUE'">是</u-radio>
							<u-radio :name="'FALSE'">否</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="库存" prop="currentQty" required class="disabled">
						<u-input v-model="form1.currentQty" type="number" disabled placeholder="请输入库存" />
					</u-form-item>
					<u-form-item label="数量" prop="qty" required>
						<u-input v-model="form1.qty" type="number" placeholder="请输入数量" />
					</u-form-item>
				</u-form>
			</view>
			<view class="popup-footer">
				<view @click="isPopupShow = false">取消</view>
				<view class="sure" @click="addSpare">确认</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 sparePartsApplicationApi from "@/api/sparePartsApplication.js"
	import * as sparePartsApi from "@/api/spareParts.js"
	import * as deviceApi from "@/api/device.js"
	import * as dictApi from "@/api/dict.js"
	import * as moldApi from "@/api/mold.js"

	const { proxy } = getCurrentInstance()
	const loading = ref(false)
	const type = ref('')
	// 备件弹窗
	const isPopupShow = ref(false)
	const sparePartsList = ref([])

	const singleColumnShow = ref(false)
	const singleColumnDefaultValue = ref([])
	const singleColumnList = ref([])
	const field = ref('')
	const deviceList = ref([])//设备列表
	const choosesingleColumnItem = ref([])
	const form = ref({
		name: '',
		subList: []
	})
	const form1 = ref({
		itemNumber: undefined,
		type: '',
		deviceNumber: undefined,
		isRadeIn: '',
		currentQty: '',
		qty: ''
	})
	const deviceMoldType = ref([])//设备列表
	const updataKey = ref(0)
	// 触发提交表单
	function submit() {
		// 校验
		if (!form.value.name) {
			proxy.$modal.showToast('请输入主题')
			return;
		}
		if (form.value.subList.length === 0) {
			proxy.$modal.showToast('请选择备件')
			return;
		}
		proxy.$modal.confirm('是否添加备件申请').then(() => {
			proxy.$modal.loading('加载中')
			loading.value = true
			sparePartsApplicationApi.sparePartsApplicationCreate(form.value).then((res) => {
				proxy.$modal.closeLoading()
				if (res.data) {
					proxy.$modal.showToast('添加成功')
					setTimeout(() => {
						proxy.$tab.navigateBack()
						loading.value = false
					}, 1500)
				} else {
					proxy.$modal.showToast('添加失败')
					loading.value = false
				}
			}).catch(() => {
				proxy.$modal.closeLoading()
				loading.value = false
			})
		})

	}
	// 重置
	function reset() {
		form.value = {
			name: '',
			subList: []
		}
	}
	// 单列模式
	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) {
		form1.value[field.value] = e[0].value
		if (field.value == 'itemNumber') {
			form1.value.name =  e[0].label
			choosesingleColumnItem.value = singleColumnList.value.filter(item => item.number == e[0].value)
			form1.value.isRadeIn = choosesingleColumnItem.value[0].isRadeIn
			form1.value.currentQty = choosesingleColumnItem.value[0].qty
			updataKey.value++
			
		}
		if (field.value == 'type') {
			form1.value.typeName =  e[0].label
			form1.value.deviceNumber = ''
			form1.value.deviceName = ''
		}
		if (field.value == 'deviceNumber') {
			form1.value.deviceName =  e[0].label
		}
		
		getApplyDeviceList()
		singleColumnShow.value = false
	}
	// 获取备件列表
	async function getApplySparePartsList() {
		await sparePartsApi.getApplySparePartsList().then(res => {
			res.data.map(item => {
				item.value = item.number
				item.label = item.name
			})
			sparePartsList.value = res.data
		}).catch(() => { })
	}
	// 获取设备列表
	async function getApplyDeviceList() {
		if (form1.value.type == 'DEVICE') {
			await deviceApi.getApplyDeviceList().then(res => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			}).catch(() => { })
		} else if (form1.value.type == 'MOLD') {
			moldApi.getApplyMoldList().then((res) => {
				res.data.map(item => {
					item.value = item.number
					item.label = item.name
				})
				deviceList.value = res.data
			}).catch(() => { })
		}
	}
	// 打开弹窗
	function open() {
		form1.value = {
			itemNumber: undefined,
			type: '',
			deviceNumber: undefined,
			isRadeIn: '',
			currentQty: "",
			qty: '',
		}
		isPopupShow.value = true
	}
	// 添加配件
	function addSpare() {
		if (!form1.value.itemNumber) {
			proxy.$modal.showToast('请选择备件')
			return;
		}
		// if (!this.form1.deviceNumber) {
		// 	this.$modal.showToast('请选择设备')
		// 	return;
		// }
		if (!form1.value.qty || form1.value.qty == 0) {
			proxy.$modal.showToast('请输入数量')
			return;
		}
		if (form1.value.qty > form1.value.currentQty) {
			proxy.$modal.showToast('数量不可以大于库存')
			return;
		}
		if (form.valuesubList && form.value.subList.length > 0) {
			let arr = form.value.subList.filter(item => item.itemNumber == form1.value.itemNumber)
			if (arr && arr.length > 0) {
				proxy.$modal.showToast('该备件已添加')
				return;
			}
		}
	
		form.value.subList.push(form1.value)
		isPopupShow.value = false
	}
	// 删除备件
	function delSpareParts(index) {
		form.value.subList.splice(index, 1)
	}
	 onLoad(async (option) => {
		if (option.type)type.value = option.type;
		if (option.number) form.value.maintenanceNumber = option.number;
		if (option.data && JSON.parse(decodeURIComponent(option.data)) && JSON.parse(decodeURIComponent(option
			.data)).id) {
			form.value = JSON.parse(decodeURIComponent(option.data))
			form.value.subList = form.value.subList
		};
		deviceMoldType.value = await dictApi.getDict('device_mold_type')
		await getApplySparePartsList()
	})
</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;
	}

	.footer {
		position: fixed;
		bottom: 0px;
		left: 0px;
		width: 100%;
		z-index: 22;
	}

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

		}
	}

	.title {
		padding: 32rpx 0rpx;
		position: relative;

		span {
			position: absolute;
			left: -16rpx;
			color: #fa3534;
			top: 19px;
		}
	}

	.list {
		padding: 0rpx 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;
			}

		}
	}

	.add-btn {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.popup-title {
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #409eff;
		padding: 30rpx 30rpx 0px
	}

	.popup {
		width: 600rpx;
		padding: 30rpx 0rpx 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;
	}
</style>