<template>
  <div class="app-container">
    <el-form :model="form" ref="form">
      <el-form-item label="数据源" prop="datasourceId">
        <el-select v-model="form.datasourceId" style="width: 100%;" clearable>
          <el-option v-for="item in this.datasourceList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName">
      <el-tab-pane label="SQL指令" name="sql">
        <codemirror v-model="form.sqlCommand" :options="editorOptions" />
      </el-tab-pane>
      <el-tab-pane label="提取" name="assignment">
        <el-table :data="form.assignment">
          <el-table-column label="变量名">
            <template slot-scope="scope">
              <el-input placeholder="请输入变量名" v-model="form.assignment[scope.$index].name" @input="e => handleTableEdit(e, 'assignment', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="提取方式">
            <template slot-scope="scope">
              <el-input placeholder="请输入提取方式" v-model="form.assignment[scope.$index].type" @input="e => handleTableEdit(e, 'assignment', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="提取对象">
            <template slot-scope="scope">
              <el-input placeholder="请输入提取对象" v-model="form.assignment[scope.$index].content" @input="e => handleTableEdit(e, 'assignment', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="提取表达式">
            <template slot-scope="scope">
              <el-input placeholder="请输入提取表达式" v-model="form.assignment[scope.$index].path" @input="e => handleTableEdit(e, 'assignment', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="60">
            <template slot-scope="scope">
              <el-button v-if="form.assignment.length > scope.$index+1" size="mini" type="text" icon="el-icon-delete" @click="handleDelete('assignment', scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="校验" name="assertion">
        <el-table :data="form.assertion">
          <el-table-column label="描述">
            <template slot-scope="scope">
              <el-input placeholder="请输入描述" v-model="form.assertion[scope.$index].name" @input="e => handleTableEdit(e, 'assertion', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="对象">
            <template slot-scope="scope">
              <el-input placeholder="请输入对象" v-model="form.assertion[scope.$index].source" @input="e => handleTableEdit(e, 'assertion', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="条件">
            <template slot-scope="scope">
              <el-input placeholder="请输入条件" v-model="form.assertion[scope.$index].fn" @input="e => handleTableEdit(e, 'assertion', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="内容">
            <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-model="form.assertion[scope.$index].target" @input="e => handleTableEdit(e, 'assertion', scope)" clearable/>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="60">
            <template slot-scope="scope">
              <el-button v-if="form.assertion.length > scope.$index+1" size="mini" type="text" icon="el-icon-delete" @click="handleDelete('assertion', scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { codemirror } from "vue-codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/mode/sql/sql"; // 语言模式
import "codemirror/theme/dracula.css";
import "codemirror/addon/display/autorefresh";

export default {
  components: { codemirror },
  props: {
    form: {
      type: Object
    },
    datasourceList: {
      type: Array
    }
  },
  data() {
    return {
      activeName: "sql",
      editorOptions: {
        autoRefresh: true,
        mode: "sql", // 代码语言
        theme: "dracula", // 主题
        lineNumbers: true, // 显示行号
        tabSize: 2
      }
    }
  },
}
</script>

<style scoped lang="scss">

</style>
