<template>
  <div
    class="widthHeightCommon"
    :id='elId'
  >
    <el-tabs
      style="width:100%;height:100%"
      v-model="value_inner"
      @tab-click="handleClick"
      :before-leave='beforeLeave'
    >
      <el-tab-pane
        v-for="(item,index) in tabsData"
        :key="index"
        :label="item.label"
        :name="item.name"
      >
      </el-tab-pane>
    </el-tabs>
    <jsoneditor
      v-model='jsoneditorVisible'
      :elInfo='elInfo'
      :jsoneditorCloseAfter='jsoneditorCloseAfter'
      :jsoneditorOpenAfter='jsoneditorOpenAfter'
    >
    </jsoneditor>
  </div>
</template>
<script>
import ucComponent from '../ucClass/uc_component'
import uuidv1 from 'uuid/v1'
import jsoneditor from '../common/jsoneditor'
export default {
  mixins: [ucComponent],
  components: {
    jsoneditor
  },
  name: 'hsTabs',
  props: {
    allSourceData: {
      type: Object,
      default() {
        return {}
      }
    },
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    value: '',
    saveConfigDataFun: {
      type: Function
    },
    jsoneditorCloseAfter: {
      type: Function
    },
    jsoneditorOpenAfter: {
      type: Function
    }
  },
  watch: {
    value(val) {
      this.value_inner = val
    },
    value_inner(val) {
      this.$emit('input', val)
    },
    allSourceData: {
      handler: function() {
        this.init()
      },
      deep: true
    }
  },
  data() {
    return {
      value_inner: '',
      elId: '',
      jsoneditorVisible: false,
      tabsData: []
    }
  },
  created() {
    this.elId = uuidv1() // 获取随机id
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const { config } = this.allSourceData
      this.tabsData = config.tabsData
    },
    handleClick(tab, event) {
      const item = this.tabsData.find(item => item.label === tab.label)
      this.$emit('handleClick', { tab, event, item })
    },
    beforeLeave() {
      this.$emit('beforeLeave')
    }
  }
}
</script>
<style scoped>
.widthHeightCommon {
  width: 100%;
  height: 100%;
}
</style>
