<template>
  <el-form ref="queryForm" class="query-form" :model="queryParams">
    <el-row :gutter="20">
      <el-col :span="16">
        <div class="code-editor">
          <java-code-edit v-model="queryParams.code" :editor-placeholder="'请输入java代码'" :editor-height="250"
                          :tab-size="2"/>
        </div>
      </el-col>
      <el-col :span="8">
        <el-form-item label="模拟数据" prop="mockData">
          <el-input v-model="queryParams.mockData" placeholder="请输入模拟数据" :rows="4" type="textarea" clearable>
          </el-input>
        </el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
        <el-button type="primary" @click="handleExecute">执行</el-button>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup>
import JavaCodeEdit from "@/components/codeEdit/JavaCodeEdit.vue";
import {ElMessage} from "element-plus";
import {executeAdapterMockData, getDataAdapterDetails,editDataAdapter} from "@/api/custom-query/adapter";
import {useRouter} from "vue-router";

const router = useRouter();
const queryId = reactive(router.currentRoute.value.params.queryId)
const queryParams = ref({
  adapterName:null,
  type:null,
  source:"CUSTOM",
  code: null,
  mockData: null
})

const getDetails = async () => {
  await getDataAdapterDetails(queryId).then(res => {
    if (res.code === 1000) {
      queryParams.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
getDetails()
const handleSave=()=>{
  editDataAdapter(queryParams.value).then(res => {
    if (res.code === 1000) {
      ElMessage.success(res.msg)
    } else {
      ElMessage.error(res.msg)
    }
  })
}
const handleExecute = () => {
  executeAdapterMockData(queryParams.value).then(res => {
    console.log('handleExecute',res)
    if (res.code === 1000) {
      ElMessage.success(res.msg)
    } else {
      ElMessage.error(res.msg)
    }
  })
}
</script>

<style scoped>

</style>
