/**模板
 *
  <a-modal
      title="mx_modal_title"
      :width="580"
      destroyOnClose
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-grid-form ref="modalForm" :items="mx_modal_formItems"></p-grid-form>
    </a-modal>
 */

import { getDrives, addFormItem, updateFormItem,deleteFormItem,testDateSource } from '../js/api'
import { getFormItems } from '../js/modalConfig'
const title = ['新增数据源', '修改数据源']

export default {
  data() {
    return {
      mx_modal_state: 0,
      mx_modal_title: '',
      visible: false,
      mx_modal_formItems: [],
      drivesList: []
    }
  },
  mounted() {

    getDrives.call(this).then(data => {
      if (data) {
        this.drivesList = data.map(item => {
          let value = Object.values(item)[0]
          return { value, label: value }
        })
        this.drivesList.unshift({ value: '', label: '请选择驱动类' })
      }
    })
  },
  methods: {
    mx_handleCancel() {
      this.visible = false
    },
    mx_modal_OPEN(state,record=null) {
      this.visible = true
      this.mx_modal_state = state
      this.mx_modal_title = this.$lang(title[this.mx_modal_state])
      this.mx_modal_formItems = getFormItems(this);
      this.$nextTick(() => {
        this.$refs.modalForm.form.resetFields()
        if (record) {
          this.$refs.modalForm.form.setFieldsValue(record)
        }

      })
    },
    /** 测试*/
    mx_testDateSource(data) {
      return testDateSource(this,data)
    },
    /** 新增*/
    mx_save_0(values) {
      return addFormItem(this, values)
    },
    /** 修改*/
    mx_save_1(values) {
      return updateFormItem(this, values)
    },
    mx_del(id) {
      return deleteFormItem(this,id)
    },
    mx_handleOk() {
      this.$refs.modalForm.form.validateFields((err, values) => {
        if (!err) {
          this['mx_save_' + this.mx_modal_state](values).then(response => {
            this.mx_handleCancel()
            if (response.success) {
              this.$refs.table.reload()
            }
          })
        }
      })
    }
  }
}

// [
//   { mysql: 'com.mysql.jdbc.Driver' },
//   { oracle: 'oracle.jdbc.OracleDriver' },
//   { presto: 'com.facebook.presto.jdbc.PrestoDriver' },
//   { postgresql: 'org.postgresql.Driver' }
// ]
