<template>
  <c-dialog
    ref="dialog"
    :size="sizeClassName"
    v-bind="$attrs"
  >
    <schema-table
      ref="table"
      :schema="schema"
      v-on="schema.listeners"
    >
      <template
        v-if="hasHeader"
        #header="scope"
      >
        <slot
          name="header"
          v-bind="scope"
        />
      </template>
    </schema-table>
    <slot />
  </c-dialog>
</template>

<script>
export default {
  name: 'TableDialog',
  inheritAttrs: false,
  props: {
    schema: {
      type: Object,
      required: true
    },
    size: {
      type: String,
      default: 'medium'
    }
  },
  computed: {
    hasHeader () {
      return !!this.$scopedSlots.header
    },
    sizeClassName () {
      return `table ${this.size} fixed`
    }
  },
  methods: {
    show (condition) {
      if (condition) {
        this.schema.condition = { ...this.schema.condition, ...condition }
      }
      this.$refs.dialog.show()
    },
    getTable () {
      return this.$refs.table
    }
  }
}
</script>
