<template>
  <div class="content">
    <el-card shadow="never">
      <div class="project-name">
        <!--<p>{{ $route.matched[1].meta.title }}</p> -->
      </div>
      <el-form ref="searchForm" :model="searchForm" @submit.native.prevent>
        <div class="search-header">
          <div class="tool-common-wrap">
            <div class="search-wrap">
              <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="quickSearch" />
            </div>
            <div class="btn-wrap">
              <el-button type="primary" @click="areaHandle('add')">添加</el-button>
              <el-button type="primary" @click="rest()"> 刷新</el-button>
            </div>
          </div>
        </div>
      </el-form>
      <el-table
        :data="tableList"
        v-loading="tableLoading"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        style="width: 100%; margin: 15px 0">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="名称" show-overflow-tooltip>
          <template slot-scope="scope">
              <el-link class="table-link" @click.native.prevent="areaHandle('edit', scope.row)"
              :type="currentRow && currentRow.id && scope.row.id && currentRow.id === scope.row.id ? 'primary' : '' "  >{{scope.row.name}}</el-link>
              </template>
        </el-table-column>
        <el-table-column prop="serverNum" label="节点数量" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span> {{ formatStatus(scope.row.status) }} </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-edit-outline"
                @click.native.prevent="areaHandle('edit', scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-delete"
                @click.native.prevent="handleDelete(scope.row)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="page.pageSize"
        :current-page.sync="page.pageNum"
        background
        @current-change="getData()"
        :total="page.total"></el-pagination>
    </el-card>
    <AreaAdd v-model="dialogVisible" :checkItem="checkItem" @confirm="getData" />
  </div>
</template>

<script>
import { queryDomaininfo, deleteDomaininfo } from '@/api/esb_service'
import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'// 列表操作需要依赖
import AreaAdd from './AreaAdd'
import QuickQuery from '@/components/quickquery/QuickQuery'

export default {
  mixins: [EsbServiceListMixins],
  components: {
    AreaAdd,
    QuickQuery
  },
  data() {
    return {
      // 高级搜索框
      searchForm: {},
      tableLoading: false,
      tableList: [],
      dialogVisible: false,
      checkItem: {},
      quickQueryKeys: ['name', 'description'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称、描述'
    }
  },
  computed: {},
  created() {},
  methods: {
    async getData() {
      this.tableLoading = true
      queryDomaininfo({
        logic: this.logic,
        filters: this.filters,
        ...this.sort,
        ...this.page
      }).then((rt) => {
        this.tableList = rt.data.list || []
        this.page.total = rt.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    formatStatus(status) {
      const STATUS = {
        '1': '运行中',
        '2': '异常',
        '3': '未知',
        '4': '部分异常'
      }
      return STATUS[status] || ''
    },
    areaHandle(type, item) {
      this.checkItem = type === 'edit' ? Object.assign({}, item) : {}
      this.dialogVisible = true
    },
    handleDelete(item) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        deleteDomaininfo(item.id).then((rt) => {
          this.tableLoading = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getData()
        }).catch(e => {
          this.tableLoading = false
          this.$message({
            message: '已绑定代理服务，删除失败',
            type: 'error'
          })
        })
      }).catch(() => {
      })
    },
    quickQueryData() {
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.content{
  padding: 10px 10px;
}
</style>
