<template>
	<view class="qbox adffc">
		<view class="list" v-if="list.length">
			<up-list @scrolltolower="scrolltolower" style="height: 100%;">
				<up-list-item class="list-item" v-for="(item, index) in list" :key="index">
					<view @click.prevent="showDialog(item)">
						<view class="status adf" :class="{'dwc':item.status===0,'ywc':item.status===1}">
							<image :src="imgBase+'questionnaire_icon_dwc.png'" v-if="item.status===0"></image>
							<image :src="imgBase+'questionnaire_icon_ywc.png'" v-else-if="item.status===1"></image>
							<text>{{item.status===0?'待完成':item.status===1?'':''}}</text>
						</view>
						<image class="expand" :src="imgBase+'questionnaire_icon_down.png'"></image>
						<view class="title">{{item.title||''}}</view>
						<view class="name">团队名称：{{item.teamName||''}}</view>
						<view class="bottom adfacjb">
							<view class="bottom-left">截止时间：{{item.endTime}}</view>
							<view class="bottom-right" v-if="item.status===0" @click.stop="showNotice(item)">立即作答</view>
							<!-- <view class="bottom-right" v-else-if="item.status===1" @click.stop="reviewReport(item)">查看报告</view> -->
						</view>
					</view>
				</up-list-item>
			</up-list>
		</view>
		<view class="empty" v-else>
			<page-empty></page-empty>
		</view>
		<view class="dialog adffc" v-if="show">
			<view class="dialog-background" @click="show=false"></view>
			<view class="dbox">
				<view class="dbox-top adfacjb">
					<view class="dbox-top-title">{{dto.title||''}}</view>
					<image class="dbox-top-expand" :src="imgBase+'questionnaire_icon_down.png'" @click="show=false"></image>
				</view>
				<view class="dbox-name">团队名称：{{ dto.teamName || '' }}</view>
				<!-- <view class="dbox-status adfac" :class="{'dwc':dto.status===0,'ywc':dto.status===1}">
					<image :src="imgBase+'questionnaire_icon_dwc.png'" v-if="dto.status===0"></image>
					<image :src="imgBase+'questionnaire_icon_ywc.png'" v-else-if="dto.status===1"></image>
					<text>{{dto.status===0?'待完成':dto.status===1?'已完成':''}}</text>
				</view> -->
				<view class="dbox-menu adf">
					<view class="dbox-menu-pre adffcac" v-for="(item,index) in menuList" :key="index" @click="handleMenuClick(index)">
						<template v-if="item.text === '分享问卷'">
							<button class="share-btn" open-type="share">
								<image :src="item.img"></image>
								<text>{{ item.text }}</text>
							</button>
						</template>
						<template v-else>
							<image :src="item.img"></image>
							<text>{{ item.text }}</text>
						</template>
					</view>
				</view>
			</view>
		</view>
		<cus-notice :show="noticeShow" @close="noticeShow=false" @handleKnow="handleKnow"></cus-notice>
		<cus-team-info :show="teamInfoShow" :teamInfo="teamInfo" @close="teamInfoShow=false"></cus-team-info>
		<cus-team-user :canEdit="false" :show="teamUserShow" :list="teamUserList" @close="teamUserShow=false"></cus-team-user>
	</view>
</template>

