<template>
	<view v-if="merchantList.length>1">
		<uni-collapse :accordion="true" >
			<view v-for="(merchant,index) in merchantList" :key="merchant.id" >
				<uni-collapse-item :title="getMerchantTitle(merchant)" :open="index===0">
					<view style="padding: 20upx;background-color: aliceblue;">
						<view v-if="merchant.equipmentList.length==0" style="text-align: center;">暂无数据</view>
						<view v-if="merchant.equipmentList.length>0">
							<uni-collapse :accordion="true">
								<uni-collapse-item title="总销售情况" @open="open">
									<mainStatistics :adminId="merchant.id" style="padding: 20upx;"></mainStatistics>
								</uni-collapse-item>
								<view v-for="equipment in merchant.equipmentList" :key="equipment.id">
									<uni-collapse-item :title="getEquipmentTitle(equipment)" @open="open">
										<mainStatistics :equipmentId="equipment.id" style="padding: 20upx;"></mainStatistics>
									</uni-collapse-item>
								</view>
							</uni-collapse>	
						</view>
					</view>
				</uni-collapse-item>
			</view>
		</uni-collapse>
	</view>
	<view v-else>
		<view v-if="merchantList[0].equipmentList.length>1">
			<uni-collapse :accordion="true">
				<uni-collapse-item title="总销售情况" @open="open">
					<mainStatistics :adminId="merchantList[0].id" style="padding: 20upx;"></mainStatistics>
				</uni-collapse-item>
				<view v-for="equipment in merchantList[0].equipmentList" :key="equipment.id">
					<uni-collapse-item :title="getEquipmentTitle(equipment)" @open="open">
						<mainStatistics :equipmentId="equipment.id" style="padding: 20upx;"></mainStatistics>
					</uni-collapse-item>
				</view>
			</uni-collapse>	
		</view>
		<view v-else style="text-align: center;">暂无数据</view>
	</view>
</template>

<script>
    import {mapState,mapActions,mapMutations} from 'vuex'
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	
    export default {
		components: {
			uniCollapse,
			uniCollapseItem,
			uniList,
			uniListItem
		},
		data() {
			return {
				merchantList:[{equipmentList:[]}],
				extraIcon: {
					color: '#4cd964',
					size: '22',
					type: 'spinner'
				}
			}
		},
		computed: {
			...mapState(['loginUser']),
		},
		onLoad(state){
			this.getEquipmentListData();
		},
		onPullDownRefresh() {
			this.getEquipmentListData();
		},
		methods: {
			...mapActions('chart', ['getEquipmentListByUser']),
			open(e){
				for (let component of e.$children) {
					if(component.$options.name=='mainStatistics'){
						component.init();
						return;
					}
				}
			},
			getMerchantTitle(merchant){
				return merchant.name?merchant.name:merchant.username;
			},
			getEquipmentTitle(equipment){
				return equipment.name?equipment.name:equipment.clientId;
			},
			getEquipmentListData(){
				this.getEquipmentListByUser(this.loginUser)
				.then(data => {
					this.merchantList = data;
					uni.stopPullDownRefresh();
				}
				, _ => void uni.stopPullDownRefresh());
			}
		}
    }
</script>

<style>
	
</style>
