<template>
	<view>
		<u-navbar :custom-back="back" title="会员中心"></u-navbar>
		
		<block >
			<view class="empty-page" v-if="!isOpenMember">
				<image src="../../static/icon/huiyuan1.png" mode=""></image>
				<text style="color: #303030;font-weight: 800;font-size: 32rpx;margin-top: 25rpx;">创建会员中心</text>
				<text style="color: #888888;font-size: 26rpx;margin-top: 20rpx;">设置积分价值、积分规则</text>
				<text style="color: #888888;font-size: 26rpx;margin-top: 10rpx;">实时查看会员信息和积分情况</text>
			
				<view @click="$jump('/pages/member-center/add')" class="cu-btn btn-bg-color round" style="width: 48%;height: 80rpx;position: fixed;bottom: 40%;">
					<u-icon name="plus-circle-fill" size="40"></u-icon>
					<text class="margin-left-10 text-lg">创建会员中心</text>
				</view>
			</view>
			
			<block v-else>
				<view class="top-menu">
					<view class="leftFixed"> 1 积分等于 {{detail.pointUnitValue}} 元</view>
					<view class="u-flex u-row-between">
						<view class="item">
							<text>{{detail.memberNum}}</text>
							<text>会员数量</text>
						</view>
						
						<view class="item">
							<text>{{detail.totalPointValue}}</text>
							<text>积分总价值</text>
						</view>
					</view>
					<view class="u-m-t-50 u-flex u-col-center u-row-center">
						<text class="u-font-24 u-m-r-20">规则:消费 1 元赠送 {{detail.sendPointUnit}} 积分</text>
						<view class="cu-btn bg-white sm round" @click="changeRule" style="padding: 10rpx 20rpx;">
							更改规则
						</view>
					</view>
				</view>
				
				<view class="padding-20 text-bold text-xl">
					<text>会员列表</text>
				</view>
			
				<view class="card">
					<view class="flex tableTitle">
						<view class="area1 title">
							会员信息
						</view>
						<view class="area2 title">
							积分数
						</view>
						<view class="area3 title">
							价值
						</view>
					</view>
					<mescroll-body :height="height" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
						:down="downOption" :up="upOption">
						<view class="item" v-for="(item,index) in dataList" :key="index" @click="jumpDetail(item)">
							<view class="area1 flex text-bold">
								<image class="img-avatar"
									:src="item.avatar">
								</image>
								<view class="center padding-left-10">
									<text>{{item.nickName}}</text>
								</view>
							</view>
							<view class="area2 center text-bold text-lg"
								style="justify-content: flex-start;margin-left: 10rpx;color: #F37A1E;">
								<image class="img-points" src="../../static/icon/points-value.png"></image>
								<text class="margin-left-10">{{$digital.keepTwoDecimal(item.pointTotal)}}</text>
							</view>
			
							<view class="area3  center text-lg">
								<text class="text-price text-bold">{{$digital.keepTwoDecimal(item.pointValue)}}</text>
							</view>
						</view>
					</mescroll-body>
				</view>
			</block>
		</block>
		
		<u-popup v-model="showPopup" mode="center" border-radius="20">
			<view class="u-popup-content">
				<text>更改积分规则</text>
				<u-form>
					<u-form-item label-width="170"  label="消费1元赠送">
						<u-input   type="digit" height="50" :border="true" :clearable="false" v-model="sendPointUnit" placeholder="请输入积分数"/>
						<view slot="right" class="cu-tag btn-bg-color radius">
							积分
						</view>
					</u-form-item>
				</u-form>
				<u-button size="medium" shape="circle" :custom-style="customStyle1" @click="update">确定</u-button>
				<u-button size="medium" shape="circle" :custom-style="customStyle2" @click="showPopup=false">取消</u-button>
			</view>
		</u-popup>
		<loading ref="loading" type="3" theme="2"/>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-body/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				loading:true,
				
				isOpenMember: false,
				detail: {},
				dataList: [],
				
				height:'300px',
				downOption: {
					use: true,
					auto: false // 不自动加载
				},
				upOption: {
					use: true,
					auto: false // 不自动加载
				},
				showPopup:false,
				//消费一元赠送的积分值
				sendPointUnit:'',
				customStyle1:{
					padding:'40rpx',
					backgroundColor:'#F39248',
					color:'#fff',
					marginBottom:'20rpx',
					width:'350rpx'
				},
				customStyle2:{
					padding:'40rpx',
					width:'350rpx'
				},
			}
		},
		watch:{
			mescroll(){
				if (this.isOpenMember) {
					this.mescroll.resetUpScroll();
				}
			}
		},
		onReady() {
			this.getElInfo()
		},
		onLoad() {
			this.fetchMemberInfo()
		},
		methods: {
			changeRule(){
				uni.navigateTo({
					url:"./add?type='change'"
				})
			},
			back(){
				uni.reLaunch({
					url:"/pages/index/index"
				})
			},
			update(){
				let params = {
					id: this.vuex_mallId,
					sendPointUnnit:this.sendPointUnit
				}
				this.$api.mall.submit(params).then(res => {
					this.fetchMemberInfo()
					this.showPopup = false
				})
			},
			
			async getElInfo() {
				let rectInfo = await this.$u.getRect('.tableTitle');
				this.height=this.$u.sys().windowHeight - rectInfo.top + 'px'
			},
			fetchMemberInfo(){
				let params={
					mallId:this.vuex_mallId
				}
				this.$api.mall.memberInfo(params).then(res=>{
					if (res.data.isOpenMember == 1) {
						this.isOpenMember = true
						this.detail = res.data
					}
				})
			},
			downCallback() {
				setTimeout(()=>{
					this.mescroll.resetUpScroll();
				},800)
			},
			upCallback(mescroll) {
				let params = {
					mallId: this.vuex_mallId,
					current: mescroll.num,
					size: mescroll.size,
					//商场会员
					type:'MALL'
				}
				try {
					this.$api.member.list(params).then(res => {
						let data = res.data.records
						let total = res.data.total
						mescroll.endBySize(data.length, total);
						if (mescroll.num == 1) this.dataList = []; //如果是第一页需手动制空列表
						this.dataList = this.dataList.concat(data); //追加新数据
					})
				} catch (e) {
					console.log(e);
					this.mescroll.endErr()
				}
			},
			jumpDetail(item){
				let params={
					userId:item.userId,
					id:item.id
				}
				uni.navigateTo({
					url:"/pages/member/detail"+this.$u.queryParams(params)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.empty-page {
		height: 600rpx;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;

		image {
			width: 160rpx;
			height: 160rpx;
			margin-bottom: 20rpx;
		}
	}

	.top-menu {
		border-radius: 20rpx;
		margin: 20rpx 20rpx;
		padding: 40rpx;
		background-image: linear-gradient(to right, #272A46, #30355D);
		color: #fff;
		position: relative;
		.leftFixed{
			padding:0 10rpx;
			font-size: 22rpx;
			height: 40rpx;
			line-height: 40rpx;
			background-color: $color;
			color: #fff;
			position: absolute;
			top: 20rpx;
			left: 0;
			border-top-right-radius: 25px;
			border-bottom-right-radius: 25px;
		}
		.item {
			display: flex;
			flex-direction: column;
			text-align: center;
			align-items: center;
			justify-content: center;
			flex:1;
			margin-top: 40rpx;
			text:first-child {
				margin-bottom: 5rpx;
				color: #FFFFFF;
				font-weight: 800;
				font-size: 38rpx;
			}

			text:last-child {
				font-size: 26rpx;
				color: #D0D4EE;
			}
		}
	}
	
	.u-popup-content{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx;
		u-form{
			margin: 60rpx 0;
			width: 500rpx;
		}
	}
	
	.card {
		width: 100%;
		background-color: #FFFFFF;
		padding: 30rpx;
		font-size: 26rpx;

		.text-area {
			display: flex;
			justify-content: flex-start;
			align-items: center;
		}

		.title {
			padding-left: 20rpx;
			color: #666666;
		}

		.area1 {
			width: 50%;
		}

		.area2 {
			width: 30%;
		}

		.area3 {
			width: 20%;
		}

		.item {
			padding: 30rpx 20rpx;
			display: flex;
			border-bottom: 1rpx solid #DDDDDD;

			.img-avatar {
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
			}

			.img-points {
				width: 35rpx;
				height: 35rpx;
			}
		}
	}
</style>
