<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-select v-model="form.assignment[scope.$index].type" style="width: 100%;" placeholder="请选择提取方式">
                <el-option v-for="dict in dict.type.extract_type" :key="dict.value" :label="dict.label" :value="dict.value" @change="e => handleTableEdit(e, 'assignment', scope)"/>
              </el-select>
<!--              <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-select v-model="form.assignment[scope.$index].content" style="width: 100%;" placeholder="请选择提取对象">
                <el-option v-for="dict in extract_object" :key="dict.value" :label="dict.label" :value="dict.value" @change="e => handleTableEdit(e, 'assignment', scope)"/>
              </el-select>
<!--              <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-collapse v-model="activeNames" accordion v-if="form.testCaseResultList" @change="handleLogChange">
          <el-collapse-item :title="itemSub.title" :name="itemSub.title" v-for="(itemSub, index) in form.testCaseResultList" :key="index">
            <el-tabs v-model="activeTabs">
              <el-tab-pane label="sql结果" name="sqlResult">
                <codemirror v-model="resultLog.sqlResult" :options="editorOptions" v-if="resultLog.sqlResult"/>
              </el-tab-pane>
            </el-tabs>
          </el-collapse-item>
        </el-collapse>
      </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-select v-model="form.assertion[scope.$index].source" style="width: 100%;" filterable allow-create placeholder="请选择对象">
                <el-option v-for="dict in extract_object" :key="dict.value" :label="dict.label" :value="dict.value" @change="e => handleTableEdit(e, 'assertion', scope)"/>
              </el-select>
<!--              <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-select v-model="form.assertion[scope.$index].fn" style="width: 100%;" placeholder="请选择条件">
                <el-option v-for="dict in dict.type.assert_condition" :key="dict.value" :label="dict.label" :value="dict.value" @change="e => handleTableEdit(e, 'assertion', scope)"/>
              </el-select>
<!--              <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";
import {getCaseStepResultLog} from "@/api/test/caseStep";

export default {
  components: { codemirror },
  dicts: ['extract_type','assert_condition'],
  props: {
    form: {
      type: Object
    },
    datasourceList: {
      type: Array
    },
    quoteList: {
      type: Array
    }
  },
  mounted() {
    // 在组件挂载后调用方法
    this.appendNamesToExtractObject();
  },
  data() {
    return {
      activeName: "sql",
      extract_object: [{
        value: '${RESULT_DATA}',
        label: '${RESULT_DATA}'
      },{
        value: '${RESULT_DATA_LENGTH}',
        label: '${RESULT_DATA_LENGTH}'
      },
      //   {
      //   value: '${INSERT_ID}',
      //   label: '${INSERT_ID}'
      // },
      {
        value: '${AFFECT_ROWS}',
        label: '${AFFECT_ROWS}'
      },{
        value: '${CHANGE_ROWS}',
        label: '${CHANGE_ROWS}'
      }],
      resultLog: {
        requestHeader: '',
        requestBody: '',
        responseHeader: '',
        responseBody: '',
        sqlResult: ''
      },
      activeNames: 'sqlResult',
      activeTabs: 'sqlResult',
      editorOptions: {
        autoRefresh: true,
        mode: "sql", // 代码语言
        theme: "dracula", // 主题
        lineNumbers: true, // 显示行号
        tabSize: 2
      }
    }
  },
  methods: {
    appendNamesToExtractObject() {
      if (this.quoteList) {
        this.quoteList.forEach(item => {
          item.assignment.forEach(item2 => {
            if (item2.name && !this.isDuplicate(item2.name)) {
              this.extract_object.push({
                value: '${' + item2.name + '}',
                label: '${' + item2.name + '}'
              });
            }
          });
        });
      }
    },
    isDuplicate(name) {
      let compareName = '${' + name + '}';
      return this.extract_object.some(obj => obj.value === compareName);
    },
    handleTableEdit(e, flag, scope) {
      if (flag === "assignment") {
        if (e && this.form.assignment.length === scope.$index + 1) {
          this.form.assignment.push({
            key: "",
            value: ""
          })
        }
      }
      if (flag === "assertion") {
        if (e && this.form.assertion.length === scope.$index + 1) {
          this.form.assertion.push({
            key: "",
            value: ""
          })
        }
      }
    },
    handleDelete(flag, scope) {
      if (flag === "assignment") {
        this.form.assignment.splice(scope.$index, 1)
      }
      if (flag === "assertion") {
        this.form.assertion.splice(scope.$index, 1)
      }
    },
    handleLogChange(val) {
      const selectedItem = this.form.testCaseResultList.find(item => item.title === val);
      if (selectedItem) {
        getCaseStepResultLog(selectedItem.id).then(res => {
          this.resultLog = res.data;
        });
      }
    },
  }
}
</script>

<style scoped lang="scss">

</style>