<script>
	import CusNotice from '@/components/CusNotice/index2.vue'
	import CusTeamUser from '@/components/CusTeamUser/index.vue'
	import CusTeamInfo from '@/components/CusTeamInfo/index.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	export default {
		components:{ CusNotice, CusTeamUser, CusTeamInfo, PageEmpty },
		props:{
			list:{
				typeof:Array,
				default:[]
			}
		},
		data(){
			return {
				show:false,
				noticeShow:false,
				teamInfoShow:false,
				teamUserShow:false,
				dto:null,
				menuList:[
					{
						img:this.$imgBase+'questionnaire_users.png',
						text:'团队成员'
					},
					{
						img:this.$imgBase+'questionnaire_share.png',
						text:'分享问卷'
					},
					{
						img:this.$imgBase+'questionnaire_info.png',
						text:'团队信息'
					},
					{
						img:this.$imgBase+'questionnaire_report.png',
						text:'查看报告'
					}
				],
				categoryData:[],
				teamScaleData:[],
				teamLevelData:[],
				teamUserList:[],
				teamInfo:null
			}
		},
		methods:{
			scrolltolower(){
				this.$emit('scrolltolower')
			},
			showDialog(item){
				this.dto = item;
				this.show = true;
				this.$emit('showDialogFn',item);
			},
			showNotice(item){
				this.dto = item;
				this.noticeShow = true;
			},
			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 handleMenuClick(type){
				if(type===0){
					await this.getUserCategoryData()
					this.$api.get(`/core/member/listByQueTeamId/${this.dto.teamQuestionnaireId}`).then(({data:res})=>{
						if(res.code!==0) return this.$showToast(res.msg)
						this.teamUserList = res.data;
						this.originTeamUserList = res.data;
						this.teamUserList.forEach(l=>{
							l.emailTM = this.$reg.desensitizeContent(l.email);
							l.mobileTM = this.$reg.desensitizeContent(l.mobile,2);
							l.categoryName = this.categoryData.find(c=>c.id==l.category)?.name;
						})
						this.teamUserShow = true
					})
				} else if(type===2){
					this.showTeamInfo()
				} else if(type===3){
					uni.redirectTo({
						url:'/pagesHome/report'
					})
				}
			},
			reviewReport(item){
				uni.redirectTo({
					url:'/pagesHome/report'
				})
			},
			handleKnow(){
				uni.navigateTo({
					url:'/pagesPublish/questionnaireFill?teamQuestionnaireId='+this.dto.teamQuestionnaireId+'&teamId='+this.dto.teamId+'&type='+this.dto.type+'&turnType=questionnaire'+'&title='+this.dto.title
				})
			},
			async getTeamScaleData(){
				return new Promise((resolve,reject)=>{
					this.$api.get('/getListByType/team_scale').then(({data:res})=>{
						if(res.code!==0) return this.$showToast(res.msg)
						this.teamScaleData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
						resolve()
					})
				})	
			},
			
			async getTeamHierarchyData(){
				return new Promise((resolve,reject)=>{
					this.$api.get('/getListByType/team_hierarchy').then(({data:res})=>{
						if(res.code!==0) return this.$showToast(res.msg)
						this.teamLevelData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
						resolve()
					})
				})	
			},
			async showTeamInfo(){
				await this.getTeamScaleData()
				await this.getTeamHierarchyData()
				this.$api.get(`/core/user/team/${this.dto.teamId}`).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.teamInfo = res.data;
					this.teamInfo.functionsName = res.data.functions.map(f=>f.functionName).join('、');
					this.teamInfo.organizationsName = res.data.organizations.map(f=>f.orgName).join('、');
					this.teamInfo.address = res.data.provinceName+res.data.cityName;
					this.teamInfo.scaleName = this.teamScaleData.find(d=>d.id==res.data.scale).name;
					this.teamInfo.hierarchyName = this.teamLevelData.find(d=>d.id==res.data.hierarchy).name;
					this.teamInfoShow = true;
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.share-btn {
	background-color: transparent;
	border: none;
	padding: 0;
	margin: 0;
	line-height: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	&::after {
		border: none;
	}
}

	.qbox{
		width: 100%;
		height: 100%;
		
		.list{
			flex: 1;
			margin-top: 20rpx;
			&-item{
				width: 100%;	
				background: #FFFFFF;
				border-radius: 24rpx;
				margin-top: 20rpx;
				padding: 99rpx 24rpx 19rpx;
				box-sizing: border-box;
				position: relative;
				display: block;
				&:first-child{
					margin-top: 0;
				}
				
				.status{
					width: 164rpx;
					height: 80rpx;
					padding: 14rpx 0 0 15rpx;
					box-sizing: border-box;
					position: absolute;
					left: 0;
					top: 0;
					image{
						width: 26rpx;
						height: 26rpx;
					}
					text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 30rpx;
						margin-left: 12rpx;
					}
					&.dwc{
						background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/questionnaire_dwc.png') no-repeat;
						background-size: 100% 100%;
						text{
							color: #193D59;
						}
					}
					&.ywc{
						background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/questionnaire_ywc.png') no-repeat;
						background-size: 100% 100%;
					}
				}
			
				.expand{
					width: 32rpx;
					height: 32rpx;
					position: absolute;
					top: 24rpx;
					right: 32rpx;
				}
				
				.title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #002846;
					line-height: 32rpx;
				}
				
				.name{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #667E90;
					line-height: 24rpx;
					margin-top: 32rpx;
				}
				
				.bottom{
					margin-top: 30rpx;
					border-top: 1rpx solid #EFEFEF;
					padding-top: 20rpx;
					&-left{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #667E90;
						line-height: 24rpx;
					}
					&-right{
						border-radius: 32rpx;
						background: linear-gradient( 90deg, #33A7A7 0%, #4DB2B2 100%);
						padding: 19rpx 22rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 26rpx;
						color: #FFFFFF;
						line-height: 26rpx;
						letter-spacing: 2rpx;
					}
				}
			}
		}
		
		.empty{
			flex: 1;
		}
	
		.dialog{
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			// background: rgba(0, 0, 0, .4);
			z-index: 1001;
			justify-content: flex-end;
			
			.dialog-background {
			    position: absolute; /* 相对于父级 dialog 定位 */
			    top: 0;
			    left: 0;
			    right: 0;
			    bottom: 0;
			    background: rgba(0, 0, 0, 0.4); /* 背景颜色放在这里 */
			    z-index: -1; /* 确保背景在 dbox 之下 */
			}
			
			.dbox{
				width: 100%;
				height: 738rpx;
				background: #FFFFFF;
				box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(0,0,0,0.07);
				border-radius: 24rpx 24rpx 0rpx 0rpx;
				padding: 48rpx 30rpx 0;
				box-sizing: border-box;
				&-top{
					&-title{
						width: calc(100% - 60rpx);
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 32rpx;
						color: #002846;
						line-height: 40rpx;
					}
					&-expand{
						width: 32rpx;
						height: 32rpx;
						transform: rotate(180deg);
					}
				}
				&-name {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #667e90;
					line-height: 24rpx;
					margin-top: 24rpx;
				}
				&-status{
					width: 140rpx;
					margin-top: 28rpx;
					border-radius: 16rpx;
					padding: 11rpx 15rpx;
					box-sizing: border-box;
					&.dwc{
						background: #BA9B31;	
					}
					&.ywc{
						background: #64BBBB;	
					}
					image{
						width: 26rpx;
						height: 26rpx;
					}
					text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #193D59;
						line-height: 24rpx;
						margin-left: 12rpx;
					}
				}
				&-menu{
					margin-top: 43rpx;
					overflow: hidden;
					flex-wrap: wrap;
					justify-content: space-between;
					&-pre{
						width: calc(25% - 22.5rpx);
						background: #F7F8FA;
						border-radius: 24rpx;
						padding: 30rpx 20rpx;
						margin-top: 24rpx;
						box-sizing: border-box;
						image{
							width: 42rpx;
							height: 42rpx;
						}
						text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #002846;
							line-height: 24rpx;
							text-align: center;
							margin-top: 24rpx;
						}
					}
				}
			}
		}
	}
</style>