<template>
	<view class="container">
		<text class="iconfont icon-youjiantou zuojiantou" @click="goBack()" style="top: 20rpx;"></text>
		<!-- tab选项 -->
		<view class="tabTitle">
			<text :class="{'actived': tabType===1}" @click="switchTab(1)">报名项目</text>
			<text :class="{'actived': tabType===2}" @click="switchTab(2)">获得认证</text>
		</view>
		<!-- tab内容 -->
		<view class="tabContent">
			<!-- 报名项目 -->
			<view class="signItemsInfoBox" v-if="tabType===1">
				<view class="signListsBox" v-if="signupInfos.length>0">
					<view class="signItem" v-for="item in signupInfos" :key="item.cerNo" @click="toDetail(item)">
						<view class="seriesNo">{{item.cerNo}}</view>
						<view class="certifyInfo">
							<view class="signBox">
								<text>报名时间：{{dateFormat(item.signupDate)}}</text>
								<text :class="statusInfo(item.status).statusClass">{{statusInfo(item.status).statusTitle}}</text>
							</view>
							<view>
								<text>认证方向：{{item.cerName}}</text>
								<i class="iconfont icon-youjiantou"></i>
							</view>
						</view>
					</view>
				</view>
				<!-- 没有报名项目 -->
				<view class="noSign" v-if="signupInfos.length==0">
					<h4>暂没有报名项目！</h4>
				</view>
			</view>
			<view class="getCertifyListsBox" v-if="tabType===2">
				<view class="certifyListContent" v-if="userCertificates.length>0">
					<view class="certifyInfoItem" v-for="item in userCertificates" :key="item.cerNo" @click="previewImg(item)">
						<h4>{{item.cerName}}</h4>
						<view class="certifyImgBox">
							<image :src="item.cerUrl" alt="" srcset="" mode="widthFix"></image>
							<view>认证编号：{{item.authenticationCode}}</view>
							<view>有效期至：{{dateFormat(item.endDate)}}</view>
						</view>
					</view>
				</view>
				<!-- 没有证书 -->
				<view class="noSign" v-if="userCertificates.length==0">
					<h4>暂没有取得证书！</h4>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import api from "../../api/api";
	import dataHandling from "@/util/dataHandling";
	export default {
		data() {
			return {
				tabType:1,
				signupInfos:[],
				userCertificates:[],
				userId: uni.getStorageSync('cffp_userId')
			};
		},
		methods:{
			goBack(){
				uni.navigateBack({  
					delta: 1  
				});	
			},
			switchTab(type){
				this.tabType = type;
				if(type===1){
					this.querySignupList();
				}else{
					this.userCertificateList();
				}
			},
			querySignupList(){
				api.querySignupList({userId:this.userId}).then(res=>{
					if(res['success']){
						this.signupInfos = res['data']['signupInfos']
					}
				})
			},
			userCertificateList(){
				const param = {
					queryType:1,
					userId: this.userId
				}
				api.userCertificateList(param).then(res=>{
					if(res['success']){
						this.userCertificates = res['data']['userCertificates']
					}
				})
			},
			toDetail(item){
				uni.navigateTo({
					url:`/pages/certifyDetail/certifyDetail?userSignupId=${item.userSignupId}&certificateId=null&status=${item.status}`
				})
			},
			dateFormat(val){
				return dataHandling.dateFormat(val,'yyyy-MM-dd')
			},
			// 状态
			statusInfo(val){
				const statusInfo = {
					statusTitle:'认证中',
					statusClass:'certifing'
				}
				if(val==0){
					statusInfo.statusTitle = '已取消';
					statusInfo.statusClass = 'cancel'
				}else if(val==2){
					statusInfo.statusTitle = '未通过';
					statusInfo.statusClass = 'fail'
				}else if(val==3){
					statusInfo.statusTitle = '通过';
					statusInfo.statusClass = 'success'
				}else{
					statusInfo.statusTitle = '认证中';
					statusInfo.statusClass = 'certifing'
				}
				return statusInfo;
			},
			// 预览图片
			previewImg(e){
				if(!e.cerUrl){
					return;
				}
				// 预览图片
				uni.previewImage({
					urls:[e.cerUrl],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			}
		},
		onLoad(){
			this.querySignupList();
		},
		
	}
</script>

<style lang="scss">
	.container{
		position: relative;
		height: 100%;
		.tabTitle{
			color: #666666;
			background-color:#fff;
			display: flex;
			align-items: center;
			justify-content: space-around;
			padding: 30rpx 60rpx 0;
			text{
				&.actived{
					color: #333;
					border-bottom: 4rpx solid #20269B;
				}
			}
		}
		.tabContent{
			.signListsBox{
				width: 95%;
				margin: 0 auto;
				.signItem{
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					background-color: #fff;
					padding:20rpx 20rpx 20rpx 50rpx;
					border-radius: 10rpx;
					.seriesNo{
						width: 0;
						height: 56rpx;
						flex: 0 0 56rpx;
						border-radius: 50%;
						background:linear-gradient(137deg, #E1B972 0%, #CDA464 100%);
						color: #fff;
						display: flex;
						justify-content: center;
						align-items: center;
						margin-right: 60rpx;
					}
					.certifyInfo{
						width: 0;
						flex: 1;
						view{
							display: flex;
							justify-content: space-between;
							align-items: baseline;
							&.signBox{
								justify-content: flex-start;
								text:last-child{
									margin-left: 20rpx;
									border-radius: 10rpx;
									width: 96rpx;
									height: 40rpx;
									display: flex;
									justify-content: center;
									align-items: center;
									background: linear-gradient(90deg, #5FB574 0%, #0BB175 0%, #87CE98 100%);
									color: #fff;
									font-size: 24rpx;
									&.success{
										background: linear-gradient(90deg, #5FB574 0%, #0BB175 0%, #87CE98 100%);
									}
									&.fail{
										background: linear-gradient(90deg, #FF2E2E 0%, #FF7B7B 100%);
									}
									&.certifing{
										background: linear-gradient(90deg, #2E82FF 0%, #63A1FE 100%);
									}
									&.cancel{
										background: #9c9c9c;
									}
								}
							}
						}
					}
					
				}
			}
			.noSign{
				text-align: center;
				margin-top: 10%;
			}
			.getCertifyListsBox{
				.certifyInfoItem{
					background-color: #fff;
					// margin-bottom: 10rpx;
					border-bottom: 10rpx #FBFBFB solid;
					padding: 20rpx 0;
					width: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;
					image{
						max-width: 100%;
						height: auto;
						margin: 0 auto;
					}
					.certifyImgBox{
						display: flex;
						flex-direction: column;
						align-items: flex-start;
					}
				}
				.certifyInfoItem:last-child{
					border-bottom: 0;
				}
			}
		}
	}
</style>
