<template>
	<!-- 详情 -->
	<view class="detail-container">
		<view class="info">
			<view class="title">
				<view>维修工单</view>
				<view @click="transfer" v-if="data.status == 'PECEIVED' && (!serviceList || serviceList && serviceList.length == 0)">转办</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="list" v-if="current == 2">
					<view class="item " v-for="(item,index) in serviceList" :key="index"
						@click="addSubForm('updata',item)">
						<view class="item-box">
							<view class="spare-title">
								<view class="title-txt">
									{{item.describes}}
								</view>
								<u-icon name="trash" color="#aaaaaa" size="40"
									@click.native.stop="delService(item)"></u-icon>
							</view>
							<view class="dec2">
								<view>故障真因：</view>
								<view>{{item.describes1}}</view>
							</view>
							<view class="dec2">
								<view>解决措施：</view>
								<view>{{item.workOut}}</view>
							</view>
							<view class="dec2">
								<view>维修人员：</view>
								<view>{{item.maintenancesName}}</view>
							</view>
							<view class="dec2">
								备件：
							</view>
							<view class="items" v-for="(cur,key) in item.items" :key="key">
								<view class="items-name">
									{{cur.name}}
								</view>
								<view class="items-dec">
									备件编号：{{cur.number}}
								</view>
								<view class="items-dec">
									数量：{{cur.qty}}
								</view>
							</view>
						</view>
						<!-- <u-icon name="minus-circle" color="#aaaaaa" size="60" ></u-icon> -->
					</view>
					<view class="add-btn"  v-if="data.status == 'PENDING' || data.status == 'PECEIVED'" >
						<u-button type="primary" @click="addSubForm('creat')"><u-icon name="plus-circle" color="#ffffff"
								size="36"></u-icon>添加维修内容</u-button>
					</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btns">
				<u-button type="primary" v-if="data.status == 'PENDING'" @click="orderClick(1)" :loading='loading'
					:disabled='loading'>接单</u-button>
				<u-button type="primary" v-if="data.status == 'PECEIVED'" @click="orderClickSuccess()" :loading='loading'
					:disabled='loading'>完成</u-button>
				<u-button type="primary" v-if="data.status == 'COMPLETED'" @click="orderClick(3)" :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="isShowSuccess" mode="center" border-radius="14">
			<view class="popup-title">完成类型</view>
			<view class="popup">
				<u-radio-group v-model="result"
					@change="radioGroupChange">
					<u-radio v-for="(item, index) in orderCompleteResult" :key="index" :name="item.value">
						{{item.label}}
					</u-radio>
				</u-radio-group>
			</view>
			<view class="popup-footer">
				<view @click="isShowSuccess = false">取消</view>
				<view class="sure" @click="sure">确认</view>
			</view>
		</u-popup>
		<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
	</view>
</template>

