<template>
	<view class="challenge-vs-container">
		<view class="challenage-vs-users">
			<view class="user-fixed challenge-user">
				<view class="user-top">
					<view class="user-body">
						<view class="user-title">挑战者</view>
						<view
							class="user-avatar"
							:style="{ backgroundImage: `url(${pkInfo.challenge_avatar})` }"
						/>
					</view>
				</view>
				<view class="user-bottom">{{ pkInfo.challenge_nickname | getName }}</view>
			</view>
			<view class="user-fixed accept-user">
				<view class="user-top" @click="choose">
					<view class="user-body">
						<view
							class="user-avatar"
							:class="pkInfo.accept_avatar ? '' : 'no-user'"
							:style="{ backgroundImage: `url(${pkInfo.accept_avatar || ''})` }"
						/>
						<view class="user-title">应战者</view>
					</view>
				</view>
				<view class="user-bottom">{{ pkInfo.accept_nickname | getName }}</view>
			</view>
		</view>
		<view class="challenage-vs-main">
			<view class="title">挑战期限</view>
			<view class="count-time">{{ chooseLong }}天</view>
			<view class="accept-time">
				<template v-if="false">
					<view class="accept-time-text">应战倒计时：</view>
					<view class="accept-time-value">24:00:00</view>
				</template>
			</view>
			<view class="control-btns">
				<!-- <view class="btn">确定</view> -->
				<view v-if="type == 2" class="btn refuse" @click="reject">拒绝</view>
				<view v-if="type == 0 || type == 2" class="btn" @click="submit">{{ type == 0 ? '确定' : '应战' }}</view>
				<!-- <view class="btn">放弃挑战</view> -->
			</view>
		</view>
	</view>
