<template>
	<view class="xkl-com-bg">
		<view style="height: 20rpx;"></view>
		<view style="padding: 30rpx;background-color: #ffffff;border-radius: 10rpx;">
			<view style="font-size: 28rpx;">充电桩汇总</view>
			<view style="height: 10rpx;"></view>
			<u-grid :border="false" :col="3">
				<u-grid-item :customStyle="{padding:'20rpx 0'}">
					<view>{{deviceInfo.install_num}}</view>
					<text class="grid-text-mini">已安装</text>
				</u-grid-item>
				<u-grid-item :customStyle="{padding:'20rpx 0'}">
					<view>{{deviceInfo.device_num - deviceInfo.install_num}}</view>
					<text class="grid-text-mini">待安装</text>
				</u-grid-item>
				<u-grid-item :customStyle="{padding:'20rpx 0'}">
					<view>{{deviceInfo.team_device_num}}</view>
					<text class="grid-text-mini">团队</text>
				</u-grid-item>
			</u-grid>
		</view>
		<view style="height: 20rpx;"></view>
		<u-subsection :list="tabData.list" keyName="name" :current="tabData.current"
			@change="sectionChange"></u-subsection>
		<view style="height: 20rpx;"></view>
		<view v-if="!deviceInfo.device_num" style="padding: 30rpx;background-color: #ffffff;border-radius: 10rpx;">
			<u-empty mode="list"></u-empty>
		</view>
		<view v-else>
			<view v-if="tabData.current == 1">
				<navigator v-for="(item,index) in deviceItems" :key="index"
					style="background-color: #ffffff;color: #666666;padding: 20rpx;font-size: 28rpx; margin-bottom: 20rpx;border-radius: 12rpx;">
					<view class="u-flex u-flex-between">
						<view>设备ID：{{item.id}}</view>
						<u-tag v-if="item.install_time" text="已安装" plain size="mini" type="success"></u-tag>
						<u-tag v-else text="未安装" plain size="mini" type="error"></u-tag>
					</view>
					<view>生效时间：{{item.created_at}}</view>
				</navigator>
			</view>
			<view v-else>
				<navigator v-for="(item,index) in deviceItems" :key="index"
					style="background-color: #ffffff;color: #666666;padding: 20rpx;font-size: 28rpx; margin-bottom: 20rpx;border-radius: 12rpx;">
					<view class="u-flex u-flex-between">
						<view>设备号：{{item.device_no}}</view>
						<view>{{item.id}}</view>
						<!-- <u-tag v-if="item.install_time" text="已安装" plain size="mini" type="success"></u-tag>
						<u-tag v-else text="未安装" plain size="mini" type="error"></u-tag> -->
					</view>
					<view style="height: 10rpx;"></view>
					<view>电站：{{item.site_name}}</view>
					<view style="height: 10rpx;"></view>
					<view>地址：{{item.address}}</view>
					<view style="height: 10rpx;"></view>
					<view>生效时间：{{item.created_at}}</view>
				</navigator>
			</view>
			<u-loadmore :status="loadStatus" />
		</view>
		<view style="height: 400rpx;"></view>
	</view>
</template>

<script>
	let self;
	export default {
		data() {
			return {
				deviceItems: [],
				deviceInfo: {
					device_num: 0,
					install_num: 0,
					team_device_num: 0,
				},
				tabData: {
					list: [{
						"name": "已安装",
						"install_time": 1
					}, {
						"name": "待安装",
						"install_time": 0
					}, ],
					current: 0
				},
				status: [],
				type: [],
				loadStatus: 'loadmore',
				search: {
					install_time: 1,
					page: 1,
					status: 0,
				},
				page: {
					current: 0,
					last_page: 1,
					page: 1
				}
			}
		},
		onLoad() {
			self = this;
			self.getDeviceInfo();
			self.getDeviceItems();
		},
		onPullDownRefresh() {
			self.search.page = 1;
			self.getDeviceInfo();
			self.deviceItems = [];
			self.getDeviceItems();

			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if (!self.pageStatus()) {
				return;
			}
			self.loadStatus = 'loading';
			self.search.page = ++self.search.page;
			self.getDeviceItems();
		},
		methods: {
			sectionChange(index) {
				self.tabData.current = index;
				self.search.install_time = self.tabData.list[index].install_time;
				self.deviceItems = [];
				self.getDeviceItems();
			},
			pageStatus() {
				self.loadStatus = 'loadmore';
				if (self.search.page >= self.page.last_page) {
					self.loadStatus = 'nomore';
					return false;
				}
				return true;
			},

			getDeviceInfo() {
				self.$http.get('/shop/v10/device/info').then(({
					data
				}) => {
					self.deviceInfo = data;
				})
			},
			getDeviceItems() {
				self.$http.get('/shop/v10/device/items', self.search).then(({
					data,
					success
				}) => {

					self.deviceItems = self.deviceItems.concat(data.data);

					self.page.last_page = data.last_page;
					self.pageStatus();
				})
			},
		}
	}
</script>

<style lang="scss">

</style>