<template>
	<view class="page" :style="themeColor">
		<view class="flex benben-position-layout flex flex-wrap align-center galleryPage_flex_0"
			:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class='flex flex-wrap align-center justify-between flex-sub galleryPage_fd0_0'>
				<view class='flex flex-wrap align-center galleryPage_fd0_0_c0' @tap.stop="handleJumpDiy"
					data-type="back" data-url="1">
					<text class='fu-iconfont2  galleryPage_fd0_0_c0_c0'>&#xE794;</text>
				</view>
				<view class='flex flex-wrap align-center'>
					<text class='galleryPage_fd0_0_c1_c0'>图库</text>
				</view>
				<view class='flex flex-wrap align-center justify-end galleryPage_fd0_0_c2' @tap.stop="sureTap">
					<text class='galleryPage_fd0_0_c2_c0'>完成</text>
				</view>
			</view>

		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
		<!---flex布局flex布局开始-->
		<view class="flex benben-flex-layout flex-wrap align-center galleryPage_flex_1">
			<view class='flex flex-wrap align-center flex-sub galleryPage_fd1_0'>
				<text class='fu-iconfont2  galleryPage_fd1_0_c0'>&#xe741;</text>
				<input class='flex-sub galleryPage_fd1_0_c1' type="text" placeholder="请输入关键词进行搜索" @confirm="searchTap()" confirm-type="done"
					:maxlength="-1" placeholder-style="color:rgba(191, 191, 191, 1);font-size:26rpx" v-model="name" />
			</view>
		</view>
		<view class="item">
			<view class="flex flex-direction align-center list" v-for="(item,index) in listData" :key="index">
				<image class="shop-pic" :src="item.image" mode="aspectFill"></image>
				<view>{{item.name}}</view>
				<image @tap="checkTap(index)" class="check-img"
					:src='item.checked?STATIC_URL+"44.png":STATIC_URL+"117.png"'></image>
			</view>
		</view>
		<fu-empty-ui v-if="listData.length==0"></fu-empty-ui>
		<!---flex布局flex布局结束-->


	</view>
</template>
<script>
	export default {
		components: {},


		data() {
			return {
				page: 1, // 分页
				allowLoadMore: true, // 允许加载更多
				loadedAll: false, // 已加载全部数据
				noListData: false, // 没有列表数据
				listData: [], //订单列表 数组
				name: '', //搜索关键字
				nameimg: '', //上个页面带入，是哪个图片上传
				count: '' //上个页面带入，最多选择记账
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		onLoad(options) {
			this.count = options.count
			this.nameimg = options.name
			this.getImages()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			this.allowLoadMore = true;
			this.loadedAll = false;
			this.noListData = false;
			this.listData = [];
			this.getImages(() => {
				uni.stopPullDownRefresh();
			});
		},
		//上拉加载更多
		onReachBottom(e) {
			this.getImages();
		},
		methods: {
			sureTap() {
				let character = [];
				let aid = [];
				this.listData.forEach(item => {
					if (item.checked) {
						character.push(item.image)
						aid.push(item.aid)
					}
				})
				if (character.length == 0) return this.$message.info('请选择图片');
				// character.forEach(item => {
				// 	item.path = item.image
				// })
				uni.$emit("ADDIMG", {
					image: JSON.stringify(character),
					aid:aid,
					type: this.nameimg
				})
				setTimeout(function() {
					uni.navigateBack({})
				}, 300)
			},
			searchTap(e){
				console.log(this.name)
				this.page = 1;
				this.allowLoadMore = true;
				this.loadedAll = false;
				this.noListData = false;
				this.listData = [];
				this.getImages()
			},
			//获取图库
			getImages(fn) {
				if (!this.allowLoadMore || this.loadedAll || this.noListData) return;
				this.allowLoadMore = false;
				this.$api.post(global.apiUrls.post6488278581ec3, {
					page: this.page,
					name: this.name
				}).then(res => {
					if (res.data.code == 1) {
						let tempLists = res.data.data.data,
							totalPage = res.data.data.last_page,
							listData = this.listData;

						this.listData.push(...tempLists);
						this.listData.forEach(item => {
							item.checked = false
						})
						console.log(this.listData)
						this.allowLoadMore = true;
						if (this.page >= totalPage) this.loadedAll = true;
						if (this.page == 1 && this.listData.length == 0) this.noListData = true;
						this.page += 1;
					} else {
						this.allowLoadMore = true;
					}
					if (fn) fn();
				})
			},
			
			//选择
			checkTap(index) {
				let list = this.listData;
				this.listData[index].checked = !this.listData[index].checked;
				let length = (this.listData.map((item, index) => (item.checked ? `${item.name}` : '')).filter(item =>
					item != '')).length;
				console.log(length)
				if (length <= this.count) {} else {
					this.listData[index].checked = false;
				}
				list = list.map((item) => {
					item.checked = item.checked;
					return item;
				})
				this.listData = list
				console.log(this.listData)
			},
		}
	};
</script>
<style lang="scss" scoped>
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: rgba(248, 248, 248, 1);
		background-size: 100% auto !important;
	}
	.item{
		display: flex;
		flex-wrap: wrap;
		padding: 0 16rpx 0 32rpx;
		.list{
			margin-right: 16rpx;
			margin-bottom: 24rpx;
			position: relative;
		}
		.shop-pic{
			width: 218rpx;
			height: 218rpx;
			margin-bottom: 16rpx;
		}
		.check-img{
			width: 32rpx;
			height: 32rpx;
			position: absolute;
			top: 18rpx;
			right: 16rpx;
		}
		view{
			width: 218rpx;
		}
	}

	.galleryPage_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.galleryPage_fd0_0_c2_c0 {
		line-height: 35rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: rgba(255, 147, 0, 1);
	}

	.galleryPage_fd0_0_c2 {
		width: 150rpx;
	}

	.galleryPage_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333;
		line-height: 88rpx;
	}

	.galleryPage_fd0_0_c0_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333;
		line-height: 36rpx;
	}

	.galleryPage_fd0_0_c0 {
		width: 150rpx;
	}

	.galleryPage_fd0_0 {
		margin: 0rpx 32rpx 0rpx 32rpx;
	}

	.galleryPage_flex_1 {
		background: transparent;
		padding: 24rpx 24rpx 0rpx 24rpx;
	}

	.galleryPage_fd1_0_c1 {
		margin: 0rpx 0rpx 0rpx 20rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: var(--benbenFontColor0);
	}

	.galleryPage_fd1_0_c0 {
		font-size: 28rpx;
		color: rgba(153, 153, 153, 1);
	}

	.galleryPage_fd1_0 {
		background: rgba(255, 255, 255, 1);
		padding: 18rpx 24rpx 18rpx 24rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		background-size: 100% auto !important;
	}
</style>