<script>
	import SparePartsCard from '@/components/sparePartsCard/index.vue'
	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"
	export default {
		data() {
			return {
				list: [{
					name: '设备信息'
				}, {
					name: '报修信息'
				}, {
					name: '维修内容'
				}],
				current: 0,
				changeItem: '',
				data: '', //工单信息
				loading: false,
				type: "",
				deviceInfo: '', //设备信息
				serviceList: [], //维修内同列表
				// 完成类型字段
				isShowSuccess:false,
				orderCompleteResult:[],//订单完成类型
				result:'TEMPORARILY'
			}
		},
		methods: {
			change(index) {
				console.log("index", index);
				this.current = index
				this.changeItem = this.list[this.current]
			},
			// 添加表格表单
			addSubForm(clickType, item) {
				if(this.data.status == 'PENDING'){
					this.$modal.showToast('请先接单')
					return
				}
				if (clickType == 'updata') {
					this.$tab.navigateTo(
						`/pages/repairOrder/addServiceRecord?type=${this.type}&factoryAreaNumber=${this.data.factoryAreaNumber}&number=${this.data.number}&data=${encodeURIComponent(JSON.stringify(item))}`
					)
				} else {
					this.$tab.navigateTo(
						`/pages/repairOrder/addServiceRecord?type=${this.type}&factoryAreaNumber=${this.data.factoryAreaNumber}&number=${this.data.number}`
					)
				}
			},
			// 添加备件
			addSpare() {

			},
			// 取消添加备件
			cancle() {
				this.isPopupShow = false
			},
			// 根据设备/模具号查询信息
			getDetailsByNumber() {
				const data = {
					number: this.data.deviceNumber,
					id: ''
				}
				if (this.type == 'DEVICE') {
					deviceApi.getDeviceDetailsByNumber(data).then((res) => {
						if (res.data) {
							this.deviceInfo = res.data
						} else {
							this.$modal.showToast(`找不到该${this.type=='DEVICE'?'设备' : '模具'}`)
						}
					})
				} else if (this.type == 'MOLD') {
					moldApi.getMoldDetailsByNumber(data).then((res) => {
						if (res.data) {
							this.deviceInfo = res.data
						} else {
							this.$modal.showToast(`找不到该${this.type=='DEVICE'?'设备' : '模具'}`)
						}
					})
				}
			},
			// 完成按钮
			orderClickSuccess(){
				this.isShowSuccess = true
			},
			radioGroupChange(e){
				console.log(e)
				this.result = e
			},
			sure(){
				this.isShowSuccess = false
				this.orderClick(2)
			},
			// APP维修工单接单、点击完成、验证
			orderClick(type) {
				const data = {
					number: this.data.number,
					status: this.data.status,
					id: this.data.id,
					result:type == 2? this.result :'',
					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
					})
				})
			},
			// 获取维修内容列表
			getRepairOrderDetaiList() {
				repairOrderApi.repairOrderDetailList({
					number: this.data.number
				}).then((res) => {
					if (res.data) {
						this.serviceList = res.data
					}
				})
			},
			// 删除维修内容
			delService(item) {
				console.log(222)
				this.$modal.confirm('确定删除维修内容吗？').then(() => {
					this.$modal.loading('加载中')
					repairOrderApi.repairOrderDetailDelete(item.id).then((res) => {
						this.$modal.closeLoading()
						this.getRepairOrderDetaiList()
					})
				})

			},
		},
		async 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.orderCompleteResult = await dictApi.getDict('order_complete_result')


		},
		onShow() {
			this.getDetailsByNumber()
			this.getRepairOrderDetaiList()
		}
	}
</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;
					width: 0px;
					word-wrap: break-word;
				}
			}
		}
	}

	.dec2 {
		padding: 10rpx 30rpx;
		display: flex;

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

			&:nth-child(2) {
				color: #888888;
				flex: 1;
				width: 0px;
				word-wrap: break-word;
			}
		}


	}

	.items {
		margin: 20rpx 30rpx;
		border-radius: 12rpx;
		background: #F5F5F5;
		padding-bottom: 20rpx;

		.items-name {
			padding: 20rpx;
			border-bottom: 1px solid #dedede;
		}

		.items-dec {
			padding: 0px 20rpx;
			margin-top: 20rpx;
		}
	}

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

	.list {
		padding: 20rpx;

		.item {
			display: flex;
			margin-bottom: 20rpx;

			.item-box {
				border-radius: 12rpx;
				border: 1px solid #dedede;
				border-radius: 12rpx;

				flex: 1;
				width: 0rpx;
			}

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

				.title-txt {
					color: #409eff;
					font-size: 30rpx;
					font-weight: bold;
					flex: 1;
				}
			}

			.dec {
				color: #9c9c9c;
				padding: 0rpx 30rpx 20rpx;
			}

		}
	}

	.add-btn {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
	::v-deep .u-radio-group {
		display: grid !important;
		padding-bottom: 20rpx;
	}
</style>