<template>
	<view class="atlas">
		<uni-search-bar class="uni-mt-10" placeholder="请输入标签名" clearButton="auto" cancelButton="none"
			@confirm="search" />
		<view class="title">
			<uni-segmented-control :current="current" :values="arrList" @clickItem="onClickItem" styleType="text"
				:activeColor="primaryColor"></uni-segmented-control>
		</view>
		<swiper class="swiper" :circular="false" :current="current" :indicator-dots="false" :autoplay="false"
			@change="changeItem" :duration="500"
			:style="'height:'+ imgBoxHeight + 'px'">
      <!-- imgBoxHeight * Math.ceil(imgList.length / 2)     -->
			<swiper-item class="swiper-item" v-for="(item,index) in arrList" :key="index">
				<view class="imgBox" v-for="(val,i) in imgList" :key="i" @click="targetImgList(val)"
              :style="'height:' + imgHeight + 'px;width:'+ imgWidth + 'px'">
					<image :src="val.img" mode="aspectFill"
                 :style="'height:' + imgHeight + 'px;width:'+ imgWidth + 'px'"
                 ></image>
					<view class="target-name">{{val.signName}}</view>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		listType,
		signImgList,
		querySignImg,
		querySignImgBySignName
	} from '@/api/atlas.js'
	export default {
		data() {
			return {
			  primaryColor: "#1a94bc",
				imgWidth: 0,
				imgHeight: 0,
				imgBoxHeight: 0,
				current: 0,
				pageNum: 1,
				pageSize: 16,
				arrList: [],
				arrListId: [],
				imgList: []
			}
		},
		async created() {
			await uni.getSystemInfo({
				success: res => {
					this.imgWidth = (res.screenWidth - 64) / 2
					this.imgHeight = ((res.screenWidth - 64) / 2 - (res.screenWidth - 64) / 4)
					this.imgBoxHeight = res.screenHeight - 152
				}
			})
			if (uni.getStorageSync('atlas_current')) {
				this.current = await uni.getStorageSync('atlas_current')
			}
			await this.getListType()
			await this.getSignImgList(this.current)
		},
		watch: {
			current(n, o) {
				this.imgList = []
				this.getSignImgList(this.current)
				immediate: true
			}
		},
		// 上划加载更多
		onReachBottom() {
			if (this.imgList.length > 1) {
				this.pageNum += 1
				this.getSignImgList(this.current)
			}
		},
		methods: {
			search(res) {
				if (res.value) {
					console.log('res.value',res.value);
					const params = {
						pageNum: 1,
						pageSize: 10,
						signName: res.value
					}
					querySignImgBySignName(params).then(response =>{
						console.log('response',response);
						if (response.data.code != 200) {
							uni.showToast({
								title: response.data.msg,
								icon: 'none',
							})
						}else{
							uni.setStorage({
								key: 'atlas_detailId',
								data: {
									signName: res.value,
									type:'search'
								},
								success() {
									uni.navigateTo({
										url: './atlasList'
									})
								}
							})
						}
					});
				} else {
					uni.showToast({
						title: '请输入要搜索的标签名',
						icon: 'none',
					})
				}
			},
			// 获取分类
			async getListType() {
				this.arrList = []
				this.arrListId = []
				const res = await listType()
				if (res.data.code === 200) {
					res.data.data.forEach(item => {
						this.arrList.push(item.typeName)
						this.arrListId.push(item.id)
					})
				} else {
					uni.showModal({
						content: res.data.msg,
						showCancel: false
					});
				}
			},
			// 获取分类下标签
			async getSignImgList(val) {
				uni.showLoading({
					title: '加载中'
				});
				const params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					typeId: this.arrListId[val]
				}
				const res = await signImgList(params)
				if (res.data.code === 200) {
					this.$nextTick(() => {
						this.imgList.push(...res.data.rows)
					})
					this.$forceUpdate()
				} else {
					uni.showModal({
						content: res.data.msg,
						showCancel: false
					});
				}
				uni.hideLoading()
			},
			onClickItem(val) {
				this.imgList = []
				this.pageNum = 1
				this.pageSize = 10
				this.current = val.currentIndex
			},
			changeItem(val) {
				console.log(val)
				this.pageNum = 1
				this.pageSize = 10
				this.imgList = []
				this.current = val.detail.current
			},
			// 前往标签详情页
			targetImgList(val) {
				uni.setStorage({
					key: 'atlas_current',
					data: this.current,
				})
				uni.setStorage({
					key: 'atlas_detailId',
					data: {
						signId: val.signId,
						typeId: val.typeId,
						signName: val.signName,
						type:'click'
					},
					success() {
						uni.navigateTo({
							url: './atlasList'
						})
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	.atlas {
		.uni-searchbar {
			border: 1px solid $uni-primary;
			border-radius: 16rpx;
			padding: 0;
			margin-left: 40rpx;
			margin-right: 40rpx;

			.uni-searchbar__box {
				padding: 0;
				border-radius: 16rpx !important;
			}
		}

    .title {
      margin: 10px 0;

      ::v-deep .segmented-control {
        border-radius: 8rpx 8rpx 0 0;
        background-color: rgba(17, 168, 253, 0.1);
      }

      ::v-deep .segmented-control__item {

        view {
          display: flex;
          align-items: center;
        }

      }

    }

		.swiper {
			overflow-y: auto;

			.swiper-item {
        display: flex;
        justify-content: space-between;
        width: calc(100% - 44px) !important;
        flex-wrap: wrap;
        padding: 0 22px;
        align-content: flex-start;

				.imgBox {
					text-align: center;
					margin-bottom: 20px;
          position: relative;

					image {
						border-radius: 10px;
					}

          .target-name {
						font-size: 12px;
            position: absolute;
            left: 10px;
            bottom: 10px;
            color: $uni-white;
					}
				}
			}
		}
	}
</style>
