<template>
  <el-dialog
   :close-on-click-modal="false"
    class="scrollbar-dialog"
    title="测试配置字段"
    :visible.sync="dialogVisible"
    v-if="dialogVisible"
    @close="closeDialog">
    <el-scrollbar class="layer-content">
      <el-form ref="dialogForm" :model="dialogForm" label-width="120px" v-loading="dialogLoading">
        <div class="formGroup">
          <el-row :gutter="20">
            <el-col :span="20" v-for="(item, index) in sqlData" :key="index">
              <el-form-item :label="item" required>
                <el-input v-model="dialogForm[item]"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-scrollbar>
    <span slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { scriptValidate } from '@/api/esb_service'

export default {
  data() {
    return {
      dialogVisible: this.value,
      dialogLoading: false,
      dialogForm: {}
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    sqlData: {
      type: Array,
      default() {
        return []
      }
    },
    sqlFormData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {

      } else {
        this.dialogForm = {}
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    closeDialog() {
      this.$refs.dialogForm.resetFields()
    },
    submit() {
      for (let index = 0; index < this.sqlData.length; index++) {
        const element = this.sqlData[index]
        if (!this.dialogForm[element]) {
          this.$message.warning(`请输入${element}！`)
          return
        }
      }
      this.dialogLoading = true
      scriptValidate({
        id: this.sqlFormData.id,
        script: this.sqlFormData.script,
        params: this.dialogForm
      }).then(rt => {
        this.dialogLoading = false
        this.$emit('confirm', rt.status === '200' ? 'true' : 'false')
        if (rt.status === '200') {
          this.$message.success(`测试成功`)
          this.dialogVisible = false
        }
      }).catch(() => {
        this.$emit('confirm', 'false')
        this.dialogLoading = false
      })
      // this.$refs['dialogForm'].validate((valid) => {
      //   if (valid) {
      //   }
      // })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
  .scrollbar-dialog {
    .el-dialog__body {
      padding: 0;

      .el-scrollbar {
        height: auto;
      }
    }
  }
</style>
