<template>
	<view class="container-box">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>
		<view class="container" v-if="tabCurrentIndex == 0">
			<view class="swiper-box">
				<scroll-view scroll-y class="cate-list" @scrolltolower="RescueData">
					<view class="nodata" v-if="RescueList.length == 0">
						<image src="/static/img/img011.png"></image>
						<!-- <view class="text">暂时没有数据哦！</view> -->
					</view>
					<view @click="navTo('/pages/record/details?id=' + item.id)" v-for="(item, index) in RescueList" class="content-box">
						<view class="flex">
							<view class="item">
								<view class="name" v-if="item.status == 0 || item.status == 2">求救者姓名：求救者{{ item.caller_id }}</view>
								<view class="name" v-if="item.status == 1 || item.status == 3">求救者姓名：{{ item.caller_nickname }}</view>
								<view class="phone" v-if="item.status == 1 || item.status == 3">手机号：{{ item.caller_mobile }}</view>
								<!-- <view class="addr">救援地址：{{item.address}}</view> -->
								<view class="time">求救时间：{{ item.createtime }}</view>
							</view>
							<view class="item-addr" v-if="item.status == 1">点击进入导航</view>
						</view>
						<view class="box-btn flex" v-if="item.status == 0">
							<view class="accept" @click.stop="access(item)">接受</view>
							<view class="refuse" @click.stop="refuse(item)">拒绝</view>
						</view>
						<view class="tpl" v-if="item.status != 0">{{ item.status_text }}</view>
						<view class="call-btn flex" v-if="item.status == 1">
							<view class="payPhone flex" @click.stop="Tocall(item)">
								<image src="/static/img/phone1.png" mode="widthFix"></image>
								<text>拨打电话</text>
							</view>
							<view class="payVideo flex" @click.stop="TocallVideo(item)" >
								<image src="/static/img/video1.png" mode="widthFix"></image>
								<text>视频通话</text>
							</view>
						</view>
					</view>
					<uni-load-more :status="loadingType"></uni-load-more>
				</scroll-view>
			</view>
		</view>
		<view class="container" v-if="tabCurrentIndex == 1">
			<view class="swiper-box">
				<scroll-view class="cate-list" scroll-y @scrolltolower="HpleData">
					<view class="nodata" v-if="HpleList.length == 0"><image src="/static/img/img011.png"></image></view>
					<view v-for="(item, index) in HpleList" class="content-box">
						<view class="item">
							<view class="name" v-if="item.status == 0 || item.status == 2">救援者姓名：救援者{{ item.helper }}</view>
							<view class="name" v-if="item.status == 1 || item.status == 3">救援者姓名：{{ item.helper_nickname }}</view>
							<view class="phone" v-if="item.status == 1 || item.status == 3">手机号：{{ item.helper_mobile }}</view>
							<view class="time">救援时间：{{ item.createtime }}</view>
						</view>
						<view class="tpl">{{ item.status_text }}</view>
						<view class="call-btn flex" v-if="item.status == 1">
							<view class="payPhone flex" @click.stop="Tocall1(item)">
								<image src="/static/img/phone1.png" mode="widthFix"></image>
								<text>拨打电话</text>
							</view>
							<view class="payVideo flex" @click.stop="TocallVideo(item)" >
								<image src="/static/img/video1.png" mode="widthFix"></image>
								<text>视频通话</text>
							</view>
						</view>
					</view>
					<uni-load-more :status="loadingType1"></uni-load-more>
				</scroll-view>
			</view>
		</view>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
