
// 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: {
		currentHasUserInfo: {
			type: Boolean,
			type: false
		},
		imageData: {
			type: Array,
			value: [],
		},
	},
	attached() {
		this.setData({
			baseImgUrl: app.globalData.imageUrl
		})
		const { imageData } = this.data;
		let currentTab = imageData
		currentTab.forEach(item => {
			// 存在 row 处理数据 8 商品 9 分类
			let newRow = null
			if (item.link) {
				if (item.link.type == 2) {
					// 父组件 更新
					const urls = getCurrentPages()
					const currentPath = urls[0]
					const pageId = item.link.url
					item.link.newPath = `/${currentPath.route}`
				}
				return
			} else if (item.row) {
				newRow = JSON.parse(item.row)
				if (newRow.type == 8) {
					newRow.newPath = '/pages/productDetail/productDetail'
				} else if (newRow.type == 9) {
					newRow.newPath = '/subPackage/page/pages/categoryPro/categoryPro'
				}
				item.row = newRow
			}
		})

		// 当前匹配路径是否需要授权 true 需要 false 不需要
		currentTab.forEach(item => {
			let checkUrl = ''
			if (item.link) {
				checkUrl = item.link.newPath ? item.link.newPath : item.link.url
			} else if (item.row) {
				checkUrl = item.row.newPath ? item.row.newPath : item.row.url
			}

			wxService.NoLoginPage(checkUrl).then(res => {
				 if(checkUrl)  item.needAuth = res.needAuth
				 this.setData({ imageList: currentTab })
			})
		})

	},
	/**
	 * 组件的初始数据
	 */
	data: {
		indicatorDots: true,
		autoplay: true,
		circular: true,
		interval: 5000,
		duration: 1000,
		isAuthorization: false, // false 不显示 true 显示
		baseImgUrl: '',
		maxHeight: 180,
		padding: 0,
		heights: [],
		imageHeights: {}, // 记录所有图片的实际高度
		imageList: []
	},
	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: {
		_getUserInfo(res = {}) {
			this.triggerEvent('getAuth', res);
		},
		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
			});
      //如果是在企业微信中 由于不支持observers属性方法，因此需要手动调用
      const currentEnv = wx.getStorageSync('_qyWeChat');
      if (currentEnv){
        this.calMaxHeight(heights);
      }
      
		},
    //计算最大高度
    calMaxHeight(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 }
          )
        })
      }
    },
    //预览
		preview(event) {
			utilLink.currentLinkRouter(event, this)
		}
	}
})
