<script>
import { mapGetters } from 'vuex';
import Favorite from '@shell/components/nav/Favorite';
import TypeDescription from '@shell/components/TypeDescription';
import { get } from '@shell/utils/object';
import { AS, _YAML } from '@shell/config/query-params';
import ResourceLoadingIndicator from './ResourceLoadingIndicator';
import TabTitle from '@shell/components/TabTitle';
import { harvesterhci2cloud, cloud2harvesterhci } from '@shell/utils/router'

/**
 * Resource List Masthead component.
 */
export default {

  name: 'MastheadResourceList-btn',

  components: {
    Favorite,
    TypeDescription,
    ResourceLoadingIndicator,
    TabTitle
  },
  props: {
    resource: {
      type:     String,
      required: true,
    },

    schema: {
      type:    Object,
      default: null,
    },
    typeDisplay: {
      type:    String,
      default: null,
    },
    isCreatable: {
      type:    Boolean,
      default: null,
    },
    isYamlCreatable: {
      type:    Boolean,
      default: null,
    },
    createLocation: {
      type:    Object,
      default: null,
    },
    yamlCreateLocation: {
      type:    Object,
      default: null,
    },

    createButtonLabel: {
      type:    String,
      default: null
    },

    /**
     * Inherited global identifier prefix for tests
     * Define a term based on the parent component to avoid conflicts on multiple components
     */
    componentTestid: {
      type:    String,
      default: 'masthead'
    }
  },

  data() {
    const params = { ...this.$route.params };

    params.resource = harvesterhci2cloud(params.resource)

    const formRoute = { name: `${ cloud2harvesterhci(this.$route.name) }-create`, params };

    const hasEditComponent = this.$store.getters['type-map/hasCustomEdit'](this.resource);

    const yamlRoute = {
      name:  `${ cloud2harvesterhci(this.$route.name) }-create`,
      params,
      query: { [AS]: _YAML },
    };

    const tabList = ['集群配置'];
    return {
      formRoute,
      yamlRoute,
      hasEditComponent,
      tabList,
    };
  },

  computed: {
    get,
    ...mapGetters(['isExplorer', 'currentCluster']),

    _typeDisplay() {
      if ( this.typeDisplay !== null) {
        return this.typeDisplay;
      }

      if ( !this.schema ) {
        return '?';
      }

      return this.$store.getters['type-map/labelFor'](this.schema, 99);
    },


    _isYamlCreatable() {
      if ( this.isYamlCreatable !== null) {
        return this.isYamlCreatable;
      }

      return this.schema && this._isCreatable && this.$store.getters['type-map/optionsFor'](this.resource).canYaml;
    },

    _isCreatable() {
      // Does not take into account hasEditComponent, such that _isYamlCreatable works
      if ( this.isCreatable !== null) {
        return this.isCreatable;
      }

      // blocked-post means you can post through norman, but not through steve.
      if ( this.schema && !this.schema?.collectionMethods.find((x) => ['blocked-post', 'post'].includes(x.toLowerCase())) ) {
        return false;
      }

      return this.$store.getters['type-map/optionsFor'](this.resource).isCreatable;
    },

    _createLocation() {
      return this.createLocation || this.formRoute;
    },

    _yamlCreateLocation() {
      return this.yamlCreateLocation || this.yamlRoute;
    },

    _createButtonlabel() {
      return this.createButtonLabel || this.t('resourceList.head.create');
    },
  }
};
</script>

<template>
  <!-- 顶部 header，带有子标题（subheader）样式类 -->
  <div style="display: flex;width: 100%;">

    <!-- 操作按钮区域 -->
    <div 
      v-if="!(tabList.includes(_typeDisplay))"
      class="actions-container"
      style="min-height: 32px;align-self: flex-end;margin-right: 10px;"
    >
      <slot name="actions">
        <div class="actions">
          <slot name="extraActions" />

          <slot name="createButton">
            <router-link
              v-if="hasEditComponent && _isCreatable"
              :to="_createLocation"
              class="btn role-primary"
              :data-testid="componentTestid+'-create'"
            >
              {{ _createButtonlabel }}
            </router-link>
            <router-link
              v-else-if="_isYamlCreatable"
              :to="_yamlCreateLocation"
              class="btn role-primary"
              :data-testid="componentTestid+'-create-yaml'"
            >
              {{ t("resourceList.head.createFromYaml") }}
            </router-link>
          </slot>
        </div>
      </slot>
    </div>
  </div>
</template>

<style lang="scss" scoped>


  header {
    /* margin-bottom: 20px; */
  }

  header.with-subheader {
    /* grid-template-areas:
      'type-banner type-banner'
      'title actions'
      'sub-header sub-header'
      'state-banner state-banner'; */
    position: relative;
    display: flex;
    margin-bottom: 16px;
    flex-direction: column;
  }
  .excram-list{
    font-size: 14px;
    line-height: 22px;
    margin-bottom: 20px;
    font-family: 'Microsoft YaHei';
  }
  .excram-last-name{
    color: var(--primary);
  }
  .descrip-name{
    font-size: 26px;
    font-family: 'Microsoft YaHei';
  }
  .masthod-title-description{
    font-family: 'Microsoft YaHei';
    margin-top: 20px;
  }
  .actions-container{
    /* display: flex; */
    /* margin-left: 0px; */
  }
</style>
