<template>
  <div :id="elId"  style="width:100%;height:100%">
      <div id="hsCarousel"  style="width:100%;height:100%">
      <el-carousel  style="width:100%;height:100%">
        <el-carousel-item :style="itemStyle" v-for="(item,index) in dataList" :key="index">
          <h3 class="small">{{ item.text }}</h3>
        </el-carousel-item>
      </el-carousel>
    </div>
     <el-dialog :close-on-click-modal=false
                :append-to-body=true
                title="配置"
                width='90%'
                :visible.sync="jsoneditorVisible">
      <jsoneditor v-if='jsoneditorVisible'
                  :elInfo='elInfo'
                  :initStyle='initStyle'
                  :saveConfigDataFun='saveConfigDataFun'
                  :jsoneditorData='jsoneditorData'
                  :dealWithSourceFun='dealWithSource'>
      </jsoneditor>
    </el-dialog>
  </div>
</template>
<script>
import jsoneditor from '../common/jsoneditor'
import uuidv1 from 'uuid/v1'
export default {
  name: 'hsCarousel',
  components: {
    jsoneditor
  },
  props: {
    allSourceData: {
      default: {}
    },
    itemStyle: {
      type: Object,
      default() {
        return {
        }
      }
    },
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    saveConfigDataFun: {
      type: Function
    },
    initStyle: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    allSourceData: {
      handler: function(newVal, oldVal) {
        this.initData(newVal)
      },
      deep: true
    }
  },
  computed: {
    jsoneditorData() {
      return {
        config: this.jsoneditorConfig,
        sourceData: this.jsoneditorSource
      }
    }
  },
  data() {
    return {
      elId: uuidv1(),
      jsoneditorVisible: false,
      dataList: [],
      jsoneditorConfig: {}, // 配置
      jsoneditorSource: [], // 原始数据
      sourceData: {}
    }
  },
  mounted() {
    this.showEditJsonDialog()
  },
  methods: {
    dealWithSource(config, sourceData) {
      return {
        config,
        sourceData
      }
    },
    // 弹出可编辑的弹框
    showEditJsonDialog() {
      const out = document.getElementById(this.elId)
      const that = this
      out.onmousedown = function(event) {
        //
        event.stopPropagation()
        if (event.shiftKey && event.altKey && !event.ctrlKey) {
          that.jsoneditorVisible = true
        }
      }
    },
    initData(data) {
      if (data && data.sourceData && data.sourceData.length) {
        this.dataList = data.sourceData
        this.jsoneditorConfig = data.config || {}
        this.jsoneditorSource = data.sourceData
      } else {
        this.dataList = [{ id: '1', text: '华晨宝马x5' }, { id: '2', text: '梅赛德斯大G' }, { id: '3', text: '奥迪Q8' }]
        this.jsoneditorConfig = {}
        this.jsoneditorSource = this.dataList
      }
    }
  }
}
</script>
<style scoped>
#hsCarousel >>>  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    margin: 0;
  }

#hsCarousel >>>   .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }

#hsCarousel >>>   .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
#hsCarousel  >>> .el-carousel__container{
    height: 100%;
  }
</style>
