<template>
  <view class="tabs_container_body">
    <view class="tabs_container">
      <view
        v-for="(i, index) in tabsList"
        :key="`tab_${index}`"
        :class="`tab_item ${index === current ? 'tab_active' : ''}`"
        @click="handleTabClick(index)"
      >
        <view class="label">
          {{ i?.label }}
        </view>
        <view class="bottom_line" :style="getStyleBgInfo()" v-if="index === current"> </view>
      </view>
    </view>
    <cc-scroll-loading :meta="metaData" @load="handleLoadMore">
      <MyEventList v-if="current === 0" :data="convenienceServices" />
      <MyConvenienceServices
        v-if="current === 1"
        :data="convenienceServices"
        @change="getConvenienceServices"
      />
    </cc-scroll-loading>
  </view>
</template>
<script setup lang="ts">
import { getCurrentInstance, onMounted, ref, watch } from 'vue'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
import MyConvenienceServices from './ConvenienceServices.vue'
import MyEventList from './EventList.vue'
import { useWeAppAuthStore } from '@/common'
import { getStyleBgInfo } from '@/common/libraries/getPageConfig'
import getUserLogin from '@/common/libraries/userUserLogin'
const loadingStatus = ref(false)
const formData = ref({ page: 1 })
const metaData = ref({})
const auth = useWeAppAuthStore()
const tabsList = [{ label: '社区活动' }, { label: '便民服务' }]
const current = ref(0)
const convenienceServices = ref<any>([])
const handleTabClick = (index: number) => {
  current.value = index
  convenienceServices.value = []
  if (current.value) {
    getConvenienceServices('PropertyExclusive')
  } else {
    getEventList()
  }
}

const getEventList = () => {
  //获取活动
  getApiLoading(Apis.Activity.Activities.List, {
    ...formData.value,
    asset_projects_id: auth?.data?.selected_house?.asset_projects_id || -1,
  }).then(res => {
    loadingStatus.value = true
    convenienceServices.value = [...convenienceServices.value, ...res.data || []]
    metaData.value = res.meta
    console.log('res', res)
  })
}
const getConvenienceServices = (type: string) => {
  //获取便民服务
  getApiLoading(Apis.Common.ConvenienceServices.List, {
    asset_projects_id: auth?.data?.selected_house?.asset_projects_id || -1,
    type: type
  }).then(res => {
    loadingStatus.value = true
    convenienceServices.value = res.data
    metaData.value = res.meta
    console.log(res)
  })
}
const handleLoadMore = (page: number) => {
  formData.value.page = page
  handleTabClick(current.value)
}

watch(
  () => auth?.data?.selected_house,
  () => {
    if (getUserLogin?.getLoginStatus() && loadingStatus.value) {
      handleTabClick(current.value)
    }
  },
  {
    deep: true
  }
)


onMounted(async () => {
  await getCurrentInstance()?.appContext.config.globalProperties.$onLaunched
  getEventList()
})
</script>
<style lang="scss" scoped>
.tabs_container {
  display: flex;
  align-items: flex-start;
  font-size: 28rpx;
  font-weight: 500;
  color: #666;
  text-align: center;
  .tab_item {
    margin-left: 30rpx;
  }
  .tab_active {
    color: #3d3d3d;
    font-size: 32rpx;

    .bottom_line {
      background-color: #2a7efb;
      width: 60rpx;
      height: 4px;
      margin: 0 auto;
      border-radius: 10rpx;
      margin-top: 10rpx;
    }
  }
}
</style>