</template>
<script>
	import { api_initiate_challenge, api_accept_challenge } from '@/api.js'
	export default {
		data() {
			return {
				pageTitle: '发起挑战',
				chooseLong: Math.abs(Number(this.$store.state.userServerInfo.day)),
				type: 0,
				pkInfo: {
					challenge_id: this.$store.state.userServerInfo.id,
					challenge_avatar: this.$store.state.userServerInfo.avatar,
					challenge_nickname: this.$store.state.userServerInfo.name,
				}
			}
		},
		filters: {
			getName(name) {
				if (name) {
					return name.length > 8 ? name.slice(0, 8) + '...' : name
				}
				return '选择应战人'
			},
		},
		onShow() {
			if (uni.pkInfo) {
				this.pkInfo = { ...this.pkInfo, ...uni.pkInfo }
				uni.pkInfo = null
			}
		},
		onLoad({ type }) {
			uni.hideLoading()
			if (type == 1) { // 我的挑战
				this.pageTitle = '我的挑战'
				this.type = 1
			}
			if (type == 2) { // 接受挑战
				this.pageTitle = '接受挑战'
				this.type = 2
			}
		},
		methods: {
			choose() { // 点击选择用户
				if (this.pageTitle === '发起挑战') {
					uni.navigateTo({ url: '../challengeChoose/challengeChoose' })
				}
			},
			reject() { // 拒绝挑战
				uni.showModal({ title: '提示', content: `确定拒绝${this.pkInfo.challenge_nickname}的挑战？` }).then(([, { confirm }]) => {
					if (confirm) {
						uni.loading()
						this.$ajax.get(`${api_accept_challenge}?id=${this.pkInfo.id}&type=1`).then(([, { data: { code, msg }}]) => {
							uni.hideLoading()
							if (code == 200) {
								uni.navigateBack()
								uni.$emit('RejectPK', this.pkInfo.id)
							} else {
								uni.showModal({ title: '提示', showCancel: false, content: msg })
							}
						})
					}
				})
			},
			submit() { // 提交挑战
				if (!this.pkInfo.accept_id) {
					// this.$refs.toast.hover('请选择挑战人', 3456, 'center')
					uni.showToast({ title: '请选择挑战人', duration: 2000, icon:"none"})
					return
				}
				if (!this.chooseLong) {
					// this.$refs.toast.hover('请选择挑战时长', 3456, 'center')
					uni.showToast({ title: '请选择挑战时长', duration: 2000, icon:"none"})
					return
				}
				if (this.type == 0) {
					uni.showModal({ title: '提示', content: `确定向${this.pkInfo.accept_nickname}发起挑战，期限${this.chooseLong}天？` }).then(([, { confirm }]) => {
						if (confirm) {
							uni.loading()
							this.$ajax.get(`${api_initiate_challenge}?id=${this.pkInfo.accept_id}`).then(([, { data: { code, msg }}]) => {
								uni.hideLoading()
								if (code == 200) {
									uni.navigateBack()
									uni.$emit('REDRESH')
								} else {
									uni.showModal({ title: '提示', showCancel: false, content: msg })
								}
							})
						}
					})
				} else if (this.type == 1) {
					console.log('放弃挑战')
				} else if (this.type == 2) {
					uni.showModal({ title: '提示', content: `确定接受${this.pkInfo.challenge_nickname}的挑战？` }).then(([, { confirm }]) => {
						if (confirm) {
							uni.loading()
							this.$ajax.get(`${api_accept_challenge}?id=${this.pkInfo.id}&type=0`).then(([, { data: { code, msg }}]) => {
								uni.hideLoading()
								if (code == 200) {
									uni.navigateBack()
									uni.$emit('AcceptPK', this.pkInfo.id)
								} else {
									uni.showModal({ title: '提示', showCancel: false, content: msg })
									uni.navigateBack()
									uni.$emit('REDRESH')
								}
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		.challenge-vs-container {
			flex: 1;
			overflow: hidden;
			background: url(../../static/new_challenge/bg.jpg) no-repeat top;
			background-size: 100%;
			.challenage-vs-users {
				width: 100%;
				height: 524rpx;
				position: relative;
				margin: 50rpx 0 28rpx 0;
				.user-fixed {
					position: absolute;
					.user-top {
						width: 259rpx;
						height: 122rpx;
						box-sizing: border-box;
						display: flex;
						align-items: center;
						margin-bottom: 20rpx;
						.user-body {
							width: 250rpx;
							height: 110rpx;
							display: flex;
							align-items: center;
							padding: 0 8rpx;
							box-sizing: border-box;
							.user-avatar {
								width: 90rpx;
								height: 90rpx;
								background-position: center;
								background-repeat: no-repeat;
								background-size: 100%;
								background-color: #FFFFFF;
								border-radius: 50%;
								overflow: hidden;
								&.no-user {
									position: relative;
									&::after, &::before {
										content: "";
										display: block;
										width: 56rpx;
										height: 6rpx;
										border-radius: 56rpx;
										position: absolute;
										top: 50%;
										left: 50%;
										background-color: #35B7FD;
										transform: translate(-50%, -50%);
									}
									&::after {
										transform: translate(-50%, -50%) rotate(90deg) !important;
									}
								}
							}
							.user-title {
								flex: 1;
								overflow: hidden;
								text-align: center;
								color: #FFFFFF;
								font-size: 32rpx;
							}
						}
					}
					.user-bottom {
						width: 260rpx;
						color: #FFFFFF;
						font-size: 28rpx;
						line-height: 40rpx;
						text-overflow: ellipsis;
						padding: 0 30rpx;
						box-sizing: border-box;
					}
					&.challenge-user {
						top: 0px;
						left: -1px;
						.user-top {
							border: 1px solid #FEC200;
							border-left: none;
							border-top-right-radius: 120rpx;
							border-bottom-right-radius: 120rpx;
							.user-body {
								background: linear-gradient(80deg, #FFC401 0%, #FE0000 100%);
								border-top-right-radius: 110rpx;
								border-bottom-right-radius: 110rpx;
								justify-content: flex-end;
							}
						}
						.user-bottom {
							text-align: left;
						}
					}
					&.accept-user {
						bottom: 0rpx;
						right: -1px;
						.user-top {
							border: 1px solid #71C6FB;
							border-right: none;
							border-top-left-radius: 120rpx;
							border-bottom-left-radius: 120rpx;
							justify-content: flex-end;
							.user-body {
								background: linear-gradient(112deg, #13AEFE 0%, #7BC9FB 100%);
								border-top-left-radius: 110rpx;
								border-bottom-left-radius: 110rpx;
							}
						}
						.user-bottom {
							text-align: right;
						}
					}
				}
			}
			.challenage-vs-main {
				display: flex;
				flex-direction: column;
				align-items: center;
				.title {
					color: #FFFFFF;
					font-size: 36rpx;
					line-height: 50rpx;
					margin-bottom: 46rpx;
				}
				.count-time {
					width: 304rpx;
					height: 90rpx;
					border-radius: 6rpx;
					background-color: #353235;
					color: #FFFFFF;
					font-size: 60rpx;
					line-height: 90rpx;
					text-align: center;
					margin-bottom: 90rpx;
				}
				.accept-time {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 98rpx;
					.accept-time-text {
						color: #FFFFFF;
						font-size: 32rpx;
						line-height: 44rpx;
					}
					.accept-time-value {
						width: 222rpx;
						height: 62rpx;
						background-color: #F2DF42;
						color: #EA4A41;
						font-size: 36rpx;
						line-height: 62rpx;
						text-align: center;
					}
				}
				.control-btns {
					width: calc(100% - 250rpx);
					margin: 0 auto;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.btn {
						flex: 1;
						overflow: hidden;
						height: 96rpx;
						line-height: 96rpx;
						text-align: center;
						border-radius: 96rpx;
						background: linear-gradient(180deg, #FEEB71 0%, #F89018 100%);
						color: #984100;
						font-size: 32rpx;
						font-weight: bold;
						&:nth-last-of-type(1) {
							margin-left: 36rpx;
						}
						&.refuse {
							color: #333333 !important;
						}
					}
				}
			}
		}
	}
	
	
	.new {
		@include page();
		.content {
			position: relative;
			.bg {
				width: 100%;
				height: calc(100% + 90rpx);
				margin-top: -90rpx ;
			}
			.person1, .person2 {
				left: 0;
				top: 201rpx;
				width:240rpx;
				height:120rpx;
				@include flex();
				position: absolute;
				justify-content: flex-start;
				border-radius:0rpx 57rpx 57rpx 0rpx;
				border:1rpx solid rgba(239,115,42,1);
				&.person2 {
					right: 0;
					left: auto;
					border-color: #C55AAE;
					justify-content: flex-end;
					border-radius:57rpx 0rpx 0rpx 57rpx;
				}
				.con {
					width:235rpx;
					height:110rpx;
					@include flex();
					background:rgba(239,115,42,1);
					border-radius:0rpx 55rpx 55rpx 0rpx;
					&.right {
						background:#C55AAE;
						border-radius:55rpx 0rpx 0rpx 55rpx;
					}
					.title {
						flex: 1;
						height: 100%;
						@include flex();
						color: #FFFFFF;
						font-size: 36rpx;
					}
					.avatar {
						margin: 6rpx;
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
					}
				}
			}
			.person1Name, .person2Name {
				top: 331rpx;
				position: absolute;
				font-size: 32rpx;
				&.person1Name {
					left: 31rpx;
					color: #EF732A;
				}
				&.person2Name {
					right: 31rpx;
					color: #C55AAE;
				}
			}
			.centerTitle {
				bottom: 725rpx;
				left: 300rpx;
				font-size:36rpx;
				position: absolute;
				color: rgba(239,115,42,1);
				text-shadow: 0rpx 1rpx 0rpx rgba(173,74,16,0.75);
			}
				
			.timecountdown {
				bottom: 450rpx !important;
				width: 540rpx !important;
				font-size: 36rpx !important;
				letter-spacing: 10rpx;
				left: 50% !important;
				margin-left: -260rpx !important;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #EF732A !important;
				
				.black {
					background: #333333;
					padding: 2rpx 4rpx;
				}
			}
			
			
			.long {
				left: 256rpx;
				opacity: 0.5;
				width: 240rpx;
				height: 98rpx;
				bottom: 590rpx;
				font-size: 54rpx;
				font-weight:bold;
				position: absolute;
				color:rgba(239,115,42,1);
				text-shadow:0rpx 1rpx 0rpx rgba(173,74,16,0.75);
				background: rgba(254,234,177,1);
				box-shadow:0px 15px 20px 0px rgba(230, 41, 126, 0.35), 3px 4px 0px 0px rgba(255,255,255,0.5), -3px -4px 0px 0px rgba(212,152,0,0.5);
				&.nopicker {
					@include flex();
				}
				.num {
					width: 100%;
					height: 98rpx;
					@include flex();
				}
			}
			.submit {
				bottom: 135rpx;
				left: 195rpx;
				width:360rpx;
				height:98rpx;
				@include flex();
				font-size:36rpx;
				position: absolute;
				border-radius:49rpx;
				color:rgba(239,115,42,1);
				text-shadow:0rpx 1rpx 0rpx rgba(173,74,16,0.75);
				background: rgba(254,234,177,1);
				box-shadow:0rpx 15rpx 20rpx 0rpx rgba(230, 41, 126, 0.35), 3rpx 4rpx 0rpx 0rpx rgba(255,255,255,0.5), -3rpx -4rpx 0rpx 0rpx rgba(212,152,0,0.5);
				&.reject {
					bottom: 280rpx;
					color:rgba(102,102,102,1);
					text-shadow:0px 1px 0px rgba(173,74,16,0.75);
				}
			}
		}
	}
</style>
