<template>
  <div>
    <div class="editJsonBox">
      <div class="editItem">
        <div>配置</div>
        <div id="editor1Db" class="jsonItem"></div>
      </div>
      <div class="editItem">
        <div>原始数据</div>
        <div id="editor2Db" class="jsonItem"></div>
      </div>
      <div class="editItem">
        <div>接口配置</div>
        <div id="editor3" class="jsonItem"></div>
      </div>
    </div>
    <div style="margin-top:30px;">
      <el-select v-model="typeValue" placeholder="请选择图表类型" @change="typeChange">
        <el-option
          v-for="item in typeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button type="danger" @click="submitToDb">提交到数据库</el-button>
    </div>
    <el-row :gutter="20">
      <commonEchart v-show="false" :initStyle="initStyle" :setOptions="commonData"></commonEchart>
    </el-row>
  </div>
</template>

<script>
import commonEchart from './commonEchart'
import MockData from './initDbConfigDataJson'
export default {
  components: { commonEchart },
  name: 'jsoneditor',
  props: {
    saveConfigDataFun: {
      type: Function
    },
    initStyle: {
      type: Object,
      default() {
        return {
          width: '400px',
          height: '400px'
        }
      }
    }
  },
  data() {
    return {
      options: {
        mode: 'text',
        indentation: 2,
        search: true
      },
      initJson: {
        config: { },
        sourceData: [],
        dbConfig: { page: '哪个页面',
          position: '页面中的哪个位置'
        }
      },
      editor1Db: null,
      editor2Db: null,
      editor3: null,
      commonData: {},
      typeValue: '',
      typeOptions: [
        { value: 1, label: 'line-bar' },
        { value: 2, label: 'pie' }
      ],
      lineBarConfig: {},
      lineBarSourceData: [],
      pieConfig: {},
      pieSourceData: []
    }
  },
  mounted() {
    this.setJson(this.initJson)
  },
  methods: {
    typeChange(val) {
      switch (val) {
        case 1:
          this.editor1Db.set(MockData['hsLineBar'].config)
          this.editor2Db.set(MockData['hsLineBar'].sourceData)
          break
        case 2:
          this.editor1Db.set(MockData['hsPie'].config)
          this.editor2Db.set(MockData['hsPie'].sourceData)
      }
    },
    setLineBar() {},
    submitToDb() {
      this.openTips()
    },
    openTips() {
      this.$confirm('此操作将覆盖数据库中的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          if (this.saveConfigDataFun) {
            this.saveConfigDataFun(this.editor1Db.get(), this.editor2Db.get())
          } else {
            this.localSaveConfigDataFun()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    localSaveConfigDataFun() {
      const config = this.editor1Db.get()
      const data = this.editor2Db.get()
      const dbConfig = this.editor3.get()
      const params = {
        page: dbConfig.page,
        position: dbConfig.position
      }
      this.$API.runSave(params, config, data).then(x => {
        this.$message({
          type: 'success',
          message: '更新成功'
        })
      })
    },
    setJson(data) {
      const { config, sourceData, dbConfig } = JSON.parse(JSON.stringify(data))
      this.drow('editor1Db', config)
      this.drow('editor2Db', sourceData)
      this.drow('editor3', dbConfig)
    },
    showEchart() {
      const config = this.editor1Db.get()
      // const sourceData = this.editor2Db.get()
      // const options = this.dealWithSourceFun(config, sourceData)
      this.commonData = config
    },
    drow(el, json = {}) {
      const container = document.getElementById(el)
      this[el] = new this.$jsoneditor(container, this.options)
      this[el].set(json)
    }
  }
}
</script>
<style scoped>
.editJsonBox {
  display: flex;
  width: 100%;
}
.editItem {
  height: 400px;
  flex: 1;
}
.jsonItem {
  height: 400px;
}
</style>
