<template>
	<view class="content">
		<scroll-view scroll-y="true" :style="{'height': height}" class="stk-list-wrap">
			<view class="" style="height: 20rpx;"></view>
			<view class="stk-wrap" v-for="item in list">
				<image src="https://zccy.liuniu946.com/static/img/stk.png" mode="" class="stk-bg"></image>
				<view class="stk-name">
					<image src="/static/icon/stk-logo.png" mode=""></image>
					{{item.name}}
				</view>
				<view class="stk-info flex">
					<view class="info-no">
						卡号：<text>{{item.code}}</text>
					</view>
					<view class="info-state">
						{{type==1?'已发放':'已核销'}}
					</view>
				</view>
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
			<view class="btm-btn">
				<view class="btn" @click="opentc">
					{{type==1?'实体卡发放':'实体卡核销'}}
				</view>
			</view>
		</scroll-view>
		<uni-popup ref="popupStk" type="center">
			<view class="stk-tc">
				<image src="https://zccy.liuniu946.com/static/img/gift-bg.png" mode="" class="gift-bg"></image>
				<view class="gift-bg-mb"></view>
				<image src="https://zccy.liuniu946.com/static/img/stk-gift.png" mode="" class="gift"></image>
				<view class="" style="height: 145rpx;">
					
				</view>
				<view class="tc-tit">
					{{type==1?'实体卡发放':'实体卡核销'}}
				</view>
				<input type="text" class="tc-inp" placeholder="请输入卡号" v-model="code">
				<input type="text" class="tc-inp tc-inp-km" placeholder="请输入卡密" v-model="password" v-if="type == 2">
				<view class="tc-btn" @click="gonew">
					{{type==1?'立即发放':'立即核销'}}
				</view>
				<view class="" style="height: 50rpx;">
					
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import { sendStk, checkStk, getStk } from '@/api/user.js'
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				height: '',
				type: 0,
				code: '',//卡号
				password: '',//卡密
				uping: false,
				loaded: false,
				loadingType: 'more',
				page: 1,
				limit: 100,
				list: []
			}
		},
		onLoad(opt) {
			this.type = opt.type
			if (opt.type == 1) {
				uni.setNavigationBarTitle({
					title: '发放实体卡'
				})
				
			} else {
				uni.setNavigationBarTitle({
					title: '核销实体卡'
				})
			}
			this.getBase()
		},
		onShow() {
			console.log(this.$store.state.baseURL)
		},
		onReachBottom() {
			this.getBase()
		},
		onReady(res) {
			var obj = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.stk-list-wrap').boundingClientRect();
					query.exec(function(res) {
						obj.height = resu.windowHeight - res[0].top + 'px';
					});
				},
				fail: res => {}
			});
		},
		methods: {
			getBase(type) {
				let obj = this
				if(type == 'reload') {
					obj.page = 1,
					obj.loadingType = 'more'
					obj.list = []
				}
				if(obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				getStk({
					page: obj.page,
					limit: obj.limit,
					status: obj.type
				}).then(res => {
					console.log(res)
					obj.list = obj.list.concat(res.data.list.list.data)
					if(res.data.list.list.data.length == obj.limit) {
						obj.loadingType = 'more'
					}else {
						obj.loadingType = 'noMore'
					}
				})
			},
			opentc() {
				this.$refs.popupStk.open()
			},
			gonew() {
				let obj = this
				if(obj.uping) {
					return
				}
				if(obj.code == '') {
					return obj.$api.msg('请输入卡号')
				}
				if(obj.type == 2) {
					if(obj.password == '') {
						return obj.$api.msg('请输入卡密')
					}
				}
				obj.uping = true
				if(obj.type == 1) {
					sendStk({
						code: obj.code
					}).then(res => {
						obj.$refs.popupStk.close()
						obj.code = ''
						obj.password = ''
						obj.getBase('reload')
						uni.showToast({
							title:'发放成功',
							duration:2000
						});
						obj.uping = false
					}).catch(err => {
						obj.uping = false
					})
				}else if(obj.type == 2) {
					checkStk({
						code: obj.code,
						password: obj.password
					}).then(res => {
						obj.$refs.popupStk.close()
						obj.code = ''
						obj.password = ''
						obj.getBase('reload')
						uni.showToast({
							title:'核销成功',
							duration:2000
						});
						obj.uping = false
					}).catch(err => {
						obj.uping = false
					})
				}
				
			}
		}
	}
</script>

<style lang="scss">
	.stk-list-wrap {
		position: relative;
		padding-bottom: 156rpx;

		.btm-btn {
			// background-color: #f8f8f8;
			position: fixed;
			height: 156rpx;
			bottom: 0;
			width: 750rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.btn {
				border-radius: 20rpx;
				background-color: #901b21;
				color: #fff;
				width: 622rpx;
				height: 86rpx;
				line-height: 86rpx;
				text-align: center;
			}
		}

		.stk-wrap {
			width: 705rpx;
			height: 204rpx;
			margin: 0 auto 20rpx;
			position: relative;

			.stk-bg {
				width: 705rpx;
				height: 204rpx;
				position: absolute;
				top: 0;
			}

			.stk-name {
				position: absolute;
				display: flex;
				height: 40rpx;
				align-items: center;
				font-size: 36rpx;
				color: #6b4713;
				font-weight: bold;
				top: 46rpx;
				left: 24rpx;

				image {
					width: 39rpx;
					height: 36rpx;
					margin-right: 10rpx;
				}
			}

			.stk-info {
				position: absolute;
				bottom: 0;
				height: 90rpx;
				width: 705rpx;
				padding: 0 24rpx;
				font-size: 26rpx;

				.info-no {
					text {
						color: #8f1b1f;
					}
				}

				// background-color: red;
			}
		}
	}

	.stk-tc {
		width: 560rpx;
		// height: 550rpx;
		background-color: #fff;
		border-radius: 20rpx;
		position: relative;
		.gift, .gift-bg, .gift-bg-mb,  {
			position: absolute;
			left: 0;
			right: 0;
			margin: auto;
		}
		.tc-tit, .tc-inp, .tc-btn {
			margin: auto;
		}
		.gift {
			top: -93rpx;
			width: 177rpx;
			height: 186rpx;
		}
		.gift-bg-mb {
			top: 0;
			width: 456rpx;
			height: 90rpx;
			background-color: #fff;
		}
		.gift-bg {
			top: -90rpx;
			width: 456rpx;
			height: 180rpx;
		}
		.tc-tit {
			font-weight: bold;
			color: #333333;
			font-size: 36rpx;
			text-align: center;
		}
		.tc-inp {
			display: block;
			margin: 20rpx auto;
			width: 440rpx;
			height: 70rpx;
			background-color: #f5f5f5;
			top: 210rpx;
			padding-left: 20rpx;
			border-radius: 15rpx;
			font-size: 32rpx;
		}
		.tc-inp-km {
			top: 305rpx;
		}
		.tc-btn {
			width: 405rpx;
			height: 86rpx;
			margin-top: 45rpx;
			background-color: #901b21;
			border-radius: 43rpx;
			text-align: center;
			color: #fff;
			line-height: 86rpx;
			font-size: 36rpx;
		}
	}
</style>
