<template>
  <Tooltip placement="top">
    <template #title>
      <span>{{ t('密度') }}</span>
    </template>

    <Dropdown placement="bottom" :trigger="['click']" :getPopupContainer="getPopupContainer">
      <IconFontSymbol
        icon="gaodu-01"
        fontSize="16"
        fillColor="#C3C8CF"
        class="table-setting-icon"
      />
      <template #overlay>
        <a-menu @click="handleTitleClick" selectable v-model:selectedKeys="selectedKeysRef">
          <MenuItem key="default">
            <span>{{ t('默认') }}</span>
          </MenuItem>
          <MenuItem key="middle">
            <span>{{ t('中等') }}</span>
          </MenuItem>
          <MenuItem key="small">
            <span>{{ t('紧凑') }}</span>
          </MenuItem>
        </a-menu>
      </template>
    </Dropdown>
  </Tooltip>
</template>
<script lang="ts">
  import type { SizeType } from '../../types/table';
  import { defineComponent, ref } from 'vue';
  import { Tooltip, Dropdown, Menu } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useTableContext } from '../../hooks/useTableContext';
  import { getPopupContainer } from '/@/utils';
  import IconFontSymbol from '/@/components/IconFontSymbol/Index.vue';

  export default defineComponent({
    name: 'SizeSetting',
    components: {
      Tooltip,
      Dropdown,
      MenuItem: Menu.Item,
      IconFontSymbol,
    },
    setup() {
      const table = useTableContext();
      const { t } = useI18n();

      const selectedKeysRef = ref<SizeType[]>([table.getSize()]);

      function handleTitleClick({ key }: { key: SizeType }) {
        selectedKeysRef.value = [key];
        table.setProps({
          size: key,
        });
      }

      return {
        handleTitleClick,
        selectedKeysRef,
        getPopupContainer,
        t,
      };
    },
  });
</script>
