<template>
	<!-- 设备 -->
	<view class="work-container">
		<view class="cartNull" v-show="!token">
			还没有登录，请<navigator open-type="navigate" url="/pages/login">先登录</navigator>
		</view>
		<view class="" v-show='token'>
			<Search @search='search' @screen='screen' :isShowScreen='false' />
			<view class="list">
				<view class="item" v-for="(item,index) in list" :key='index'>
					<u-image :src="item.images" width='160' height="160">
						<template v-slot:error>
							<view class="image-error">
								<u-icon name="photo" color="#c7c7c7" size="38"></u-icon>
								<view style="font-size: 24rpx;">暂无图片</view>
							</view>
						</template>
					</u-image>
					<view class="text">
						<view class="title">
							{{item.name}}
						</view>
						<view class="dec1">
							{{item.number}}
						</view>
						<view class="dec2">
							{{item.factoryAreaName}}
						</view>
					</view>
				</view>
			</view>
			<view style="height: 94rpx;padding-top: 30rpx;">
				<u-loadmore :status="status" v-if="status != 'loadmore'" />
			</view>
		</view>

		<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
	</view>
</template>

<script setup lang="ts">
	import {
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		getAccessToken
	} from '@/utils/auth'
	import * as deviceApi from "@/api/device.js"
	import Search from '../../components/search/index.vue'
	const { proxy } = getCurrentInstance()
	const params = ref({
		pageNo: 1,
		pageSize: 10,
		type: '',
		name: ''
	})
	const status = ref('loadmore') //是否显示没有更多了
	const list = ref([])
	const token = ref('')

	// 搜索
	function search(keyWord) {
		params.value.name = keyWord
		params.value.pageNo = 1
		list.value = []
		status.value = 'loadmore'
		getList()
	}
	function screen() {
		proxy.$tab.navigateTo(`/pages/device/screen`)
	}
	function getList() {
		if (status.value == 'nomore') return;
		status.value = 'loading';
		proxy.$modal.loading('加载中')
		console.log('params.value',params.value)
		deviceApi.devicePage(params.value).then((res) => {
			proxy.$modal.closeLoading()
			if (res.data.list.length > 0) {
				list.value = list.value.concat(res.data.list);
				params.value.pageNo++;
				status.value = 'loadmore'
			} else {
				status.value = 'nomore'
			}
		}).catch(() => { })
	}
	onShow(() => {
		if (getAccessToken()) {
			token.value = getAccessToken()
			params.value.pageNo = 1
			list.value = []
			status.value = 'loadmore'
			getList()
		}
	})
	onReachBottom(() => {
		getList()
	})
	// 	onReachBottom() {
	// 		this.getList()
	// 	}
	// export default {
	// 	data() {
	// 		return {
	// 			params: {
	// 				pageNo: 1,
	// 				pageSize: 10,
	// 				type: '',
	// 				name:''
	// 			},
	// 			status: 'loadmore', //是否显示没有更多了
	// 			list: [],
	// 			token:''
	// 		}
	// 	},
	// 	methods: {
	// 		// 搜索
	// 		search(keyWord) {
	// 			this.params.name = keyWord
	// 			this.params.pageNo = 1
	// 			this.list = []
	// 			this.status = 'loadmore'
	// 			this.getList()
	// 		},
	// 		// 筛选
	// 		screen() {
	// 			this.$tab.navigateTo(`/pages/device/screen`)
	// 		},
	// 		// 获取设备保修列表
	// 		getList() {
	// 			if (this.status == 'nomore') return;
	// 			this.status = 'loading';
	// 			this.$modal.loading('加载中')
	// 			deviceApi.devicePage(this.params).then((res) => {
	// 				this.$modal.closeLoading()
	// 				if (res.data.list.length > 0) {
	// 					this.list = this.list.concat(res.data.list);
	// 					this.params.pageNo++;
	// 					this.status = 'loadmore'
	// 				} else {
	// 					this.status = 'nomore'
	// 				}
	// 			}).catch(()=>{})
	// 		},
	// 	},
	// 	onShow() {
	// 		if (getAccessToken()) {
	// 			this.token = getAccessToken()
	// 			this.params.pageNo = 1
	// 			this.list = []
	// 			this.status = 'loadmore'
	// 			this.getList()
	// 		}
 	// },
	// 	onReachBottom() {
	// 		this.getList()
	// 	}
	// }
</script>

<style lang="scss" scoped>
	.work-container{
		min-height: 100vh;
		background: #f5f5f5;
	}
	.list {
		padding-bottom: 20rpx;

		.item {
			margin-top: 20rpx;
			background: white;
			padding: 30rpx;
			display: flex;
			align-items: center;

			.text {
				margin-left: 20rpx;

				.title {
					font-size: 32rpx;
					font-weight: bold;
				}

				.dec1 {
					font-size: 28rpx;
					margin-top: 16rpx;
					color: #acacac;
				}

				.dec2 {
					font-size: 28rpx;
					margin-top: 6rpx;
					color: #acacac;
				}
			}
		}
	}

	.image-error {
		text-align: center;
	}

	.cartNull {
		text-align: center;
		padding: 500rpx 40rpx 0;
		font-size: 28rpx;
		color: #888;
	}

	.cartNull navigator {
		color: #2979ff;
	}
</style>