import { toMySosList, accessSos, refuseSos, mySosList } from '@/api/record.js';
import { getTime } from '@/utils/rocessor.js';
import { getUserInfo } from '@/api/login.js';
// 引入实时通信
export default {
	components: {
		uniLoadMore,
		empty
	},
	computed: {
		...mapState(['userInfo'])
	},
	data() {
		return {
			tabCurrentIndex: 0,
			navList: [
				{
					id: 1,
					text: '救援记录'
				},
				{
					id: 2,
					text: '求救记录'
				}
			],
			HpleList: [],
			RescueList: [],
			limit: 4, //每次加载数据条数
			page: 1, //当前页数
			page1: 1,
			loadingType: 'more', //加载更多状态
			loadingType1: 'more', //加载更多状态
			loading: 0
		};
	},
	onLoad() {
		this.loadBaseData()
	},
	onShow() {
		this.page = 1;
		this.loadingType = 'more';
		this.RescueList = [];
		this.RescueData();
	},
	//下拉刷新
	// onPullDownRefresh() {
	// 	this.page1 = 1;
	// 	this.page = 1;
	// 	this.RescueData('refresh');
	// 	this.HpleData('refresh');
	// },
	methods: {
		...mapMutations(['setUserInfo','logout']),
		// 加载初始数据
		loadBaseData() {
			let obj = this;
			getUserInfo({}).then(({ data }) => {
				obj.setUserInfo(data.user);
			}).catch((e) => {
				obj.logout()
			});
		},
		//我的救援列表
		async RescueData(source) {
			let obj = this;
			if (source != 'refresh') {
				//没有更多数据直接跳出方法
				if (obj.loadingType === 'nomore') {
					return;
				} else {
					// 设置当前为数据载入中
					obj.loadingType = 'loading';
				}
			} else {
				//当重新加载数据时更新状态为可继续添加数据
				obj.loadingType = 'more';
			}
			toMySosList({
				status: -1,
				page: obj.page,
				limit: obj.limit
			})
				.then(({ data }) => {
					if (source === 'refresh') {
						obj.RescueList = [];
					}
					let arr = data.map(e => {
						e.createtime = getTime(e.createtime);
						return e;
					});
					obj.RescueList = obj.RescueList.concat(arr);
					//判断是否还有下一页，有是more  没有是nomore
					if (obj.limit == arr.length) {
						obj.page++;
						obj.loadingType = 'more';
					} else {
						obj.loadingType = 'nomore';
					}
					// 判断是否为刷新数据
					if (source == 'refresh') {
						// 判断是否为点击搜索按钮跳转加载
						if (obj.loading == 1) {
							uni.hideLoading();
						} else {
							uni.stopPullDownRefresh();
						}
					}
					console.log(obj.loadingType, 'zuihou');
				})
				.catch(e => {
					obj.loadingType = 'nomore';
					uni.stopPullDownRefresh();
					uni.hideLoading();
				});
		},
		//我的求救列表
		async HpleData(source) {
			//这里是将订单挂载到tab列表下
			let obj = this;
			if (source != 'refresh') {
				//没有更多数据直接跳出方法
				if (obj.loadingType1 === 'nomore') {
					return;
				} else {
					// 设置当前为数据载入中
					obj.loadingType1 = 'loading';
				}
			} else {
				//当重新加载数据时更新状态为可继续添加数据
				obj.loadingType1 = 'more';
			}
			mySosList({
				status: -1,
				page: obj.page1,
				limit: obj.limit
			})
				.then(({ data }) => {
					if (source === 'refresh') {
						obj.HpleList = [];
					}
					let arr = data.map(e => {
						e.createtime = getTime(e.createtime);
						return e;
					});
					obj.HpleList = obj.HpleList.concat(arr);
					//判断是否还有下一页，有是more  没有是nomore
					if (obj.limit == arr.length) {
						obj.page1++;
						obj.loadingType1 = 'more';
					} else {
						obj.loadingType1 = 'nomore';
					}
					// 判断是否为刷新数据
					if (source == 'refresh') {
						// 判断是否为点击搜索按钮跳转加载
						if (obj.loading == 1) {
							uni.hideLoading();
						} else {
							uni.stopPullDownRefresh();
						}
					}
				})
				.catch(e => {
					obj.loadingType1 = 'nomore';
					uni.stopPullDownRefresh();
					uni.hideLoading();
				});
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
			if (this.tabCurrentIndex == 0) {
				this.page = 1;
				this.loadingType = 'more';
				this.RescueData('refresh');
			}
			if (this.tabCurrentIndex == 1) {
				this.page1 = 1;
				this.loadingType1 = 'more';
				this.HpleData('refresh');
			}
		},
		//swiper 切换
		// changeTab(e) {
		// 	this.tabCurrentIndex = e.target.current;
		// 	this.loadData('tabChange');
		// },
		//拨打电话
		Tocall(item) {
			uni.makePhoneCall({
				phoneNumber: item.caller_mobile
			});
		},
		Tocall1(item) {
			uni.makePhoneCall({
				phoneNumber: item.helper_mobile
			});
		},
		//拨打电话
		TocallVideo(item) {
			let obj = this;
			console.log( obj.userInfo);
			uni.navigateTo({
				url: '/pagesA/record/call?userId=' + obj.userInfo.id + '&roomId=' + item.id
			});
		},
      
		access(item) {
			let obj = this;
			uni.showModal({
				title: '提示',
				content: '您确定要接受求救吗',
				success: function(res) {
					if (res.confirm) {
						accessSos({
							id: item.id
						})
							.then(data => {
								obj.$api.msg(data.msg);
								obj.page = 1;
								obj.RescueData('refresh');
								obj.HpleData('refresh');
							})
							.catch(e => {
								obj.$api.msg(e.message);
								obj.page = 1;
								obj.RescueData('refresh');
								obj.HpleData('refresh');
							});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		refuse(item) {
			let obj = this;
			uni.showModal({
				title: '提示',
				content: '您确定要拒绝求救吗?',
				success: function(res) {
					if (res.confirm) {
						refuseSos({
							id: item.id
						})
							.then(data => {
								console.log(data.msg, 77);
								obj.$api.msg(data.msg);
								obj.page = 1;
								obj.RescueData('refresh');
								obj.HpleData('refresh');
							})
							.catch(e => {
								obj.$api.msg(e.message);
								obj.page = 1;
								obj.RescueData('refresh');
								obj.HpleData('refresh');
							});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		navTo(url) {
			uni.navigateTo({
				url
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #f8f6f6;
	height: 100%;
	.container {
		width: 100%;
		height: 100%;
		.swiper-box {
			height: 85vh;
			padding-top: 25rpx;
			.cate-list {
				height: 100%;
			}
		}
	}
}
.navbar {
	display: flex;
	height: 40px;
	background: #fff;
	box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
	position: relative;
	z-index: 10;
	.nav-item {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		font-size: 15px;
		color: $font-color-dark;
		position: relative;
		&.current {
			color: $font-color;
			&:after {
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				width: 44px;
				height: 0;
				border-bottom: 2px solid #bc253a;
			}
		}
	}
}
.nodata {
	width: 430rpx;
	margin: 0rpx auto;
	padding-top: 80rpx;
	image {
		width: 430rpx;
		height: 337rpx;
	}
	.text {
		text-align: center;
		color: #999999;
		font-size: 26rpx;
	}
}
.swiper-box {
	height: calc(100% - 40px);
}
.list-scroll-content {
	height: 100%;
}
.navbar {
	overflow: hidden;
	white-space: nowrap;
	background: #fff;
	padding: 0rpx 25rpx;
	box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
	.nav-item {
		text-align: center;
		display: inline-block;
		height: 100%;
		font-size: 28rpx;
		color: $font-color-dark;
		position: relative;
		width: 25%;
		padding: 25rpx 0rpx;
		&.current {
			color: #ff6666;
			&:after {
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				width: 35px;
				height: 0;
				border-bottom: 2px solid #ff6666;
			}
		}
	}
}

.content-box {
	width: 90%;
	margin: 0rpx auto;
	background-color: #ffffff;
	border-radius: 15rpx;
	padding: 47rpx 30rpx;
	font-size: 24rpx;
	line-height: 60rpx;
	color: #666666;
	margin-bottom: 25rpx;
}
.item {
	.name {
		font-size: 28rpx;
		color: #333333;
		font-weight: 500;
	}
}
.item-addr{
		background-color: #8DD64C;
		border-radius: 50rpx;
		padding: 8rpx 25rpx;
		color: #FFFFFF;
	}
.call-btn {
	padding-top: 25rpx;
	font-size: 30rpx !important;
	color: #FFFFFF;
	.payPhone {
		background-color: #FF5050;
		border-radius: 50rpx;
		padding: 8rpx 25rpx;
		border: 1px solid #ff4f4f;
		border-radius: 99rpx;
		}
	.payPhone {
		border-right: 1px solid #ff4f4f;
		padding: 0rpx 30rpx;
		align-self: stretch ;
		image {
			width: 60rpx;
			height: 60rpx;
			margin-right: 15rpx;
		}
	}
	.payVideo {
		background-color: #3686FD;
		border-radius: 50rpx;
		padding: 8rpx 25rpx;
		image {
			width: 60rpx;
			height: 48rpx;
			margin-right: 15rpx;
		}
	}
	}
.box-btn {
	width: 80%;
	margin: 0rpx auto;
	margin-top: 30rpx;
	text-align: center;
	.accept {
		background: #ff4f4f;
		color: #ffffff;
		padding: 0rpx 30rpx;
		width: 35%;
	}
	.refuse {
		background: #f3f3f3;
		width: 35%;
		padding: 0rpx 30rpx;
	}
}
.tpl {
	font-size: 28rpx;
	font-weight: bold;
	color: rgba(255, 79, 79, 1);
}
</style>
