<template>
  <el-dialog
    :title="title"
    center
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" @keyup.enter.native="doSubmit()"
             label-width="120px" 
             element-loading-text="正在测试数据库连接"
             :class="method==='view'?'readonly':''" :disabled="method==='view'"
             v-loading="loading" @submit.native.prevent>
    <el-row :gutter="15">
        <el-col :span="12">
            <el-form-item label="连接名称" prop="name" :rules="[{required: true, message:'连接名称不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.name" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="连接英文名" prop="enName" :rules="[{required: true, message:'连接英文名不能为空', trigger:'blur'},
            {validator:checkEnName, trigger:'blur'}]">
              <el-input v-model="inputForm.enName" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数据库类型" prop="type" :rules="[{required: true, message:'数据库类型不能为空', trigger:'blur'}]">
              <el-select v-model="inputForm.type" placeholder="请选择来源"  style="width: 100%;">
                <el-option
                  v-for="item in this.$dictUtils.getDictList('db_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="主机地址" prop="host" :rules="[{required: true, message:'主机地址不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.host" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="端口" prop="port" :rules="[{required: true, message:'端口不能为空', trigger:'blur'}]">
              <el-input v-model.number="inputForm.port" type="number" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数据库名" prop="dbname" :rules="[{required: true, message:'数据库名不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.dbname" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数据库用户名" prop="username" :rules="[{required: true, message:'数据库用户名不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.username" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
         <el-col :span="12">
            <el-form-item label="数据库密码" prop="password" :rules="[{required: true, message:'数据库密码名不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.password" maxlength="50" placeholder=""></el-input>
            </el-form-item>
        </el-col>
    </el-row>
      
     
     
    </el-form>
    <span slot="footer"  class="dialog-footer">
      <el-button v-if="method != 'view'" type="primary" @click="testDbLink()">测试连接</el-button>
      <el-button v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      <el-button @click="visible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      loading: false,
      method: '',
      title: '',
      checkEnName: (rule, value, callback) => {
        if (value === 'master') {
          return callback(new Error('master是系统默认主数据源名，请换一个名字!'))
        }
        this.$http.get(`/database/datalink/dataSource/checkEnName?oldEnName=${this.inputForm.oldEnName}&enName=${value}`).then(({data}) => {
          if (!data.success) {
            return callback(new Error('连接英文名已存在!'))
          } else {
            return callback()
          }
        })
      },
      inputForm: {
        id: '',
        name: '',
        enName: '',
        oldEnName: '',
        type: '',
        host: '',
        port: '',
        dbname: '',
        username: '',
        password: ''
      }
    }
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建数据库连接`
      } else if (method === 'edit') {
        this.title = '修改数据库连接'
      } else if (method === 'view') {
        this.title = '查看数据库连接'
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.$http({
            url: `/database/datalink/dataSource/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.dataSource)
            this.inputForm.oldEnName = this.inputForm.enName
          })
        }
      })
    },

    testDbLink () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post('/database/datalink/dataSource/test', this.inputForm).then(({data}) => {
            this.$message.info(data.msg)
            this.loading = false
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/database/datalink/dataSource/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
