<template>
	<view>
		<view class="status_bar"></view>
		<view style="position: fixed;z-index: 100;top: 40px;">
			<uni-nav-bar @clickLeft="clickLeft" left-icon="back" left-text="返回">
				<view class="bases_search">
					<view class="bases_search_text" @click="search">
						<u-icon name="search" class="search" @click="search"></u-icon>
						<input type="text" v-model="data.search" placeholder="设备ID搜索" disabled/>
					</view>
				</view>
			</uni-nav-bar>
		</view>
		<image :src="$imageURL+'/bigdata_app/image/afterSale/dcd7147f4b15d00c5c90006738b8453.png'" class="expertimages"></image>
		<view class="aftersaleTF" v-if="aftersaleTF">
			暂无数据
		</view>
		<view class="aftersale" v-else>
			<view class="aftersale_item" v-for="(item,index) in faultdata" :key="index">
				<view class="aftersale_item_title">
					<u-icon name="calendar" color="#71D8AF" size="34"></u-icon>
					<span>设备 ID：{{item.device_id}}</span>
				</view>
				<view class="aftersale_item_con">
					<p>联系人：{{item.user}}</p>
					<p>联系电话：{{item.userphone}}</p>
					<p>位置：{{item.addr}}</p>
					<p>故障上报时间：{{item.uptime|timeFormat()}}</p>
				</view>
				<view class="aftersale_item_operate">
					<button :class="item.errordesc?'fault':'none'" :disabled="!item.errordesc" @click="fault(item.errordesc)">故障原因</button>
					<button :class="item.errorimg?'imgs':'none'" :disabled="!item.errorimg" @click="faultImg(item.errorimg)">图片</button>
					<button :class="item.errorvideo?'video':'none'" :disabled="!item.errorvideo" @click="faultVideo(item.errorvideo)">视频</button>
				</view>
				<view class="aftersale_item_icon">
					<image :src="$imageURL+'/bigdata_app/image/afterSale/'+item.device_type+'.png'" mode=""></image>
				</view>
			</view>
		</view>
		<view class="addindent" @click="addf">
			新 增 售 后 单
		</view>
		<u-modal v-model="show" :mask-close-able="maskcloseable" :show-confirm-button="showconfirmbutton" :title="title" class="model">
			<view class="model_box">
				<p v-if="faultinfo_tf">{{faultinfo}}</p>
				<u-swiper :list="faultimg" v-if="faultimg_tf" class="uswiper" height="400" style="width: 100%;" @click="examine(faultimg)"></u-swiper>
				<video :src="BASE_URL+faultvideo" controls v-if="faultvideo_tf" style="width:90%;"></video>
			</view>
		</u-modal>
		<view class="top" v-if="isTop" @click="top">
			<image :src="$imageURL+'/bigdata_app'+'/image/6209a98f0cb3b5086f2ca36152c9269.png'" mode=""></image>
		</view>
	</view>
