<script lang="ts" setup>
import { useUserStore } from "@/pinia";
import type { ToolButtonProp } from "./TableToolBarType";
defineProps({
  toolButtons: Array as PropType<ToolButtonProp[]>,
});
const userStore = useUserStore();

function checkButtonAuth(toolButton: ToolButtonProp) {
  if (toolButton.isAuth !== undefined && !toolButton.isAuth) return true;
  if (toolButton.authCode in userStore.buttonAuth) {
    return true;
  }
  return false;
}

function buttonClick(toolButtonProp: ToolButtonProp) {
  if (toolButtonProp.click === undefined) return;
  toolButtonProp.click();
}
</script>
<template>
  <div>
    <template v-for="toolButton of toolButtons || []" :key="toolButton.authCode">
      <el-button
        v-if="checkButtonAuth(toolButton)"
        @click="buttonClick(toolButton)"
        :type="toolButton.colorType || 'primary'"
      >
        {{ toolButton.text }}
      </el-button>
    </template>
  </div>
</template>
