<template>
  <div class="card" :style="props.style">
    <template v-if="cardLoading">
      <a-spin class="card-loading" :spinning="cardLoading"></a-spin>
    </template>

    <template v-else>
      <!-- 卡片头部 -->
      <div class="card-header" v-if="slots.header">
        <slot name="header"></slot>
      </div>

      <!--卡片内容-->
      <div class="card-body not-padding-tab" v-if="!isTabSlot">
        <a-tabs v-model:activeKey="tabActiveKey" v-if="props.isShowTab" @change="tabChange">
          <a-tab-pane :key="tabItem.key" :tab="tabItem.name" v-for="tabItem in props.tabList"> </a-tab-pane>
          <template #renderTabBar="{ DefaultTabBar, ...tabProps }">
            <component :is="DefaultTabBar" v-bind="tabProps" :style="props.tabBarStyle ? props.tabBarStyle : {}" />
          </template>
        </a-tabs>
        <slot name="body" v-if="slots.body"></slot>
      </div>
      <div class="card-body not-padding-tab" v-else>
        <a-tabs v-model:activeKey="tabActiveKey" v-if="props.isShowTab" @change="tabChange">
          <a-tab-pane :key="tabItem.key" v-for="tabItem in props.tabList">
            <template #tab>
              <slot name="tab" :scope="tabItem" v-bind="tabItem || {}"></slot>
            </template>
          </a-tab-pane>
          <template #renderTabBar="{ DefaultTabBar, ...tabProps }">
            <component :is="DefaultTabBar" v-bind="tabProps" :style="props.tabBarStyle ? props.tabBarStyle : {}" />
          </template>
        </a-tabs>
        <slot name="body" v-if="slots.body"></slot>
      </div>
    </template>
  </div>
</template>

<script setup>
import { useSlots, ref, onUpdated } from 'vue';

const slots = useSlots();

const props = defineProps({
  // tab栏默认选中
  activeKey: {
    type: [String, Number],
    default: ''
  },
  // 是否显示tab栏
  isShowTab: {
    type: Boolean,
    default: false
  },
  // tab栏列表
  tabList: {
    type: Array,
    default: () => {
      return [];
    }
  },
  // tabBar样式
  tabBarStyle: {
    type: Object,
    default: () => {
      return {};
    }
  },
  // 是否可以使用tab栏插槽
  isTabSlot: {
    type: Boolean,
    default: false
  },
  loading: {
    type: Boolean,
    default: false
  },
  style: {
    type: Object,
    default: {}
  }
});

const emits = defineEmits(['tabChange']);

const tabActiveKey = ref(props.activeKey);

const cardLoading = ref(props.loading);

// tab切换
const tabChange = key => {
  emits('tabChange', key);
};

onUpdated(() => {
  tabActiveKey.value = props.activeKey;
});
</script>

<style scoped lang="less">
.card {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 12px 24px;
  box-sizing: border-box;
  overflow-y: auto;
  overflow-x: hidden;
  background-color: #ffffff;
  color: #383838;
}

.card-header {
  position: relative;
  width: 100%;
  height: 40px;
  font-size: 18px;
  color: #006cb9;
}

.card-body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.card-loading {
  position: absolute;
  top: 50%;
  left: 50%;
}
</style>
