

var initEvent = function(model, props){
	
	var init = 0;
var width = 0;
var w = 0;
var margin = 12;
var unit = 12;
var containerPad = 16;
var setHeightCount = 0;
var length = props.data.rowcount;
var firstExpenseCount = props.data.firstExpenseCount;
// 初始化：根据后端数量创建 div 并显示第一张图片
createCarouselItems(props.data.rowcount);	



// 动态创建 div
        function createCarouselItems(count) {
            const carouselInner = model.dom.querySelector('.kingdee-fi-tripslide-carousel-inner');
           carouselInner.innerHTML = ''; // 清空原有内容
		   const targetId = 'kingdeefitripslide';
			var element = model.dom;
		  //var element = model.dom.getElementById('kingdeefitripslide');
		  width = element.offsetWidth; // 例如：200（单位：px）
		  width = width - containerPad;
		  console.log("kingdeefitripslide: " + width);
		  w = width - unit - margin;
            for (let i = 0; i < count; i++) {
                const div = document.createElement('div');
                div.classList.add('kingdee-fi-tripslide-carousel-item');
                div.id = `kingdeefitripslide_div_${i}`; // 设置唯一的 id，如 div1, div2, div3
                //div.style.backgroundColor = getRandomColor(); // 随机背景颜色
               // div.textContent = i; // 设置文本内容
                carouselInner.appendChild(div);
            }

            // 初始化布局
            adjustLayout();
        }
		
		// 生成随机颜色
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }
		// 动态调整布局
        function adjustLayout() {
            const items = model.dom.querySelectorAll('.kingdee-fi-tripslide-carousel-item');
            if (items.length === 1) {
                // 如果只有一个 div，100% 显示
                items[0].style.flex = '0 0 ' + (w + unit + margin - containerPad) +  'px';
                items[0].style.marginRight = containerPad + 'px';
				items[0].style.marginLeft = containerPad + 'px';
				items[0].style.width = (w + unit + margin - containerPad) +  'px';
            } else {
                // 如果有多个 div，最后一个 div 不留右边距
                items.forEach((item, index) => {
					if (index === items.length - 1) {
                        item.style.flex = '0 0 ' + (w ) +  'px'; // 最后一个 div 占 90%
                        item.style.marginRight = containerPad + 'px';
						item.style.width = w +  'px'; 
                    }else if (index === 0) {
                        item.style.flex = '0 0 ' + w +  'px';
                        item.style.marginRight = margin +  'px' ;
                    }else {
						 item.style.flex = '0 0 ' + w  +  'px';
                         item.style.marginRight = margin +  'px' ;
					}
                });
            }
        }

        // 显示指定索引的图片
        function showSlide(index) {
			const carouselInner = model.dom.querySelector('.kingdee-fi-tripslide-carousel-inner');	
			carouselInner.style.transition = 'transform 0.5s ease-in-out, height 0.5s ease'; // 恢复过渡效果
			
            const items = model.dom.querySelectorAll('.kingdee-fi-tripslide-carousel-item');
            if (index < 0 || index >= items.length) {
                return; // 如果超出范围，直接返回
            }
            currentIndex = index;
            var offset =  0 ; // 每个 div 占 90% 宽度
			if(index > 0 && index < length -1){
				//offset = -currentIndex * (w + margin  ) + unit ;
				offset = -(currentIndex ) * (w +margin    )    ;
			}else if (index === length -1 && index > 0) {
				//offset = -currentIndex * (w  + margin ) + unit + margin;
				offset = -(currentIndex ) * (w +margin   )   +  (unit  + margin )  ;
			}
			//如果仅有一个行程，不左移
			if(index > 0 || items.length > 1){
				carouselInner.style.transform = `translateX(${offset}px)`;
			}
			
			var element = model.dom;
			if(index >= 0 && index < items.length - 1){
				element.style.paddingLeft = containerPad + 'px';
			}else if(index === items.length - 1){
				element.style.paddingLeft = 0 + 'px';
			}
			model.invoke('touchend', index);
			if(init === 0){
				setTimeout(() => {
								setHeight(0, 0);
							}, 500);
			}else {
				setHeight(index, index);
			}
			
			
			function setHeight(m, rank){
				var itemDiv = model.dom.querySelector("[id='kingdeefitripslide_div_" + m + "'");
				var expenseCount =  itemDiv.querySelectorAll("#cardentryflexpanelap").length;
				if((rank ===0  && firstExpenseCount === expenseCount) || rank > 0 || (rank ===0  && setHeightCount > 1)){						
					const carouselInner = model.dom.querySelector('.kingdee-fi-tripslide-carousel-inner');	
					const high = itemDiv.offsetHeight + 'px'; // 
					carouselInner.style.height=high;
					init = 1;
					console.log("currenIndex: " + high);
				}else if (rank ===0  && setHeightCount < 2){
					setHeightCount = setHeightCount + 1;
						setTimeout(() => {
								setHeight(m, rank);
							}, 1000);
				}
			}

			
        }
		
	

        // 触摸开始
        let currentIndex = 0;
        let startX = 0; // 触摸起始位置
        let isSwiping = false; // 是否正在滑动

        const carouselInner = model.dom.querySelector('.kingdee-fi-tripslide-carousel-inner');
        let items = model.dom.querySelectorAll('.kingdee-fi-tripslide-carousel-item');

        carouselInner.addEventListener('touchstart', (e) => {
			if (length < 2){
				return;
			}
            startX = e.touches[0].clientX; // 记录起始位置
            isSwiping = true;
        });

        // 触摸移动
        carouselInner.addEventListener('touchmove', (e) => {
			if (length < 2){
				return;
			}
            if (!isSwiping) return;
            const currentX = e.touches[0].clientX; // 当前触摸位置
            const diffX = startX - currentX; // 计算滑动距离s
            if (Math.abs(diffX) > 30) { // 避免误触
                e.preventDefault(); // 阻止默认行为（如页面滚动）
                carouselInner.style.transition = 'none'; // 禁用过渡效果
                const offset = -currentIndex * 90 - (diffX / window.innerWidth) * 90;
                carouselInner.style.transform = `translateX(${offset}%)`;
            }
        });

        // 触摸结束
        carouselInner.addEventListener('touchend', (e) => {
			if (length < 2){
				return;
			}
            if (!isSwiping) return;
            isSwiping = false;
            const endX = e.changedTouches[0].clientX; // 结束位置
            const diffX = startX - endX; // 计算滑动距离
            if (Math.abs(diffX) > 50) { // 滑动距离超过阈值
                if (diffX > 0 && currentIndex < items.length - 1) {
                    showSlide(currentIndex + 1); // 向左滑动，切换到下一张
                } else if (diffX < 0 && currentIndex > 0) {
                    showSlide(currentIndex - 1); // 向右滑动，切换到上一张
                } else {
                    // 滑动距离不足，回退到当前图片
                    showSlide(currentIndex);
                }
            } else {
                // 滑动距离不足，回退到当前图片
                showSlide(currentIndex);
            }
            
        });


        items = model.dom.querySelectorAll('.kingdee-fi-tripslide-carousel-item');// 重新获取 items
        showSlide(currentIndex);
}