<template>
  <div>
    <div style="display:flex;">
      <div id='editor1'
           style="width: 700px; height: 400px;"></div>
      <div id='editor2'
           style="width: 700px; height: 400px;">
      </div>
    </div>
    <el-row :gutter="20">
      <el-col :span="16">
        <div class="grid-content bg-purple">
          <el-input placeholder="请输入内容">
            <template slot="prepend">Http://</template>
          </el-input>
        </div>
      </el-col>
      <div>
        <el-button type="primary"
                   @click='showEchart'>查看效果</el-button>
        <el-button type="danger"
                   @click="openTips">提交到数据库</el-button>
      </div>
      <commonTable :tableAll="commonTableData"
                   :configAll="commonConfigData"></commonTable>

    </el-row>
  </div>
</template>
<script>
import commonTable from '../common/commonTable'
export default {
  components: {
    commonTable
  },
  name: 'tableEditor',
  props: {
    saveConfigDataFun: {
      type: Function
    },
    configAll: {
      type: Object,
      default() {
        return {}
      }
    },
    tableAll: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      options: {
        mode: 'text',
        indentation: 2,
        search: true
      },
      idEditor1: '',
      idEditor2: '',
      editor1: null,
      editor2: null,
      editorChange: false,
      commonTableData: [],
      commonConfigData: {
        'styles': {
          'tableStyle': 'color:#babaff;font-size:.18rem;background-color:#232449;',
          'maxHeight': 'none',
          'border': false,
          'hideHeader': false,
          'headerRow': {
            'allStyle': 'border-bottom: 0.02rem solid #181635;color:#babaff;font-size:.14rem;text-align:center;padding: .2rem 0;'
          },
          'row': {
            'allStyle': ''
          },
          'column': {
            'columnIndexArr': [0, 2, 3, 4],
            'styleArr': ['fontSize:0.14rem;', 'color:#00DCE3;', 'color:#5E80FF;'],
            'allStyle': 'border-color: #232449;text-align:center;padding: .15rem 0 !important;'
          },
          'hover': {
            'hoverBackground': 'no',
            'leaveBackground': 'no'
          }
        }
      }
    }
  },
  mounted() {
    this.setJson()
  },
  methods: {
    openTips() {
      this.$confirm('此操作将覆盖数据库中的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.submitToDb()
        })
        .catch((e) => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    submitToDb() {
      if (this.editorChange) {
        this.saveConfigDataFun(this.editor1.get(), this.editor2.get())
      } else {
        this.saveConfigDataFun(this.configAll, this.tableAll)
      }
    },
    setJson() {
      this.drow('editor1', this.configAll, 1)
      this.drow('editor2', this.tableAll, 2)
    },
    showEchart() {
      this.editorChange = true
      const config = this.editor1.get()
      const sourceData = this.editor2.get()
      this.commonConfigData = config
      this.commonTableData = sourceData
    },
    drow(el, json = {}, flage) {
      const container = document.getElementById(el)
      this[`editor${flage}`] = new this.$jsoneditor(container, this.options)
      this[`editor${flage}`].set(json)
    }
  }
}
</script>
