<template>
	<view>
		<view class="status_bar"></view>
		<view style="position: fixed;z-index: 100;top: 44px;">
			<uni-nav-bar @clickLeft="clickLeft" left-icon="back" left-text="返回" title="售后系统" rightIcon="search"
				@clickRight="clickRight"></uni-nav-bar>
			<view class="inputs" :style="{'width':width+'rpx'}">
				<input type="text" value="" placeholder="请输入设备ID或设备名称" v-model="imports" @input="searchinp"
					class="inputbox" :clearable="false" />
				<u-icon name="search" size="40" class="icon" @click="search"></u-icon>
			</view>
		</view>
		<image :src="$imageURL+'/bigdata_app/image/afterSale/dcd7147f4b15d00c5c90006738b8453.png'"
			class="expertimages"></image>
		<view class="loading" v-if="loadingtf">
			<image src="../../static/images/ajax-loader.gif" mode="" class="img"></image>
		</view>
		<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>设备类型：{{devtype[item.device_type]}}</p>
					<p>联系人：{{item.user?item.user:"无"}}</p>
					<p>联系电话：{{item.userphone?item.userphone:"无"}}</p>
					<p>位置：{{item.addr?item.addr:"无"}}</p>
					<!-- <p>故障上报时间：{{item.uptime|timeFormat()}}</p> -->
					<p>故障信息：{{item.errordesc}}</p>
				</view>
				<view class="aftersale_item_operate">
					<view class="aftersale_item_operate_tiem">
						{{item.uptime|timeFormat()}}
					</view>
					<view class="aftersale_item_operate_btn">
						<button :class="item.errorimg.length!=0?'imgs':'none'" :disabled="item.errorimg.length==0"
							@click="faultImg(item.errorimg)">图片</button>
						<button :class="item.errorvideo?'video':'none'" :disabled="!item.errorvideo"
							@click="faultVideo(item.errorvideo)">视频</button>
					</view>
					<!-- <button :class="item.errordesc?'fault':'none'" :disabled="!item.errordesc"
						@click="fault(item.errordesc)">故障原因</button> -->

				</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>
	import {
		Debounce,
		Throttle
	} from "../../util/anitthro.js"
	export default {
		data() {
			return {
				BASE_URL: 'https://web.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,
				imports: "", //搜索设备id
				width: 0, //顶部搜索框的宽度
				loadingtf: false, //loading
				devtype: {

				}
			}
		},
		methods: {
			async getAftersale() {
				this.loadingtf = true
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=after_sale.after_sale_manage.aftersale_info',
					data: {
						page: this.page,
						f_id: Number(this.imports)
					}
				})
				this.loadingtf = false
				if (res.counts == 0) {
					this.aftersaleTF = true
				} else {
					this.aftersaleTF = false
				}
				this.faultdata = this.faultdata.concat(res.data)
				for (var i = 0; i < this.faultdata.length; i++) {
					if (this.faultdata[i].errorimg) {
						if (!Array.isArray(this.faultdata[i].errorimg)) {
							this.faultdata[i].errorimg = JSON.parse(this.faultdata[i].errorimg)
						}
					} else {
						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) { //查看故障图片
				this.faultimg = []
				for (var i = 0; i < info.length; i++) {
					let obj = {}
					obj.image = info[i]
					this.faultimg.push(obj)
				}
				console.log(this.faultimg)
				this.title = "故障图片"
				this.show = true
				this.faultimg_tf = true
				this.faultinfo_tf = false
				this.faultvideo_tf = false
			},
			faultVideo(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
			},
			clickRight() { //搜索
				this.width = 600
			},
			addf() { //添加
				uni.navigateTo({
					url: "./addafter"
				})
			},
			clickLeft() {
				uni.switchTab({
					url: "../index/index"
				})
			},
			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
				})
			},
			search() { //搜索按钮搜索
				this.faultdata = []
				this.getAftersale()
			},
			searchinp() { //自动搜索
				Debounce(() => {
					this.faultdata = []
					this.getAftersale()
				}, 1000)()
			},
			async getdevtype() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=home.homes.user_device_type',
					data: {}
				})
				console.log(res)
				for (var i = 0; i < res.length; i++) {
					this.devtype[res[i].id] = res[i].type_name
				}
				console.log(this.devtype)
			}
		},
		onLoad() {
			this.getdevtype()
		},
		onShow() {
			this.page = 1
			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: 88px;
		z-index: 100;
	}

	.aftersale_search {
		position: fixed;
		z-index: 100;
		top: 104rpx;
		right: 20rpx;

		.sp_icon {
			font-size: 36rpx;
			margin-left: 16rpx;
		}
	}

	.aftersaleTF {
		width: 100%;
		position: relative;
		top: 340rpx;
		text-align: center;
		padding-top: 40rpx;
		font-size: 20px;
	}

	.loading {
		position: fixed;
		top: 440px;
		width: 95%;
		left: 2.5%;
		text-align: center;

		.img {
			width: 300rpx;
			height: 40rpx;
		}
	}

	.inputs {
		height: 54rpx;
		background-color: #E4E4E4;
		border-radius: 27rpx;
		position: absolute;
		right: 20rpx;
		top: 24rpx;
		transition: width 0.5s;
		overflow: hidden;
		padding-top: 8rpx;
		box-sizing: border-box;

		.inputbox {
			width: 85%;
			text-indent: 1rem;
			font-size: 26rpx;
		}

		.icon {
			position: absolute;
			top: 8rpx;
			right: 26rpx;
		}
	}

	.aftersale {
		width: 100%;
		position: relative;
		top: 340rpx;
		margin-bottom: 80rpx;

		.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;
					line-height: 48rpx;
					font-size: 24rpx;
					color: #7d7d7d;
				}
			}

			.aftersale_item_operate {
				width: 100%;
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;
				.aftersale_item_operate_btn{
					display: flex;
				}
				.aftersale_item_operate_tiem{
					// line-height: 60rpx;
					height: 60rpx;
					padding-top: 30rpx;
					color: #BEBEBE;
					font-size: 12px;
					box-sizing: border-box;
				}
				button {
					width: 130rpx;
					padding: 0;
					font-size: 24rpx;
					color: #FFFFFF;
					margin-left: 20rpx;
				}

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