<template>
  <div class="pa-3" style="background-color: #FFF;">
    <filter-list :option="filter" @search="handleSearch" />
    <table-list
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :page-info="pageInfo"
      :show-select="false"
      @add="handleAdd"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #actions="{ item }">
        <v-btn text color="primary" @click="handleEdit(item)">编辑</v-btn>
        <v-btn text color="error" @click="handleDelete(item.uid)">删除</v-btn>
      </template>
      <template v-slot:remark="{item}">
        <td >
          {{ item.remark && item.remark.substr(0, 8) }}
          <template v-if="item.remark && item.remark.length > 8">...</template>
        </td>
      </template>
      <template #status="{ item }">
        <v-chip :color="item.status ? 'success' : 'error'" small>{{ item.status ? '启用' : '禁用' }}</v-chip>
      </template>
      <template #credential_configured="{ item }">
        <v-chip
          :color="item.credential_configured ? 'success' : 'warning'"
          small
        >
          {{ item.credential_configured ? '已配置' : '未配置' }}
        </v-chip>
      </template>
    </table-list>
    <edit-dialog :title="title" :visible.sync="show" @submit="handleSubmit">
      <edit-page class="pt-5" v-if="show" ref="edit" :item-data="itemData"></edit-page>
    </edit-dialog>

    <Linear :visible.sync="submitLoading"></Linear>
  </div>
</template>

<script>
import EditPage from './components/edit.vue'
import FilterList from '@/components/Filter'
import TableList from '@/components/List/table.vue'
import EditDialog from '@/components/Dialog'
import Linear from '@/components/Progress/linear.vue'
import {
  getDatasourceList,
  saveDatasource,
  deleteDatasource
} from '@/api/dataOrigin'

export default {
  name: 'RDbmsDataSource',
  components: { FilterList, TableList, EditDialog, EditPage, Linear },
  data () {
    return {
      show: false,
      filter: {
        list: [
          { type: 'textField', value: '', label: '数据源名称', key: 'name_zh' }
          // { type: 'autocomplete', value: null, label: '配置类型', key: 'type', items: [] }
        ]
      },
      queryForm: {
        name_zh: '' // 配置名称
      },
      pageInfo: {
        current: 1,
        size: 10
      },
      total: 0,
      orders: [],
      loading: true,
      submitLoading: false,
      headers: [
        { text: '中文名称', align: 'start', value: 'name_zh', slotName: 'name_zh' },
        { text: '英文名称', align: 'start', value: 'name_en' },
        { text: '数据库类型', align: 'start', value: 'type' },
        { text: '主机地址', align: 'start', value: 'host' },
        { text: '端口', align: 'start', value: 'port' },
        { text: '数据库名称', align: 'start', value: 'database' },
        { text: '数据库结构（schema）', align: 'start', value: 'schema' },
        { text: '凭据状态', align: 'start', value: 'credential_configured', useSlot: true },
        { text: '状态', align: 'start', value: 'status' },
        { text: '备注', align: 'start', value: 'desc', useSlot: true },
        { text: '操作', align: 'start', value: 'actions' }
      ],
      items: [],
      itemData: {}
    }
  },
  computed: {
    title () {
      return `${Object.keys(this.itemData).length ? '编辑' : '新增'}数据源`
    }
  },
  created () {
    // this.init()
    this.initPage()
  },
  methods: {
    async initPage () {
      try {
        this.loading = true
        const { data } = await getDatasourceList({
          ...this.queryForm
        })
        this.total = data.total
        this.items = data.data_source
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    },
    handleSearch (val) {
      this.pageInfo.current = 1
      Object.assign(this.queryForm, val)
      this.initPage()
    },
    async handleSubmit () {
      const _ref = this.$refs.edit
      if (_ref.validate()) {
        const isEdit = Object.keys(this.itemData).length
        const data = {}
        if (isEdit) data.uid = this.itemData.uid
        Object.assign(data, _ref.getQuery())
        try {
          this.submitLoading = true
          const { message } = await saveDatasource(data)
          this.$snackbar.success(message)
          this.show = false
          this.initPage()
        } catch (error) {
          this.$snackbar.error(error)
        } finally {
          this.submitLoading = false
        }
      }
    },
    // table 关联操作 新增
    handleAdd () {
      this.show = true
      this.itemData = {}
    },
    // table 关联操作 编辑
    handleEdit (item) {
      this.itemData = item
      this.show = true
    },
    // table 关联操作 删除
    handleDelete (uid) {
      // if (!ids.length) return this.$snackbar.warning('请选择删除项')
      this.$confirm('提示', '是否确定删除该选项').then(async () => {
        try {
          await deleteDatasource({ uid })
          this.$snackbar.success('删除成功')
          this.initPage()
        } catch (error) {
          this.$snackbar.error(error)
        }
      })
    },
    // table 关联操作 切换分页
    pageHandleChange (index) {
      this.pageInfo.current = index
      this.initPage()
    },
    handleSort (val) {
      this.orders = val
      this.initPage()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
