<template>
	<view class="new">
		<custom-nav :title="pageTitle"></custom-nav>
		<custom-toast ref='toast'></custom-toast>
		<view class="content">
			<image class="bg" src="../../static/icon/challenge-bg.png"></image>
			<view class="person1">
				<view class="con">
					<view class="title">挑战者</view>
					<image class="avatar" :src="pkInfo.challenge_avatar"></image>
				</view>
			</view>
			<view class="person1Name">{{ pkInfo.challenge_nickname | getName }}</view>
			<view class="person2" @click="choose">
				<view class="con right">
					<image class="avatar" :src="pkInfo.accept_avatar ? pkInfo.accept_avatar : '../../static/icon/choose.png'"></image>
					<view class="title">应战者</view>
				</view>
			</view>
			<view class="person2Name">{{ pkInfo.accept_nickname | getName }}</view>
			<view class="centerTitle">挑战期限</view>
			<view class="long nopicker">{{ chooseLong }} 天</view>
			<!-- <view class="long nopicker timecountdown">应战倒计时：<view class="black">24:00:00</view></view> -->
			<view v-if="type == 2" class="submit reject" @click="reject">拒绝</view>
			<view v-if="type == 0 || type == 2" class="submit" @click="submit">{{ type == 0 ? '确定' : '应战' }}</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.userWeixinInfo.avatarUrl,
					challenge_nickname: this.$store.state.userWeixinInfo.nickName,
				}
			};
		},
		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
				console.log(this.pkInfo)
			}
		},
		onLoad({ type }) {
			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')
					return
				}
				if (!this.chooseLong) {
					this.$refs.toast.hover('请选择挑战时长', 3456, 'center')
					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">
	.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>
