<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-10-09 16:04:09
 * @LastEditors: 无尘
 * @LastEditTime: 2019-10-11 16:45:51
 -->
<!--
<web-editor
  :showJsonDialog="showJsonDialog"
  @hideJsonDialog="hideJsonDialog">
</web-editor>

import webEditor from '@/components/createApi/web-editor.vue'
 -->
<template>
  <div v-show="customDialog">
    <el-dialog
      class="edit-dialog"
      title="导入json"
      :visible.sync="customDialog"
      width="600px"
      :before-close="handleClose">
      <div class="edit-dialog-body">
        <el-tabs
          v-model="activeName"
          @tab-click="handleClick">
          <el-tab-pane
            label="JSON"
            name="first">
            <editor
              ref="myEditorJson"
              v-model="contentJson"
              lang="json"
              theme="chrome"
              width="500"
              height="300"
              @init="editorInit" />
          </el-tab-pane>
          <el-tab-pane
            label="JSON-SCHEMA"
            name="second">
            <editor
              ref="myEditorJsonSchema"
              v-model="contentJsonSchema"
              lang="json"
              theme="chrome"
              width="500"
              height="300"
              @init="editorInit" />
          </el-tab-pane>
        </el-tabs>
      </div>
      <div
        slot="footer"
        class="dialog-footer"
        style="padding-bottom: 10px;">
        <el-button @click="customCancel">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="customConfirm('importForm')">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
/* eslint-disable */
import { getRequest } from '@/api/api';
import showMsg from '@/assets/js/showmsg';
import errMsg from '@/assets/js/error';
import { _debounce } from '@/assets/js/public';
import isJSON  from 'is-json';
export default {
  name: 'import-dubbo',
  props: {
    showJsonDialog: {
      type: Boolean,
      default: false
    }
  },
  components: {
    editor: require('vue2-ace-editor'),
  },
  data() {
    return {
      customDialog: false, // 弹框显示
      activeName: 'first',
      contentJson: '',
      contentJsonSchema:''
    };
  },
  methods: {
    /**
     *   关闭
     */
    handleClose(done) {
      const that = this;
      done();
      that.hideDialog();
    },

    /**
     *  取消
     */
    customCancel() {
      const that = this;
      that.hideDialog();
    },

    hideDialog() {
      const that = this;
      that.customDialog = false;
      that.$emit('hideJsonDialog', '');
    },

    /**
     *  确定
     */
    customConfirm(formName) {
      const that = this;
      if (that.activeName == 'first' && !isJSON(that.contentJson)) {
        showMsg.showmsg('json 数据格式有误', 'warning');
        return false;
      }
      if (that.activeName == 'second' && (!isJSON(that.contentJsonSchema) || Array.isArray(JSON.parse(that.contentJsonSchema)))) {
        showMsg.showmsg('JSON-SCHEMA 数据格式有误', 'warning');
        return false;
      }
      const emitData = {

      }//that.activeName == 'first' ? that.contentJson : that.contentJsonSchema;
      // 第一种情况： json----obj
      if (that.activeName == 'first' && Object.prototype.toString.call(JSON.parse(that.contentJson)) == '[object Object]') {

      }
      // 第二种情况： json----array
      if (that.activeName == 'first') {

      }
      // 第三种情况： jsonschema----obj
      if (that.activeName == 'second') {

      }
      // 处理json 和jsonschema
      that.$nextTick(() => {
        that.$emit('hideJsonDialog', emitData, that.activeName);
      })

    },

    editorInit: function (editor) {
      require('brace/ext/language_tools') //language extension prerequsite...
      require('brace/mode/html')
      require('brace/mode/json')
      require('brace/mode/javascript')    //language
      require('brace/theme/chrome')
      require('brace/snippets/javascript') //snippet
      // editor.getSession().setMode("brace/mode/json")
      editor.setShowPrintMargin(false);
      editor.setOptions({
        fontFamily: "Consolas,Monaco,monospace",
        fontSize: "12px"
      });
    }

  },
  watch: {
    showJsonDialog: function(newData, oldData) {
      const that = this;
      that.customDialog = newData;
    }
  },

  /* 接收数据 */
  mounted() {
    const that = this;
    that.customDialog = that.showJsonDialog;
  }
};
</script>

<style lang="less" scoped>
.edit-dialog {
  /deep/ .el-dialog__body {
    // padding: 2px 55px 0 34px;
  }

  /deep/ .el-dialog__footer {
    border: none;
    padding-top: 3px;
  }

  /deep/ .ace_line{
    font-family: Consolas, Monaco, monospace;
  }

  &__title {
    width: 100%;
    height: 38px;
    padding: 12px 15px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background: rgba(235, 238, 245, 1);
  }

  .el-alert--info {
    width: 400px;
    font-size: 14px;
    color: #606266;
    background: #e6f7ff;
    border: 1px solid rgba(145, 213, 255, 1);
  }
  .el-icon-info {
    width: 12px;
    font-size: 12px;
    color: #1890ff;
  }
}
</style>
