<template>
	<view class="content">
		<view class="one">
			<uni-search-bar placeholder="搜索" bgColor="#EEEEEE" @confirm="toSearch" @input="input" />
		</view>
		<view class="two">
			<uni-grid :column="4" :show-border="false" :square="false">
				<uni-grid-item v-for="(item, index) in moduleList" :index="index" :key="index" @tap="change(item)">
					<view class="grid">
						<image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode="aspectFill">
						</image>
						<text class="text">{{ item.name }}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-view" @scroll="scroll">
			<view class="list-scroll-view">
				<view class="thr">
					<view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
						<image :src="item.file" class="image" mode="aspectFill" />
						<view class="name textOver">{{item.name}}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				moduleList: [],
				list: [{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					},
					{
						name: '联想（Lenovo）LT201黑色墨粉盒(适用S1801/LJ2205/M1851/M7206W/M7255F/F2081/LJ2206W/M7256WHF打印机)',
						type: '0',
						file: '/static/lenovo.jpeg',
						brief: '联想（Lenovo）LT201黑色墨粉盒',
					}
				],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 返回顶部
				scrollTop: 0,
			}
		},
		async onLoad() {
			const that = this;
			await that.searchToken();
			await that.search();
		},
		onShow() {

		},
		methods: {
			async searchToken() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async function(res) {
						// 替换底部菜单
						let aee = await that.$api(`/Role/menu`, 'GET', {
							is_use: '0',
							code: res.data.role
						});
						if (aee.errcode == '0') {
							if (aee.data.menu && aee.data.menu.length > 0) {
								for (let val of aee.data.menu) {
									uni.setTabBarItem({
										index: val.index,
										text: val.text,
										pagePath: val.pagePath,
										iconPath: val.iconPath[0].url,
										selectedIconPath: val.selectedIconPath[0].url,
									})
								}
							}
						}
					},
					fail: function(err) {
						uni.showToast({
							title: err.errmsg,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			async search() {
				const that = this;
				let res = await that.$api(`/Module`, 'GET', {
					is_use: '0'
				});
				if (res.errcode == '0') that.$set(that, `moduleList`, res.data);
			},
			async toSearch(res) {
				console.log(res);
			},
			async input(res) {
				console.log(res)
			},
			async change(e) {
				console.log(e);
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.searchMarket();
					uni.hideLoading();
				}
			},
			// 计算高度
			scroll: function(e) {
				const that = this;
				that.old_scrollTop = e.detail.scrollTop
			},
			clearPage() {
				const that = this;
				that.$set(that, `moduleList`, [])
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;

		.one {
			width: 100%;
		}

		.two {
			padding: 1vw 2vw;
			background-color: var(--f8Color);

			.grid {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 5px 0 0 0;
				margin: 5px;
				background-color: var(--mainColor);
				border-radius: 10px;

				.image {
					width: 25px;
					height: 25px;
				}

				.text {
					font-size: var(--font14Size);
					margin-top: 5px;
				}
			}
		}

		.thr {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			padding: 2vw;
			background-color: var(--f8Color);

			.list {
				position: relative;
				width: 43vw;
				padding: 2vw;
				margin: 0 0 2vw 0;
				border-radius: 10px;
				background-color: var(--mainColor);

				.image {
					width: 100%;
					height: 40vw;
					border-top-right-radius: 10px;
					border-top-left-radius: 10px;
				}

				.name {
					font-size: var(--font14Size);
				}
			}
		}
	}

	.scroll-view {
		// height: 100vw;
	}
</style>
