<template>
	<NavBar />
	<view class="container">
		<view class="title">
			<PageTitle :title="$t('activity.pointExchange')" />
		</view>
		<view class="content">
			<Spin v-show="loading" />
			<view class="goods-info" v-if="info">
				<image :src="info.imgs.split(',')[0]" class="img"></image>
				<view class="info">
					<view class="order-title">{{ info.goods_name }}</view>
					<view class="order-point">
						<text class="point">{{ info.integral }}</text>
						<text>{{ ' ' + $t('activity.point') + ' x1' }}</text>
					</view>
				</view>
			</view>
			<view class="address-info" v-if="info">
				<view class="item">
					<image src="../../../static/shop/recipients.svg" class="icon"></image>
					<view class="item-content">
						<view class="label">{{ $t('activity.recipients') }}</view>
						<view class="value">{{ addr.user_name }}</view>
					</view>
					<view class="item-btn">
						<button class="mini-btn" size="mini" style="flex-grow: 0; margin-right: 0; font-size: 24upx; font-weight: 400" @click="goAddress">
							{{ $t('activity.addrMgr') }}
						</button>
					</view>
				</view>
				<view class="item">
					<image src="../../../static/shop/phone.svg" class="icon"></image>
					<view class="item-content">
						<view class="label">{{ $t('activity.phone') }}</view>
						<view class="value">{{ addr.user_phone }}</view>
					</view>
				</view>
				<view class="item">
					<image src="../../../static/shop/addr.svg" class="icon"></image>
					<view class="item-content">
						<view class="label">{{ $t('activity.addr') }}</view>
						<view class="value">
							{{ (addr.userCountry ? addr.userCountry + ' ' : '') + (addr.userCity ? addr.userCity + ' ' : '') + (addr.userAddr ? addr.userAddr : '') }}
						</view>
					</view>
				</view>
			</view>
			<view style="margin-top: 76upx" v-if="info">
				<button class="primaryButton" style="font-size: 32upx; font-weight: 400" @click="openApplyDialog">
					{{ $t('activity.exchange') }}
				</button>
			</view>
		</view>
	</view>
	<uni-popup ref="applyDialog" mask-background-color="transparent" :isMaskClick="false">
		<view class="formDialog">
			<view class="searchForm">
				<uni-forms ref="searchForm" :modelValue="applyFormData">
					<uni-row>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.goodsName')">
								<view style="height: 100%; display: flex; align-items: center">{{ info.goods_name }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.point')">
								<view style="height: 100%; display: flex; align-items: center">{{ info.integral }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.country')">
								<view style="height: 100%; display: flex; align-items: center">{{ addr.userCountry }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.userCity')">
								<view style="height: 100%; display: flex; align-items: center">{{ addr.userCity }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.addr')">
								<view style="height: 100%; display: flex; align-items: center">{{ addr.userAddr }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.phone')">
								<view style="height: 100%; display: flex; align-items: center">{{ addr.user_phone }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('activity.recipients')">
								<view style="height: 100%; display: flex; align-items: center">{{ addr.user_name }}</view>
							</uni-forms-item>
						</uni-col>
						<uni-col :span="24">
							<uni-forms-item :label="$t('form.remark.label')">
								<uni-easyinput primaryColor="#29BBE4" v-model="applyFormData.remark" />
							</uni-forms-item>
						</uni-col>
					</uni-row>
				</uni-forms>
				<view class="submitter">
					<button :disabled="submitBtnLoading" class="primaryButton" @click="save">
						<image v-show="submitBtnLoading" src="/static/loadingCircle.svg" mode="aspectFit" style="width: 16px; height: 16px"></image>
						{{ $t('common.okText') }}
					</button>
					<button class="secondaryButton" @click="closeApplyDialog">
						{{ $t('common.cancelText') }}
					</button>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
import { getGoodsInfo, dh } from '@/services/activity/shop';
export default {
	data() {
		return {
			id: undefined,
			info: null,
			addr: null,
			loading: false,
			submitBtnLoading: false,
			applyFormData: {
				id: undefined,
				remark: undefined
			}
		};
	},
	methods: {
		openApplyDialog() {
			this.applyFormData.id = this.info.id;
			this.applyFormData.remark = undefined;
			this.$refs.applyDialog.open();
		},
		closeApplyDialog() {
			this.$refs.applyDialog.close();
			this.applyFormData.id = undefined;
			this.applyFormData.remark = undefined;
		},
		isEmpty(str) {
		  if (this.isNumber(str)) str = str + '' //改为字符串，避免传入Number判定为空 add by mb 0526
		  if (str == "undefined" || str == 'null' || str == null || str == "" || str == undefined) {
		      return true;
		  } else {
		      return false;
		  }
		},
		isNumber(value) {
		  return typeof value === 'number' && !isNaN(value);
		},
		async save() {
			this.submitBtnLoading = true;
			let data = {
				...this.applyFormData,
				delivery_address: this.addr.userAddr,
				phone: this.addr.user_phone,
				recipients: this.addr.user_name,
				country: this.addr.userCountry,
				city: this.addr.userCity
			};
			if (this.isEmpty(data.delivery_address) || this.isEmpty(data.phone) || this.isEmpty(data.recipients)) {
				this.closeApplyDialog();
				this.$nextTick(() => {					
					this.$cusModal.showModal({
						type: 'message',
						status: 'warning',
						contentText: this.$t('activity.addressErrorTips')
					});
				})
				return
			}
			const resp = await dh(data);
			this.submitBtnLoading = false;
			if (resp.code == 0) {
				this.closeApplyDialog();
				this.$nextTick(() => {
					this.$cusModal.showModal({
						type: 'message',
						status: 'success',
						contentText: this.$t('activity.dh'),
						onClose() {
							uni.redirectTo({
								url: '/pages/activity/shop/myOrder'
							});
						}
					});
				})
			} else if (resp.msg == 'kcbz' || resp.msg == 'jfbz' || resp.msg == 'sqgd') {
				this.closeApplyDialog();
				this.$nextTick(() => {
					this.$cusModal.showModal({
						type: 'message',
						status: 'warning',
						contentText: this.$t('activity.' + resp.msg)
					});
				})
			} else {
				this.closeApplyDialog();
				this.$nextTick(() => {					
					this.$cusModal.showModal({
						type: 'message',
						status: 'warning',
						contentText: resp.msg ?? this.$t('common.error.sysError')
					});
				})
			}
		},
		goAddress() {
			uni.navigateTo({
				url: '/pages/activity/shop/address'
			});
		}
	},
	onLoad(options) {
		uni.$on('changeAddr', (data) => {
			this.addr = {
				userCountry: data.country,
				userCity: data.city,
				user_name: data.recipients,
				userAddr: data.delivery_address,
				user_phone: data.phone
			};
		});
		const { goods } = options;
		this.id = goods;
		this.loading = true;
		getGoodsInfo({ id: goods })
			.then((resp) => {
				if (resp.code == 0) {
					this.info = resp.data.info;
					resp.data.info = null;
					this.addr = resp.data;
				}
				this.loading = false;
			})
			.catch(() => {
				this.loading = false;
			});
	},
	onUnload() {
		uni.$off('changeAddr');
	}
};
</script>

<style lang="scss" scoped>
.container {
	padding: 0 40upx 162upx;
	.title {
		margin: 14px 0 15px;
	}
	.content {
		display: flex;
		flex-direction: column;
		margin-top: 40upx;
	}
}

.goods-info {
	width: 100%;
	display: flex;
	box-shadow: -1px 0px 5px 1px rgba(0, 0, 0, 0.15);
	padding: 32upx;
	box-sizing: border-box;
	margin-bottom: 34upx;

	.img {
		width: 272upx;
		height: 152upx;
		vertical-align: bottom;
		margin-right: 40upx;
		flex-shrink: 0;
	}

	.info {
		flex-grow: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.order-title {
			font-size: 40upx;
			font-weight: 700;
			color: rgba(51, 51, 51, 1);
		}

		.order-point {
			margin-top: 20upx;
			color: rgba(102, 102, 102, 1);
			font-size: 28upx;
			font-weight: 400;

			.point {
				color: rgba(41, 187, 228, 1);
				font-size: 28upx;
				font-weight: 400;
			}
		}
	}
}

.address-info {
	width: 100%;
	display: flex;
	flex-direction: column;
	box-shadow: -1px 0px 5px 1px rgba(0, 0, 0, 0.15);
	padding: 40upx;
	box-sizing: border-box;

	.item:not(:last-of-type) {
		margin-bottom: 40upx;
	}

	.item {
		display: flex;
		flex-wrap: nowrap;

		.icon {
			width: 40upx;
			height: 40upx;
			vertical-align: bottom;
			margin-right: 28upx;
			flex-shrink: 0;
		}

		.item-content {
			flex-shrink: 1;
			flex-grow: 1;

			.label {
				font-size: 36upx;
				font-weight: 700;
				color: rgba(51, 51, 51, 1);
				margin-bottom: 10upx;
			}

			.value {
				font-size: 32upx;
				font-weight: 400;
				color: rgba(119, 119, 119, 1);
			}
		}

		.item-btn {
			flex-shrink: 0;
		}
	}
}

.mini-btn {
	flex: 1;
	display: flex;
	justify-content: center;
	align-items: center;
	// height: 40px;
	color: #fff;
	background-color: rgba(41, 187, 228, 1);
	border-radius: 0px;
	cursor: pointer;

	&::after {
		border: none;
	}
}

.formDialog {
	padding: 0 14px;
	.searchForm {
		padding: 28px 16px;
		background: #fff;
		box-shadow: -1px 0px 5px 1px rgba(0, 0, 0, 0.15);
	}
	.submitter {
		display: flex;
		column-gap: 10px;
		color: #fff;
		.confirmBtn,
		.cancelBtn {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 40px;
			flex: 1;
		}
		.confirmBtn {
			background-color: rgba(77, 192, 229, 1);
		}
		.cancelBtn {
			background-color: rgba(15, 54, 117, 1);
		}
	}
}
</style>
