<template>
	<view class="default_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='PERILL团队发展动态评估报告' bgColor="transparent"></cus-header>
		<image class="top_bg" :src="imgBase+'report_bg.png'" mode="widthFix"></image>
		<view class="tab adfac">
			<view class="tab-pre" :class="{'active':tindex===0}" @click="changeTab(0)">我收到的</view>
			<view class="tab-pre" :class="{'active':tindex===1}" @click="changeTab(1)">我生成的</view>
			<view class="tab-pre" :class="{'active':tindex===2}" @click="changeTab(2)">我发送的</view>
		</view>
		<view class="query adfacjb">
			<u-icon name="search" size="42rpx" color="#B3BFC8"></u-icon>
			<view class="query-inp">
				<u-input v-model="queryParams.teamName" border="none" fontSize="28rpx" color="#002846" confirmType="search"
				 placeholder="请输入团队名称查询" @confirm="getList"></u-input>
			</view>
			<view class="query-right" @click="getList">搜索</view>
		</view>
		<view class="box" v-if="list.length">
			<template v-if="tindex===0">
				<receive-list :list="list" @scrolltolower="scrolltolower"></receive-list>
			</template>
			<template v-else-if="tindex===1">
				<generate-list :list="list" @scrolltolower="scrolltolower" @reSendReport="reSendReport"></generate-list>
			</template>
			<template v-else-if="tindex===2">
				<send-list :list="list" @scrolltolower="scrolltolower"></send-list>
			</template>
		</view>
		<view style="flex: 1;" v-else>
			<page-empty text='暂无报告'></page-empty>
		</view>
	</view>
</template>

<script>
	import ReceiveList from './components/report/receiveList.vue'
	import GenerateList from './components/report/generateList.vue'
	import SendList from './components/report/sendList.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	export default {
		components:{
			ReceiveList,
			GenerateList,
			SendList,
			PageEmpty
		},
		data(){
			return {
				tindex:0,
				queryParams:{
					page:1,
					limit:10,
					teamName:''
				},
				list:[],
				isOver:false,
				categoryData:[]
			}
		},
		onShow() {
			this.getList();
		},
		methods:{
			changeTab(index){
				this.tindex = index;
				this.initList();
				this.getList();
			},
			initList(){
				this.queryParams.page = 1;
				this.isOver = false;
				this.list = [];
			},
			getList(){
				if(this.tindex===0) this.getReceiveList()
				else if(this.tindex===1) this.getGenerateList()
				else if(this.tindex===2) this.getSendList()
			},
			getReceiveList(){
				this.$api.get('/core/report/receivedReportList',this.queryParams).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.list = [...this.list,...res.data.list];
					this.queryParams.page++;
					if(res.data.list.length===0) this.isOver = true;
					if(this.list.length===0) this.$showToast('暂无数据')
				})
			},
			getGenerateList(){
				this.$api.get('/core/report/generatedReportList',this.queryParams).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.list = [...this.list,...res.data.list];
					this.queryParams.page++;
					if(res.data.list.length===0) this.isOver = true;
					if(this.list.length===0) this.$showToast('暂无数据')
				})
			},
			async getUserCategoryData(){
				return new Promise((resolve,reject)=>{
					this.$api.get('/getListByType/UserCategory').then(({data:res})=>{
						if(res.code!==0) return this.$showToast(res.msg)
						this.categoryData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
						resolve()
					})
				})
			},
			async getSendList(){
				await this.getUserCategoryData()
				let query = JSON.parse(JSON.stringify(this.queryParams));
				query.coachId = uni.getStorageSync('userInfo')&&JSON.parse(uni.getStorageSync('userInfo')).id||'';
				this.$api.get('/core/report/receivedReportList',query).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.list = [...this.list,...res.data.list];
					this.list.forEach(l=>{
						l.categoryName = this.categoryData.find(c=>c.id===l.category).name||'';
					})
					this.queryParams.page++;
					if(res.data.list.length===0) this.isOver = true;
					if(this.list.length===0) this.$showToast('暂无数据')
				})
			},
			reSendReport(){
				this.initList();
				this.getList()
				this.$showToast('重新生成成功')
			},
			scrolltolower(){
				if(this.isOver) return
				this.getList()
			}
		}
	}
</script>

<style scoped lang="scss">
	.default_page{
		padding: 0 24rpx 40rpx;
		background: #F7F7F7;
		box-sizing: border-box;
		
		.top_bg{
			width: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}
	
		.query{
			width: calc(100% - 12rpx);
			margin: 40rpx 6rpx 0;
			height: 72rpx;
			background: #FFFFFF;
			border-radius: 36rpx;
			padding: 0 36rpx;
			box-sizing: border-box;
			position: relative;
			&-inp{
				flex: 1;
				padding: 0 20rpx;
				box-sizing: border-box;
			}
			&-right{
				font-size: 28rpx;
				color: #33A7A7;
				padding-left: 20rpx;
				position: relative;
				&::before{
					content: '';
					width: 1rpx;
					height: 30rpx;
					background: rgba(51, 167, 167, .5);
					position: absolute;
					left: 0;
					top: 50%;
					margin-top: -15rpx;
				}
			}
		}
	
		.box{
			flex: 1;
			overflow-y: auto;
		}
	
		.tab{
			margin-top: 20rpx;
			position: relative;
			&-pre{
				width: 50%;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #002846;
				line-height: 42rpx;
				text-align: center;
				&.active{
					font-weight: bold;
					font-size: 32rpx;
					line-height: 45rpx;
					position: relative;
					&::after{
						content: '';
						width: 48rpx;
						height: 6rpx;
						background: #335368;
						border-radius: 9rpx;
						position: absolute;
						left: 50%;
						margin-left: -24rpx;
						bottom: -22rpx;
					}
				}
			}
		}
	}
</style>