import breadcrumbComponent from '@/components/breadcrumb.vue'
import tableComponent from '@/components/table.vue'
import list from '@/mixins/list'
import routerUserMenu from '@/mixins/router-user-menu'

export default {
  name: 'DatabaseList',
  components: {breadcrumbComponent, tableComponent},
  mixins: [list, routerUserMenu],
  data () {
    return {
      searchForm: {
        resourceName: '',
        resourceType: '',
        pageSize: 10,
        page: 1
      },
      typeOptions: [],
      // 列表数据
      tableData: {
        data: [],
        total: 0
      },
      // 列表设置
      tableOption: {
        // 是否显示序号
        index: true,
        // 是否显示勾选框
        select: false,
        // 默认排序
        defaultSort: {prop: 'updateTime', order: 'descending'},
        // 列
        tableLabel: [
          {prop: 'resourceName', title: '资源名称', width: '100'},
          {prop: 'resourceTypeName', title: '资源类型', width: '100'},
          {prop: 'databaseTypeName', title: '仓库类型', width: '100'},
          {prop: 'ip', title: 'IP', width: '100'},
          {prop: 'resourcePort', title: '端口', width: '100'},
          {prop: 'updateUserName', title: '修改人', width: '100'},
          {prop: 'updateTime', title: '修改时间', width: '120', sort: true}
        ],
        // 操作
        tableOperate: {
          width: '150',
          buttons: [
            {label: '刷新', methods: 'refresh', color: 'link'},
            {label: '修改', methods: 'edit', color: 'safe'},
            {label: '删除', methods: 'del', color: 'warn'}
          ]
        },
      },
    }
  },
  created () {
  },
  mounted () {
    this.listResourceType()
    // this.getList()
  },
  methods: {

    /* 编辑功能 */
    edit (obj) {
      this.go('DatabaseEdit', {id: obj.id})
    },

    /* 刷新功能 */
    refresh (obj) {
      let params = {id: obj.id}
      this.$api.database.refreshDB(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    // 执行删除
    delAction (obj) {
      let params={id: obj.id}
      this.$api.database.removeDatabaseById(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    // 删除
    del(obj){
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delAction(obj)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /* 加载资源类型下拉列表 */
    listResourceType () {
      this.$api.database.listResourceType()
        .then(res => {
          this.typeOptions = res.data
        })
    },

    /* 获取数据库列表信息 */
    getList () {
      let params = this.searchForm
      this.$api.database.listDatabase(params)
        .then(res => {
          this.tableData.data = res.data
          this.tableData.total = res.totalNumber
        })
    },
  }
}
