<template>
  <a-row>
    <a-col :span="24">
      <span class="title">参数</span>
      <p-button type="primary" :loading="spinning" size="small" style="margin-left: 20px; margin-bottom: 5px" @click="run">{{ $lang('运行') }}</p-button>
      <PCode ref="code" :height="400" style="border: 1px solid #e8e8e8" v-model="content" language="json" />
    </a-col>
  </a-row>
</template>

<script>
import PCode from './code'
import jsonViewer from 'vue-json-viewer'
import { sqlForTest } from './api'

export default {
  components: {
    PCode: PCode,
    jsonViewer: jsonViewer,
  },

  props: ['temp', 'infId', 'item', 'testParams'],

  data() {
    return {
      isShowModal: false,
      showModalTitle: '',
      confirmLoading: false,
      error: '',
      content: '',
      jsonData: '',
      spinning: false,
      data: [],
      tableColumns: [],
      second: 0.0,
    }
  },

  methods: {
    run() {
      let p = {}
      let _this = this
      //console.log(_this)
      if (this.content) {
        try {
          p = JSON.parse(this.content)
        } catch (error) {
          _this.$message.error('请输入正确参数')
          return
        }
      }

      this.spinning = true
      sqlForTest(p).then((res) => {
          this.spinning = false
          if(res.success){
            this.$message.success('测试成功!')
            this.$emit('callBackcallBack')
          }else{
            this.$exception(res)
            return
          }
        })
        .catch((er) => {
          this.$error(er)
          this.spinning = false
        })
    },

    parseData(data) {
      if (!data) {
        return
      }
      var da = data.list
      if (da.length > 0) {
        var item = da[0]
        this.data = da
      }
    },
    showModal() {
      this.$emit('callBack')
      setTimeout(() => {
        if (!this.testParams.dbKey) {
          this.$message.error('请输入数据源信息')
          return
        }
        this.isShowModal = true
        this.jsonData = ''
      }, 500)
    },

    handleOk() {},

    handleCancel() {
      this.isShowModal = false
      //this.content = '';
      this.jsonData = ''
      this.tableColumns = []
      this.data = []
    },
  },
}
</script>

<style lang="less" scoped>
@import '~ant-design-vue/es/style/themes/default';

.monaco-editor,
.monaco-editor .inputarea.ime-input {
  display: none;
}
.stime {
  color: @primary-color;
  display: inline-block;
  float: right;
}
.title {
  font-weight: bold;
  color: #000;
}
</style>
