
// component/imageSwiper/imageSwiper.js
var app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const utilLink = require('../../utils/currentLinkRouter')
const envInfo = require('../../config/index').envInfo

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		imageData: {
			type: Array,
			value: [],
		},
	},
	attached() {
		this.setData({
			baseImgUrl: app.globalData.imageUrl
		})
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		indicatorDots: true,
		autoplay: true,
		circular: true,
		interval: 5000,
		duration: 1000,
		isAuthorization: false, // false 不显示 true 显示
		baseImgUrl: '',
		maxHeight: 0,
		padding: 0,
		heights: [],
		imageHeights: {}, // 记录所有图片的实际高度
	},
	observers: {
		heights: function (heights) {
			let imageData = this.data.imageData
			if (heights.length === imageData.length) {
				let maxHeight = Math.max.apply(null, heights)
				this.setData({
					maxHeight,
				})
				wxService.nextTick(() => {
					this.triggerEvent(
						'imageLoad',
						{ maxHeight, windowWidth: this.data.windowWidth },
						{ bubbles: true, composed: true }
					)
				})
			}
		},
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		imageLoad: function (e) {
			// 图片加载完成回调
			let heights = this.data.heights
			let id = e.target.id
			var imageWidth = e.detail.width
			var imageHeight = e.detail.height
			var ratio = imageWidth / imageHeight
			var sysRes = app.globalData.systemInfo
			var imageHeights = this.data.imageHeights
			const height = (sysRes.windowWidth - (this.data.padding * sysRes.windowWidth) / 750) / ratio
			heights.push(height)
			imageHeights[id] = height
			this.setData({
				imageHeights: imageHeights,
				windowWidth: sysRes.windowWidth,
				heights
			})
		},
		preview(event) {
			utilLink.currentLinkRouter(event)
		}
	}
})
