<template>
	<!-- 详情 -->
	<view class="detail-container">
		<view class="info">
			<view class="title">
				<view>维修工单</view>
				<view>转办</view>
			</view>
			<view class="dec">
				<view class="dec-item">
					<view>工单单号</view>
					<view>{{data.number}}</view>
				</view>
				<view class="dec-item">
					<view>报修描述</view>
					<view>{{data.describes}}</view>
				</view>
				<view class="dec-item">
					<view>班次</view>
					<view>{{data.classesName}}</view>
				</view>
				<view class="dec-item">
					<view>故障类型</view>
					<view>{{data.faultTypeName}}</view>
				</view>
				<view class="dec-item">
					<view>维修工</view>
					<view>{{data.maintenance}}</view>
				</view>
			</view>
		</view>
		<view class="info" style="padding-bottom: 130rpx;">
			<view class="tab">
				<u-tabs :list="list" :is-scroll="false" bar-height="2" bar-width="250" v-model="current"
					@change="change"></u-tabs>
			</view>

			<view>
				<view class="title">
					<view>{{changeItem.name}}</view>
				</view>
				<view class="dec" v-if="current == 0">
					<view class="dec-item">
						<view>设备编号</view>
						<view>{{deviceInfo.number}}</view>
					</view>
					<view class="dec-item">
						<view>设备名称</view>
						<view>{{deviceInfo.name}}</view>
					</view>
					<view class="dec-item">
						<view>所属厂区</view>
						<view>{{deviceInfo.factoryAreaName}}</view>
					</view>
					<view class="dec-item">
						<view>设备类型</view>
						<view>{{deviceInfo.typeName}}</view>
					</view>
				</view>
				<view class="dec" v-if="current == 1">
					<view class="dec-item">
						<view>工单单号</view>
						<view>{{data.number}}</view>
					</view>
					<view class="dec-item">
						<view>报修描述</view>
						<view>{{data.describes}}</view>
					</view>
					<view class="dec-item">
						<view>维修工</view>
						<view>{{data.maintenance}}</view>
					</view>
				</view>
				<view class="dec" v-if="current == 2">
					<SparePartsCard :subForm='subForm' :data='data' @addSubForm='addSubForm'
						@delSubFormItem='delSubFormItem'>
					</SparePartsCard>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btns">
				<u-button type="primary" v-if="data.status == 'PENDING'" @click="orderClick()" :loading='loading' :disabled='loading'>接单</u-button>
				<u-button type="primary" v-if="data.status == 'PECEIVED'" @click="orderClick()" :loading='loading' :disabled='loading'>完成</u-button>
				<u-button type="primary" v-if="data.status == 'COMPLETED'" @click="orderClick()" :loading='loading' :disabled='loading'>验证完结</u-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">
				<Form :form='subForm' :data='data' ref="form2"></Form>
			</view>
			<view class="popup-footer">
				<view @click="cancle">取消</view>
				<view class="sure" @click="addSpare">确认</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import Form from '@/components/form/index.vue'
	import SparePartsCard from '@/components/sparePartsCard/index.vue'
	import * as deviceApi from "@/api/device.js"
	import * as repairOrderApi from "@/api/repairOrder.js"
	export default {
		components: {
			Form,
			SparePartsCard
		},
		data() {
			return {
				list: [{
					name: '设备信息'
				}, {
					name: '报修信息'
				}, {
					name: '维修记录'
				}],
				current: 0,
				changeItem: '',
				data: '', //工单信息
				loading: false,
				type: "",
				deviceInfo: '', //设备信息
				isPopupShow: false,
				subForm: {
					formLabel: '',
					formField: 'subList',
					required: true,
					isShowButton: true,
					disabled: false,
					visible: true,
					loading: false,
					labelWidth: '180rpx',
					border: true,
					errorType: ['toast'],
					formData: [{
						type: 'singleColumn',
						field: 'name',
						label: '模具',
						disabled: false,
						required: true,
						span: 12,
						list: [{
							label: '类型1',
							value: 1
						}, {
							label: '类型2',
							value: 2
						}, {
							label: '类型3',
							value: 3
						}]
					}, {
						type: 'text',
						field: 'stork',
						label: '库存',
						disabled: false,
						required: true,
						isList: true,
						span: 6
					}, {
						type: 'text',
						field: 'number',
						label: '数量',
						disabled: false,
						required: true,
						isList: true,
						span: 6
					}],
				},

			}
		},
		methods: {
			change(index) {
				console.log("index", index);
				this.current = index
				this.changeItem = this.list[this.current]
			},
			// 添加表格表单
			addSubForm() {
				console.log(5)
				this.isPopupShow = true
			},
			// 添加备件
			addSpare() {

			},
			// 取消添加备件
			cancle() {
				this.isPopupShow = false
			},
			// 根据设备/模具号查询信息
			getDetailsByNumber() {
				const data = {
					number: this.data.deviceNumber,
					type: this.type,
					id: ''
				}
				deviceApi.getDetailsByNumber(data).then((res) => {
					if (res.data) {
						this.deviceInfo = res.data
					} else {
						this.$modal.showToast(`找不到该${this.type=='DEVICE'?'设备' : '模具'}`)
					}
				})
			},
			// APP维修工单接单、点击完成、验证
			orderClick() {
				const data = {
					number: this.data.number,
					status: this.data.status,
					id: this.data.id,
					requestNumber: ''
				}
				let tips = this.data.status == 'PENDING' ? '是否接单？' : this.data.status == 'PECEIVED'?'是否完成？' : this.data.status == 'COMPLETED'?'是否验证完结？':''
				this.$modal.confirm(tips).then(() => {
					this.$modal.loading('加载中')
					this.loading = true
					repairOrderApi.orderClick(data).then((res) => {
						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
					})
				})

			}
		},
		onLoad(option) {
			if (option.type) this.type = option.type;
			if (option.data) {
				this.data = JSON.parse(decodeURIComponent(option.data))

			}
			this.changeItem = this.list[this.current]

			this.getDetailsByNumber()
		}
	}
</script>

<style lang="scss" scoped>
	.info {
		background: white;
		margin-bottom: 20rpx;
	}

	.tab {
		border-bottom: 1px solid #e4e4e4;

	}

	.title {
		display: flex;
		align-items: center;
		padding: 20rpx 30rpx;
		border-bottom: 1px solid #e4e4e4;

		view {
			&:nth-child(1) {
				flex: 1;
				border-left: 10rpx solid #409eff;
				padding-left: 20rpx;
				font-weight: bold;
			}
		}
	}

	.dec {
		padding: 30rpx;

		.dec-item {
			padding-bottom: 30rpx;
			display: flex;

			view {
				&:nth-child(1) {
					width: 160rpx;
				}

				&:nth-child(2) {
					color: #888888;
					flex: 1;
				}
			}
		}
	}

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

	.popup {
		width: 600rpx;
		padding: 0rpx 60rpx 0rpx;

	}

	.popup-footer {
		display: flex;
		border-top: 1px solid #e4e4e4;

		view {
			line-height: 100rpx;
			flex: 1;
			text-align: center;

			&.sure {
				color: #409eff;
			}
		}
	}

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

	.btns {
		display: flex;

		padding: 20rpx;
		box-shadow: 0px -2rpx 20rpx rgba(0, 0, 0, 0.1);


		button {
			flex: 1;
			margin: 0px 10rpx;
		}

		.sure {
			background: #409eff;
			color: white;
			border-radius: 8rpx;

			&::after {
				border: 1px solid #409eff;
				border-radius: 0px;
			}
		}
	}
</style>