<template>
  <div class="app-container">
    <div class="filter-container" style="float:right">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <el-table
      :data="configs"
      :header-cell-style="{background:'#F3F4F7',color:'#555'}"
      max-height="700"
      border
      highlight-current-row
      style="width: 100%;">
      <el-table-column label="ID" type="index" align="center" width="65"/>

      <el-table-column label="Database" prop="dbname" align="center"/>

      <el-table-column label="Host" prop="ip" align="center"/>

      <el-table-column label="Port" prop="port" align="center"/>

      <el-table-column label="Tables" prop="table_list" align="center" min-width="300"/>

      <el-table-column :label="$t('table.actions')" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">{{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->

    <el-dialog :title="dialogStatus" :visible.sync="dialogFormVisible">
      <el-alert
        v-show="dialogStatus==='新建'"
        style="margin-bottom:20px"
        title="新建需要管理员权限"
        type="warning"/>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 90%; margin-left:40px;">
        <el-form-item label="Database" prop="dbname" >
          <el-input v-model="temp.dbname" :disabled="disabled" style="width:100%"/>
        </el-form-item>

        <el-form :inline="true" :rules="rules" :model="temp" class="demo-form-inline" label-width="90px">
          <el-form-item label="Host" prop="ip">
            <el-input v-model="temp.ip" :disabled="disabled" placeholder="数据库同步环境IP" class="input-width"/>
          </el-form-item>
          <el-form-item label="Port" prop="port">
            <el-input v-model="temp.port" :disabled="disabled" placeholder="数据库同步环境端口" class="input-width"/>
          </el-form-item>
        </el-form>
        <el-form :inline="true" :model="temp" class="demo-form-inline" label-width="90px">
          <el-form-item label="Username">
            <el-input v-model="temp.username" placeholder="为空则使用默认的用户名" class="input-width"/>
          </el-form-item>
          <el-form-item label="Password">
            <el-input v-model="temp.passwd" placeholder="为空则使用默认的密码" class="input-width"/>
          </el-form-item>
        </el-form>

        <el-form-item label="Tables">
          <el-input :autosize="{ minRows: 5, maxRows: 10}" v-model="temp.table_list" type="textarea" placeholder="需要同步数据的表列表，表名与表名之间使用空格分隔，为空则该数据库下所有的表都不进行表数据同步"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button :disabled="disabled && dialogStatus==='新建'" type="primary" @click="createData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogDeleteVisible" :data="temp" title="Delete">
      <span >
        确定要删除{{ temp.dbname }}吗?
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-permission="['admin']" type="primary" @click="deleteData()">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getConfig, saveConfig, deleteConfig } from '@/api/dbconfig'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令

export default {
  components: { Pagination },
  directives: { permission },

  filters: {
  },
  data() {
    return {
      temp: {
      },
      disabled: undefined,
      dialogFormVisible: false,
      dialogDeleteVisible: false,
      dialogStatus: '',
      rules: {
        dbname: [{ required: true, message: '请输入', trigger: 'change' }],
        ip: [{ required: true, message: '请输入', trigger: 'change' }],
        port: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      configs: []
    }
  },
  computed: {

  },
  watch: {
  },
  created() {
    this.getConfig()
    this.disabled = !this.$store.getters.roles.includes('admin')
  },
  methods: {
    getConfig() {
      getConfig(this.listQuery).then(res => {
        this.configs = res.data
      })
    },

    resetTemp() {
      this.temp = {
        dbname: '',
        ip: '',
        port: '',
        username: '',
        passwd: '',
        table_list: ''
      }
    },

    handleCreate() {
      this.resetTemp()
      this.dialogStatus = '新建'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveConfig(this.temp).then((res) => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
            this.getConfig()
          })
        }
      })
    },

    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleDelete(row) {
      this.temp = row
      this.dialogDeleteVisible = true
    },

    deleteData() {
      const query = { _id: this.temp._id }
      deleteConfig(query).then(() => {
        this.dialogDeleteVisible = false
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getConfig()
      })
    }
  }
}
</script>

<style scoped>
.input-width {
  width: 200px;
}
</style>