</template>
<script>
	export default {
		data() {  
			return {
				data:{
					search:''
				},
				BASE_URL: 'http://dev.hnyfwlw.com',
				faultdata: [],
				title:"",//弹框标题
				show: false,//弹框先显示
				maskcloseable: true,
				showconfirmbutton: false,
				faultinfo: '',//故障信息
				faultinfo_tf:false,//故障信息显示
				faultvideo: '',//故障视频
				faultvideo_tf:false,//故障视频显示
				faultimg: [],//故障图片
				faultimg_tf: false,//故障图片显示
				page:1,
				aftersaleTF:true,
				isTop:false
			}
		},
		methods: {
			async getAftersale() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=after_sale.after_sale_manage.aftersale_info',
					data:{
						page:this.page
					}
				})
				console.log(res)
				if(res.counts == 0){
					this.aftersaleTF = true
				}else{
					this.aftersaleTF = false
				}
				this.faultdata =this.faultdata.concat(res.data)
				console.log(this.faultdata)
				// for (var i = 0; i < this.faultdata.length; i++) {
				// 	if (this.faultdata[i].errorimg) {
				// 		this.faultdata[i].errorimg = JSON.parse(this.faultdata[i].errorimg)
				// 	}else{
				// 		this.faultdata[i].errorimg = []
				// 	}
				// 	console.log(this.faultdata[i].errorimg)
				// }
				// console.log(res)
			}, 
			fault(info) { //查看故障信息
				this.title="故障信息"
				this.show = true
				this.faultinfo = info
				this.faultimg_tf = false
				this.faultinfo_tf=true
				this.faultvideo_tf=false
			},
			faultImg(info) { //查看故障图片
				console.log(JSON.parse(info))
				this.faultimg=JSON.parse(info)
				console.log(this.faultimg)
				this.title="故障图片"
				this.show = true
				this.faultimg_tf = true
				this.faultinfo_tf=false
				this.faultvideo_tf=false
			},
			faultVideo(info) { //查看故障视频
				console.log(info)
				this.title="故障视频" 
				if(info.indexOf("[")!=-1){
					this.faultvideo=JSON.parse(info)[0]
				}else{
					this.faultvideo=info
				}
				console.log(this.faultvideo)
				this.show = true
				this.faultimg_tf = false
				this.faultinfo_tf=false
				this.faultvideo_tf=true
			},
			search() { //搜索
				uni.navigateTo({
					url:"./search"
				})
			},
			addf() { //添加
				uni.navigateTo({
					url:"./addafter"
				})
			},
			clickLeft(){
				// uni.switchTab({
				// 	url:"../index/index"
				// })
				// 返回上一页面
				uni.navigateBack({
					delta: 1
				})
			},
			examine(url) {
				var imgarr =[]
				for(var i=0;i<url.length;i++){
					imgarr.push(url[i].image)
				}
				console.log(imgarr)
				uni.previewImage({
					urls: imgarr
				});
			},
			top() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 500
				})
			}
		},
		onLoad() { 
			// this.getAftersale()
		},
		onShow(){
			this.faultdata=[]
			this.getAftersale()
		},
		onReachBottom() {
			this.page++
			this.getAftersale()
		},
		onPageScroll(e) { //nvue暂不支持滚动监听，可用bindingx代替
			if (e.scrollTop > 200) { //距离大于200时显示
				this.isTop = true
			} else { //距离小于200时隐藏
				this.isTop = false
			}
		},
	}
</script>

<style lang="scss">
	.expertimages {
		width: 100%;
		height: 154rpx;
		position: fixed;
		top: 84px;
		z-index: 100;
	}
	.aftersaleTF{
		width: 100%;
		position: relative;
		top: 320rpx;
		text-align: center;
		padding-top: 40rpx;
		font-size: 20px;
	}
	.bases_search {
		width: 60%;
		background-color: #FFFFFF;
		position: absolute;
		top: 10rpx;
		left: 50%;
		margin-left: -33%;
		.bases_search_text {
			width: 90%;
			margin: 0 auto;
			background-color: #F8F8F8;
			height: 60rpx;
			border-radius: 30rpx;
			display: flex;
			line-height: 60rpx;
	
			.search {
				padding: 0 20rpx;
				font-size: 34rpx;
			}
	
			input {
				width: 80%;
				margin-top: 10rpx;
				font-size: 28rpx;
			}
		}
	}
	.aftersale {
		width: 100%;
		position: relative;
		top: 330rpx;
		margin-bottom: 100rpx;
		.aftersale_item {
			width: 90%;
			margin: 0 auto 30rpx;
			box-shadow: 0 0 10rpx #bcb9ca;
			padding: 30rpx 20rpx 20rpx;
			position: relative;
			box-sizing: border-box;
			.aftersale_item_title {
				width: 100%;

				span {
					margin-left: 16rpx;
					font-weight: 700;
				}
			}

			.aftersale_item_con {
				width: 92%;
				margin-left: 8%;
				margin-top: 20rpx;

				p {
					height: 48rpx;
					font-size: 24rpx;
					color: #BEBEBE;
				}
			}

			.aftersale_item_operate {
				width: 70%;
				display: flex;
				margin: 18rpx 0 0 30%;

				button {
					width: 130rpx;
					padding: 0;
					font-size: 24rpx;
					color: #FFFFFF;
				}

				.fault {
					background-color: #F78E01;
				}

				.imgs {
					background-color: #71CD9A;
				}

				.video {
					background-color: #53C6E6;
				}

				.none {
					background-color: #F3F3F3;
					color: #B4B4B4;
				}
			}

			.aftersale_item_icon {
				position: absolute;
				top: 0;
				right: 36rpx;

				image {
					width: 68rpx;
					height: 64rpx;
				}
			}
		}
	}

	.model {
		width: 90%;
		.model_box {
			width: 90%;
			margin: 40rpx auto 48rpx;
			display: flex;
			justify-content: center;
		}
	}
	.top {
		position: fixed;
		right: 30px;
		bottom: 100px;
		z-index: 100;
		image{
			width: 100rpx;
			height: 100rpx;
		}
	}
	.addindent{
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		background-color: #71cd9a;
		color: #ffffff;
		font-size: 16px;
	}
</style>
