<template>
	<div class="app-list-content">
		<cl-crud class="liu-crud-wrap" @load="onLoad">
			<template #slot-tabs="{ scope }">
				<el-tabs
					class="liu-tabs"
					type="border-card"
					v-model="tabIndex"
					@tab-click="handleClick"
				>
					<el-tab-pane
						v-for="(item, index) in tabsData"
						:key="index"
						:label="`${item.text}(${item.val})`"
						:name="item.key"
					></el-tab-pane>
				</el-tabs>
			</template>

			<!-- 头像 -->
			<template #table-column-smallAvatarUrl="{scope}">
				<cl-avatar
					shape="square"
					:size="40"
					:src="scope.row.smallAvatarUrl | default_avatar"
					:style="{ margin: 'auto' }"
				></cl-avatar>
			</template>

			<!-- 余额 -->
			<template #table-column-balance="{scope}">
				<span>{{ scope.row.userAsset.balance }}</span>
			</template>

			<!-- 成长值 -->
			<template #table-column-growth="{scope}">
				<span>{{ scope.row.userAsset.growth }}</span>
			</template>

			<!-- 关注公众号 -->
			<template #table-column-subscribe="{scope}">
				<span>{{ scope.row.subscribe == 1 ? '有' : '没有' }}</span>
			</template>
		</cl-crud>
	</div>
</template>

<script>
export default {
	data() {
		return {
			// cl-crud
			app: null,
			tabIndex: '-1',
			tabsData: [
				{
					text: '全部',
					key: '-1',
					val: 0
				},
				{
					text: '粉丝',
					key: '1',
					val: 0
				},
				{
					text: '会员',
					key: '2',
					val: 0
				}
			]
		};
	},
	mounted() {},
	methods: {
		handleClick() {
			this.app.refresh({ status: this.tabIndex });
		},
		// crud
		onLoad({ ctx, app }) {
			this.app = app;
			ctx.service(this.$service.sassMember)
				.set('table', {
					columns: [
						{
							prop: 'id',
							label: 'ID',
							align: 'center'
						},
						{
							prop: 'smallAvatarUrl',
							label: '头像',
							align: 'center'
						},
						{
							prop: 'userName',
							label: '来源昵称',
							align: 'center'
						},
						{
							prop: 'mobile',
							label: '手机号',
							align: 'center'
						},
						{
							prop: 'merchantName',
							label: '商家',
							align: 'center',
							emptyText: '无'
						},
						{
							prop: 'memberLevel',
							label: '会员级别',
							align: 'center',
							emptyText: '无'
						},
						{
							prop: 'balance',
							label: '余额',
							align: 'center',
							emptyText: '无'
						},
						{
							prop: 'growth',
							label: '成长值',
							align: 'center',
							emptyText: '无'
						},
						{
							prop: 'subscribe',
							label: '关注公众号',
							align: 'center',
							emptyText: '无'
						},
						{
							prop: 'visitTimeFormat',
							label: '最近访问',
							align: 'center',
							emptyText: '无'
						}
					],
					// 操作列
					op: {
						visible: false, // 是否显示
						// 同 element-ui Table-column Attributes
						props: {
							width: 150,
							align: 'center',
							fixed: 'right',
							label: '操作'
						},
						// 布局，请移步 `layout`
						layout: []
					}
				})
				.set('layout', [
					['slot-tabs'],
					['flex1', 'refresh-btn'],
					['data-table'],
					['flex1', 'pagination']
				])
				.on('refresh', async (params, { next }) => {
					// 继续执行刷新
					let { asset } = await next(params);
					if (this.$util.isEmpty(asset)) return false;
					this.tabsData[0].val = asset.totalUser;
					this.tabsData[1].val = asset.totalFans;
					this.tabsData[2].val = asset.totalMember;
				})
				.done();
			app.refresh();
		}
	}
};
</script>
