<template>
  <div class="content" v-loading="tableLoading">
    <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="nodeHandle('add')">添加</el-button>
              <el-button type="primary" @click="rest()"> 刷新</el-button>
              <!-- <el-button type="primary" @click="nodeHandle('add')">添加</el-button>
              <el-button type="primary" @click="rest()"> 刷新</el-button> -->
            </div>
          </div>
          <!-- <div class="tool-common-wrap">
            <div class="btn-wrap">
              <span style="font-size:16px;padding-right:15px">批量操作:</span>
              <el-button type="primary" @click="deployAction('start')">启动</el-button>
              <el-button type="primary" @click="deployAction('pause')">暂停</el-button>
              <el-button type="primary" @click="deployAction('restore')">恢复</el-button>
              <el-button type="primary" @click="deployAction('stop')">停止</el-button>
              <el-button type="primary" @click="deployAction('reboot')">重启</el-button>
            </div>
          </div> -->
        </div>
      </el-form>
      <el-table
        :data="tableList"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        style="width: 100%; margin: 15px 0">
        <!-- @selection-change="handleSelectionChange" -->
        <!-- <el-table-column type="selection" width="55">
        </el-table-column> -->
        <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="nodeHandle('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="host" label="地址" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="port" label="端口" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="domainInfoDTO.name" 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="nodeHandle('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-video-play"
                @click.native.prevent="nodeOpe('start', 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-video-pause"
                @click.native.prevent="nodeOpe('stop', 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-remove-outline"
                @click.native.prevent="nodeOpe('stopNow', 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>
    <NodeAdd v-model="dialogVisible" :checkItem="checkItem" @confirm="getData" />
  </div>
</template>

<script>
import { queryServer, deleteServer, startService, stopService,
  suspendService, resumeService, restartService, nodeStart, nodeStop, nodeStopNow } from '@/api/esb_service'
import NodeAdd from './NodeAdd'
import QuickQuery from '@/components/quickquery/QuickQuery'
import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'// 列表操作需要依赖

export default {
  mixins: [EsbServiceListMixins],
  components: {
    NodeAdd,
    QuickQuery
  },
  data() {
    return {
      // 高级搜索框
      searchForm: {},
      tableLoading: false,
      tableList: [],
      dialogVisible: false,
      checkItem: {},
      quickQueryKeys: ['name'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称'
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
  },
  methods: {
    nodeOpe(name, row) {
      switch (name) {
        case 'start':
          nodeStart(row.id).then(res => {
            if (res.status === '200') {
              this.$message.success('启动成功')
            } else {
              this.$message.error(res.message)
            }
          }).catch()
          break
        case 'stop':
          nodeStop(row.id).then(res => {
            if (res.status === '200') {
              this.$message.success('启动成功')
            } else {
              this.$message.error(res.message)
            }
          }).catch()
          break
        case 'stopNow':
          nodeStopNow(row.id).then(res => {
            if (res.status === '200') {
              this.$message.success('启动成功')
            } else {
              this.$message.error(res.message)
            }
          }).catch()
          break
      }
    },
    deploySelectionChange(rows) {
      console.log(rows, '选中')
      this.deploymentMultipleSelection = rows
    },
    // 启动获取
    startServices(id1, id) {
      startService({
        'id': id1,
        'servers': id
      }).then(res => {
        this.statusTab = res.status === '500' ? '服务异常' : '正常'
        if (res.status === '200') {
          this.$message({
            message: '启动指定服务成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '启动指定服务失败',
            type: 'error'
          })
        }
      })
    },
    // 停止启动服务
    stopService(id1, id) {
      stopService({
        'id': id1,
        'servers': id
      }).then(res => {
        this.statusTab = res.status === '500' ? '服务异常' : '正常'
        if (res.status === '200') {
          this.$message({
            message: '停止启动服务成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '停止启动服务失败',
            type: 'error'
          })
        }
      })
    },
    // 部署信息恢复指定服务
    resumeService(id1, id) {
      resumeService({
        'id': id1,
        'servers': id
      }).then(res => {
        this.statusTab = res.status === '500' ? '服务异常' : '正常'
        if (res.status === '200') {
          this.$message({
            message: '恢复指定服务成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '恢复指定服务失败',
            type: 'error'
          })
        }
      })
    },
    // 部署信息暂停指定服务
    suspendService(id1, id) {
      suspendService({
        'id': id1,
        'servers': id
      }).then(res => {
        this.statusTab = res.status === '500' ? '服务异常' : '正常'
        if (res.status === '200') {
          this.$message({
            message: '暂停指定服务成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '暂停指定服务失败',
            type: 'error'
          })
        }
      })
    },
    // 部署信息重启指定服务
    restartService(id1, id) {
      restartService({
        'id': id1,
        'servers': id
      }).then(res => {
        this.statusTab = res.status === '500' ? '服务异常' : '正常'
        if (res.status === '200') {
          this.$message({
            message: '重启指定服务成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '重启指定服务失败',
            type: 'error'
          })
        }
      })
    },
    /* 部署信息的操作 -> 批量操作*/
    deployAction(actionType, singleItem) {
      if (this.deploymentMultipleSelection.length === 0) {
        this.$message.warning('请先选择节点！')
        return false
      }
      var str = ''
      this.deploymentMultipleSelection.forEach(function(e) {
        str += e.serverId + ','
      })
      str = str.substring(0, str.length - 1)
      this.id_list = str.split(',')
      switch (actionType) {
        // 启动
        case 'start':
          // this.startServices(this.deploymentMultipleSelection[0].serverId)
          this.startServices(this.formData.id, this.id_list)
          this.id_list = []
          break
          // 暂停
        case 'pause':
          this.suspendService(this.formData.id, this.id_list)
          this.id_list = []
          break
          //  恢复
        case 'restore':
          this.resumeService(this.formData.id, this.id_list)
          this.id_list = []
          break
          //  停止
        case 'stop':
          this.stopService(this.formData.id, this.id_list)
          this.id_list = []
          break
          //  重启
        case 'reboot':
          this.restartService(this.formData.id, this.id_list)
          this.id_list = []
          break
        default:
          break
      }
    },
    formatStatus(status) {
      const STATUS = {
        '1': '运行中',
        '2': '未运行',
        '3': '未运行',
        '4': '未运行'
        // '2': '异常',
        // '3': '未知',
        // '4': '部分异常'
      }
      return STATUS[status] || ''
    },
    async getData() {
      this.tableLoading = true
      queryServer({
        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
      })
    },
    nodeHandle(type, item) {
      this.checkItem = type === 'edit' ? Object.assign({}, item) : {}
      this.dialogVisible = true
    },
    handleDelete(item) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        deleteServer(item.id).then((rt) => {
          this.tableLoading = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getData()
        }).catch(e => {
          this.tableLoading = false
        })
      }).catch(() => {
      })
    },
    quickQueryData() {
      this.getData()
    }
  }
}
</script>

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