<template>
	<!-- 添加设备报修 -->
	<view class="add-form-container">
		<u-form :model="form" ref="form1" label-width="160rpx">
			<u-form-item label="故障描述" prop="describes" required>
				<u-input v-model="form.describes" placeholder="请输入故障描述" />
			</u-form-item>
			<u-form-item label="所属厂区" prop="factoryAreaNumber" required>
				<view class="select" @click="openSingleColumn('factoryAreaNumber',form.factoryAreaNumber,factoryAreaList)">
					<view class="input" v-if='form.factoryAreaNumber'>
						{{selectFormat(form.factoryAreaNumber,factoryAreaList)}}
					</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'?'设备' : '模具'}`" 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'?'设备' : '模具'}`}}
					</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,appDeviceMoldType)">
					<view class="input" v-if='form.receiverType'>
						{{selectFormat(form.receiverType,appDeviceMoldType)}}
					</view>
					<view class="placeholder" v-else>
						{{`请选择类型`}}
					</view>
					<u-icon name="arrow-right" color="#aaaaaa" size="28"></u-icon>
				</view>
			</u-form-item>
			<view class="image">
				<view class="image-list" v-if='imgList.length>0'>
					<view class="image-item" v-for="(item,index) in imgList" :key="index">
						<image :src="item" mode=""></image>
						<u-icon name="close-circle-fill" color="red" class="close" size="36" @click="delImage(index)">
						</u-icon>
					</view>
				</view>
				<view class="image-item image-item1" @click="chooseImage" v-if='imgList.length<3'>
					<image src="../../static/images/photo.png" mode=""></image>
				</view>
			</view>
		</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>
	import * as deviceApi from "@/api/device.js"
	import * as moldApi from "@/api/mold.js"
	import * as dictApi from "@/api/dict.js"
	import * as uploadApi from "@/api/upload.js"
	import * as deptApi from "@/api/dept.js"

	export default {
		data() {
			return {
				loading: false,
				type: "",
				appDeviceMoldType: [],
				factoryAreaList:[],//工厂列表
				deviceList:[],//设备列表
				form: {
					describes: "",
					deviceNumber: '',
					deviceName: '',
					factoryAreaName: '',
					factoryAreaNumber: '',
					receiverType: ''
				},
				singleColumnShow: false,
				singleColumnDefaultValue: [],
				singleColumnList: [],
				imgList: []
			}
		},
		methods: {
			// 扫描设备条码
			chickRightButton(field) {
				uni.scanCode({
					success: function(res) {
						this.form.deviceNumber = res.result
						this.getDetailsByNumber()
					}
				});
			},
			blur() {
				if (this.form.deviceNumber) {
					this.getDetailsByNumber()
				}
			},
			// 获取厂区列表
			getFactoryAreaList() {
				deptApi.getFactoryAreaList().then((res) => {
					if (res.data&&res.data.length>0) {
						res.data.map(item => {
							item.value = item.id
							item.label = item.name
						})
						this.factoryAreaList = res.data
					} else{
							this.factoryAreaList =[]
							}
				})
			},
			// 根据设备/模具号查询信息
			getDetailsByNumber() {
				if (this.type == 'DEVICE') {
					deviceApi.getDeviceByFactoryAreaNumber(this.form.factoryAreaNumber).then((res) => {
						if (res.data&&res.data.length>0) {
							res.data.map(item => {
								item.value = item.number
								item.label = item.name
							})
							this.deviceList = res.data
						}else{
							this.deviceList = []
						}
					})
				} else if (this.type == 'MOLD') {
					moldApi.getMoldByFactoryAreaNumber(this.form.factoryAreaNumber).then((res) => {
						if (res.data&&res.data.length>0) {
							res.data.map(item => {
								item.value = item.number
								item.label = item.name
							})
							this.deviceList = res.data
						}else{
							this.deviceList = []
						}
					})
				}
			},
			// 触发提交表单
			submit() {
				// 校验
				if (!this.form.describes) {
					this.$modal.showToast('请输入故障描述')
					return;
				}
				if (!this.form.factoryAreaNumber) {
					this.$modal.showToast(`请选择选择所属厂区`)
					return;
				}
				if (!this.form.deviceNumber) {
					this.$modal.showToast(`请选择${this.type=='DEVICE'?'设备' : '模具'}`)
					return;
				}
				if (!this.form.receiverType) {
					this.$modal.showToast(`请选择类型'}`)
					return;
				}
				if (this.imgList.length == 0) {
					this.$modal.showToast('请选择图片')
					return;
				}
				const data = {
					describes: this.form.describes,
					deviceNumber: this.form.deviceNumber,
					factoryAreaNumber: this.form.factoryAreaNumber,
					receiverType: this.form.receiverType,
					type: this.type,
					filePathList: this.imgList,
				}
				this.$modal.confirm('是否添加报修').then(() => {
					this.$modal.loading('加载中')
					this.loading = true
					deviceApi.deviceRepairCreate(data).then((res) => {
						this.$modal.closeLoading()
						if (res.data) {
							this.$modal.showToast('添加成功')
							setTimeout(() => {
								this.$tab.navigateBack()
								this.loading = false
							}, 1500)
						} else {
							this.$modal.showToast('添加失败')
							this.loading = false
						}
					}).catch(() => {
						this.$modal.closeLoading()
						this.loading = false
					})
				})

			},
			// 重置
			reset() {
				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) {
				this.form[this.field] = e[0].value
				if(this.field == 'factoryAreaNumber'){
					this.form.deviceNumber = ''
					this.form.deviceName = ''
					this.getDetailsByNumber()
				}
				this.singleColumnShow = false
				this.$emit('singleColumn', this.field, this.form[this.field])
				this.$forceUpdate()
			},
			chooseImage() {
				uni.chooseImage({
					count: 1, // 默认9
					sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (res) => {
						let filePath = res.tempFilePaths[0]
						this.$modal.loading()
						uploadApi.uploadFile({
							filePath: filePath,
							name: 'file',
							formData: {
								'user': 'test'
							},
						}).then(ret => {
							this.$modal.closeLoading()
							this.imgList.push(ret.data)
							console.log(ret)
						}).catch((err) => {
							this.$modal.closeLoading()
						});
					},
				});
			},
			// 删除图片
			delImage(index) {
				this.imgList.splice(index, 1)
			}
		},
		async onLoad(option) {
			if (option.type) this.type = option.type;
			this.appDeviceMoldType = await dictApi.getDict('app_device_mold_type')
			this.appDeviceMoldType = this.appDeviceMoldType.filter((item=>item.value!="MOLD"))
			this.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;
			}
		}
	}

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

		}
	}

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

	.image {
		padding: 0px 30rpx;
		padding-bottom: 30rpx;
		display: flex;
		align-items: center;

		flex-wrap: wrap;

		.image-list {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
		}
	}

	.image-item {
		width: 210rpx;
		height: 210rpx;
		margin-right: 20rpx;
		position: relative;
		border: 1px solid rgba(230, 230, 230, 0.5);
		margin-bottom: 20rpx;
		border-radius: 20rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}

		.close {
			position: absolute;
			right: 0px;
			top: 0px;
		}
	}

	.image-item1 {
		border: none;
		margin-right: 0px;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>