<template>
	<u-swiper bg-color="transparent" 
	:mode="list && list.length > 1 ?'round': 'none'"
	 :list="list" :height="height"
		@click="openSwiper">
	</u-swiper>
</template>

<script>
	import {
		getBannerList
	} from "@/api/government.js"
	export default {
		name: "zw-swiper",
		props: {
			autoLoad: {
				type: Boolean,
				default: true
			},
			height: {
				type: Number,
				default: 200
			},
			StorageKey: {
				type: String,
				default: ""
			},
			keyword: {
				type: String,
				default: ""
			},
		},
		data() {
			return {
				list: []
			};
		},
		created() {

		},
		methods: {
			init() {
				this.getBanner(this.keyword)
			},
			// 获取 banner
			getBanner(keyword) {
				if (!keyword) return
				getBannerList(keyword).then(res => {
					const data = res.data || []
					this.list = data.map(el => {
						el.image = this.$getImgPath(el.image)
						return el
					});
					// this.list.map(item => {
					//   item.image = this.$getImgPath(item.image)
					// })
					uni.setStorage({
						key: this.StorageKey,
						data: this.list,
						success: function() {
							console.log('success');
						}
					});
					console.log('')

				});
			},

			// 点击轮播图
			openSwiper(e) {
				this.$openPage(this.list[e])
			},
		},
		watch: {
			keyword: {
				handler: function(newKwd, oldKod) {
					if (oldKod === undefined) {
						uni.getStorage({
							key: this.StorageKey,
							success: res => {
								this.list = res.data || []
							}
						});
					}
					if (this.autoLoad && newKwd && newKwd !== oldKod) this.getBanner(newKwd)
				},
				immediate: true,
				deep: true
			}
		}
	}
</script>

<style>

</style>