<template>
  <static-content-wrapper
    :designer="designer"
    :field="field"
    :design-state="designState"
    :display-style="field.options.displayStyle"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
    <el-dropdown
      :trigger="field.options.dropdownTrigger"
      :disabled="field.options.disabled"
    >
      <el-button
        type="primary"
        class="button-sty"
        size="mini"
        v-if="field.options.dropdownType === 'button'"
      >
        <span>{{ getI18nLabel(field.options.label) }}</span
        ><span class="line"></span>
        <i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <span class="el-dropdown-link" v-else>
        <a class="a-link">
          <span>
            <span>{{ getI18nLabel(field.options.label) }}</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
        </a>
      </span>
      <!-- <span class="el-dropdown-link button-sty" v-else>
        
      </span> -->
      <el-dropdown-menu slot="dropdown">
        <template v-for="(subWidget, index) in field.widgetList">
          <el-dropdown-item
            v-show="!subWidget.options.hidden"
            :key="index"
            :icon="subWidget.options.icon"
            @click.native="(e) => handleSubButtonWidgetClick(e, index)"
            :disabled="subWidget.options.disabled"
          >
            <!--            {{ getI18nLabel(subWidget.options.label) }}-->
            <dropdownItemWidget
              :field="subWidget"
              :parent-list="field.widgetList"
              :index-of-parent-list="index"
              :parent-widget="field"
              :tableParam="tableParam"
              :formItemProp="formItemProp"
              :ref="'item' + index"
              @hiddenChange="handleHidden"
              @disabledChange="handleDisabled"
            >
            </dropdownItemWidget>
          </el-dropdown-item>
        </template>
      </el-dropdown-menu>
    </el-dropdown>
  </static-content-wrapper>
</template>

<script>
import StaticContentWrapper from "./static-content-wrapper";
import emitter from "../../../utils/emitter";
import i18n from "../../../utils/i18n";
import fieldMixin from "./fieldMixin";
import dropdownItemWidget from "./dropdown-item-widget.vue";

export default {
  name: "dropdown-widget",
  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin, i18n],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,

    designState: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
  },
  components: {
    StaticContentWrapper,
    dropdownItemWidget,
  },
  computed: {},
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
       需要在父组件created中初始化！！ */
    this.registerToRefList();
    this.initEventHandler();
    this.init();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  beforeDestroy() {
    this.unregisterFromRefList();
  },

  methods: {
    init() {
      if (this.designState) return;
      if (!this.field.options.dropdownType)
        this.field.options.dropdownType = "button";
      if (!this.field.options.dropdownTrigger)
        this.field.options.dropdownTrigger = "hover";
      let isShow = this.field.widgetList.some((item) => !item.options.hidden);
      if (!isShow) {
        this.field.options.hidden = true;
      }
      let isEnabled = this.field.widgetList.some(
        (item) => !item.options.disabled
      );
      if (!isEnabled) {
        this.field.options.disabled = true;
      }
    },
    handleSubButtonWidgetClick(e, index) {
      this.$refs["item" + index][0].handleButtonWidgetClick(e);
    },
    handleHidden() {
      let isShow = this.field.widgetList.some((item) => !item.options.hidden);
      this.setHidden(!isShow);
    },
    handleDisabled() {
      let isEnabled = this.field.widgetList.some(
        (item) => !item.options.disabled
      );
      this.setDisabled(!isEnabled);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/global.scss"; //* static-content-wrapper已引入，还需要重复引入吗？ *//
.el-dropdown-link[disabled] a.a-link {
  cursor: not-allowed;
}
</style>
