<template>
	<view class="content">
		<view class="redemptionCodeModel">
			<view class="redemptionCodeTitle">
				直购码
			</view>
			<view class="redemptionCodeInitModel">
				<u--input placeholder="请输入" :disabled="isShow" v-model="code" border="none"></u--input>
			</view>
		</view>
		<view class="applyModel">
			<view class="applyBtn" @click="bindingCode">确认</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: "",
				buycode: "",
				isShow: false
			}
		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			//绑定直购码
			bindingCode() {
				this.$request('/bind-buycode', 'POST', {
					code: this.code
				}).then(res => {
					console.log(res.data)
					this.code = null;
					uni.showToast({
						icon: 'success',
						title: '操作成功'
					})
					let now = new Date();
					uni.setStorageSync("codeTime", now)
					uni.switchTab({
						url: '/pages/product/product'
					})
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			//获取用户信息
			getUserInfo() {
				this.$request('/user-info', 'GET', {}).then(res => {
					console.log('请求成功');
					this.code = res.data.buycode;
					this.buycode = res.data.buycode;
					this.isShow = this.buycode.length > 0 ? true : false
				}).catch(err => {
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: calc(100vh - 30rpx);
		padding-top: 30rpx;
		background: linear-gradient(to bottom, #f3d4d7, #f8f8f8, #f8f8f8);
	}

	.redemptionCodeModel {
		width: calc(100% - 120rpx);
		padding: 30rpx;
		margin: 0 30rpx;
		height: auto;
		overflow-y: scroll;
		background-color: #ffffff;
		border-radius: 20rpx;

		.redemptionCodeTitle {
			font-size: 28rpx;
			color: #333333;
			height: 60rpx;
		}

		.redemptionCodeInitModel {
			width: calc(100% - 60rpx);
			padding: 30rpx;
			background-color: #F6F7F8;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			min-height: 60rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
		}


	}
</style>