<!-- 装修基础组件：宫格导航 -->
<template>
  <uni-grid :showBorder="Boolean(data.border)" :column="data.column" v-if="initData && initData.length > 0">

	<template  v-for="(item, index) in initData">
		<uni-grid-item
		  :key="index"
		  @tap="sheep.$router.go(item.url)"
		  v-if="item.isVisible"
		>
			
		  <view class="grid-item-box ss-flex ss-flex-col ss-row-center ss-col-center">
		    <view class="img-box">
		      <view
		        class="tag-box"
		        v-if="item.badge.show"
		        :style="[{ background: item.badge.bgColor, color: item.badge.textColor }]"
		      >
		        {{ item.badge.text }}
		      </view>
		      <image class="menu-image" :src="sheep.$url.cdn(item.iconUrl)"></image>
		    </view>
		
		    <view class="title-box ss-flex ss-flex-col ss-row-center ss-col-center">
		      <view class="grid-text" :style="[{ color: item.titleColor }]">
		        {{ item.title }}
		      </view>
		      <view class="grid-tip" :style="[{ color: item.subtitleColor }]">
		        {{ item.subtitle }}
		      </view>
		    </view>
		  </view>
		</uni-grid-item>
	</template>
    
  </uni-grid>
</template>

<script setup>
  import sheep from '@/sheep';
  import UserApi from '@/sheep/api/member/user';
  import { ref, watch, onMounted } from 'vue';
  const props = defineProps({
    data: {
      type: Object,
      default() {},
    },
  });
  
  // 使用 ref 来存储初始化后的数据
  const initData = ref([]);
  
  watch(
    () => props.data.list,
    (newList) => {
      if (Array.isArray(newList) && newList.length > 0) {
        initData.value = newList.map(item => ({
			...item,
			isVisible: item.title === '商户入驻' ? true : (item.isVisible !== undefined ? item.isVisible : true),

        }));
      } else {
        initData.value = [];
      }
    },
    { immediate: true }
  );
  
  // 在组件挂载后获取用户信息并更新 initData
  onMounted(() => {
	  try {	
		UserApi.getUserInfo()
		  .then((res) => {
			const userInfo = res.data;
	  
			// 检查用户信息后修改 initData 的 isVisible 属性
			if (userInfo?.agent) {
			  const targetIndex = initData.value.findIndex(
				  (item) => item.title === '商户入驻'
			  );
			  if (targetIndex !== -1) {
				// 使用 splice 替换对象，确保响应性
				const updatedItem = {
				  ...initData.value[targetIndex],
				  isVisible: true,
				};
				initData.value.splice(targetIndex, 1, updatedItem);
			  }
			}
		  })
		  .catch((error) => {
			console.error('获取用户信息时发生错误:', error);
		  });
	  } catch (error) {
			console.log(error);
	  }
	  // console.log(initData.value);
  });
  
</script>

<style lang="scss" scoped>
  .menu-image {
    width: 24px;
    height: 24px;
  }
  .grid-item-box {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    .img-box {
      position: relative;
      .tag-box {
        position: absolute;
        z-index: 2;
        top: 0;
        right: 0;
        font-size: 2em;
        line-height: 1;
        padding: 0.4em 0.6em 0.3em;
        transform: scale(0.4) translateX(0.5em) translatey(-0.6em);
        transform-origin: 100% 0;
        border-radius: 200rpx;
        white-space: nowrap;
      }
    }

    .title-box {
      .grid-tip {
        font-size: 24rpx;
        white-space: nowrap;
        text-align: center;
      }
    }
  }
</style>
