<template>
	<view class="home">
		<view class="device-info">
			<i class="iconfont icon-shebei"></i>设备信息
		</view>
		<view class="info-area">
			<u--form
				labelPosition="left"
				:model="model1"
				ref="form1"
				:labelWidth="100"
				labelAlign="center"
			>
				<u-form-item
					label="设备名称"
					prop="deviceName"
					borderBottom
					ref="item1"
				>
					{{model1.deviceName}}
				</u-form-item>
				<u-form-item
					label="设备编码"
					prop="deviceNumber"
					borderBottom
					ref="item2"
				>
					{{model1.deviceNumber}}
				</u-form-item>
			</u--form>
		</view>
		<view class="type-head">
			<i class="iconfont icon-dingwei"></i>请填写故障现象
		</view>
		<view class="type-area">
			<u--textarea 
			border="none" 
			v-model="fault" 
			placeholder="请输入故障现象" 
			autoHeight
			maxlength="-1"></u--textarea>
		</view>
		<view class="btn-area">
			<u-button color="#67C23A" size="small" @click="toReportFault">上报</u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { searchAsset,reportFault } from "@/api/asset.js"
	import cache from '@/utils/storage.js'
	export default {
		data() {
			return {
				model1: {
					deviceName: '',
					deviceNumber: '',
				},
				fault:"",
				id:""
			};
		},
		methods:{
			initAssets(data,str){
				uni.showLoading({
					title:str,
					mask:true,
					icon:'none'
				})
				searchAsset({barCode: data}).then(res=>{
					if(res.barCode===null){
						this.model1.deviceName = ""
						this.model1.deviceNumber = this.id
						this.$refs.uToast.show({
							type: 'error',
							icon: false,
							message: "该设备不存在，即将返回上一页",
							duration:1500
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else if(res.status==="0" || res.status==="2"){
						this.$refs.uToast.show({
							type: 'error',
							icon: false,
							message: "该设备已经上报故障，即将返回上一页",
							duration:1500
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						this.model1.deviceName = res.name
						this.model1.deviceNumber = res.barCode
					}
					uni.hideLoading()
				})
			},
			toReportFault(){
				uni.showLoading({
					title: "正在上报中",
					mask: true,
					icon:'none'
				});
				let data = {
					"assetBarCode": this.model1.deviceNumber,
					"faultPhenomenon": this.fault,
					"tel": cache.session.getJSON('phone')
				}
				reportFault(data).then(res=>{
					uni.hideLoading()
					if(res.faultPhenomenon!==""){
						uni.showModal({
							title: '系统提示',
							content: '故障上报成功，是否返回上一页',
							showCancel:false,
							success: function (resp) {
								if (resp.confirm) {
									uni.navigateBack({
										delta:1
									})
								} else if (resp.cancel) {
									return;
								}
							}
						});
					}
					
				})
			}
		},
		onLoad(option) {
			this.id = option.id
			this.initAssets(option.id,'正在加载中')
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f5f6fa;
}
.home{
	// padding: 20rpx 0;
}
.device-info,.type-head{
	display: flex;
	align-items: center;
	padding:20rpx;
	.iconfont{
		font-size: 40rpx;
		margin-right: 10rpx;
	}
}
.info-area,.type-area{
	background-color: #fff;
}
.type-area{
	padding: 0 20rpx;
}
.btn-area{
	// padding: 20rpx 20rpx 20rpx 500rpx;
	margin-top: 120rpx;
	padding: 0 250rpx;
}
</style>
