File size: 5,808 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<template>
  <el-dialog
    v-model="visible"
    width="600px"
    :top="isMobile() ? '45px' : '15vh'"
    :append-to-body="false"
    title="实例分组管理"
    :close-on-click-modal="false"
  >
    <div class="group_container">
      <div class="header">
        <el-button type="primary" @click="addGroup">添加分组</el-button>
      </div>
      <el-table v-loading="loading" :data="list">
        <el-table-column prop="index" label="序号" />
        <el-table-column prop="name" label="分组名称" />
        <el-table-column label="关联实例数量" min-width="115px">
          <template #default="{ row }">
            <el-popover
              v-if="row.hosts.list.length !== 0"
              placement="left"
              :width="350"
              trigger="hover"
            >
              <template #reference>
                <u class="host_count">{{ row.hosts.count }}</u>
              </template>
              <ul>
                <li v-for="item in row.hosts.list" :key="item.host">
                  <span>{{ item.host }}</span>
                  -
                  <span>{{ item.name }}</span>
                </li>
              </ul>
            </el-popover>
            <u v-else class="host_count">0</u>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="160px">
          <template #default="{ row }">
            <el-button type="primary" @click="handleChange(row)">修改</el-button>
            <el-button v-show="row.id !== 'default'" type="danger" @click="deleteGroup(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        v-model="groupFormVisible"
        width="600px"
        top="150px"
        :title="isModify ? '修改分组' : '添加分组'"
        :close-on-click-modal="false"
        @close="clearFormInfo"
      >
        <el-form
          ref="updateFormRef"
          :model="groupForm"
          :rules="rules"
          :hide-required-asterisk="true"
          label-suffix=":"
          label-width="100px"
          :show-message="false"
        >
          <el-form-item label="分组名称" prop="name">
            <el-input
              v-model="groupForm.name"
              clearable
              autofocus
              placeholder=""
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="分组序号" prop="index">
            <el-input
              v-model.number="groupForm.index"
              clearable
              placeholder="用于分组排序"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span>
            <el-button @click="groupFormVisible = false">关闭</el-button>
            <el-button type="primary" @click="updateForm">{{ isModify ? '修改' : '添加' }}</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </el-dialog>
</template>

<script setup>
import { isMobile } from '@/utils'
import { ref, reactive, computed, nextTick, getCurrentInstance } from 'vue'

const { proxy: { $api, $message, $messageBox, $store } } = getCurrentInstance()

const props = defineProps({
  show: {
    required: true,
    type: Boolean
  }
})

const emit = defineEmits(['update:show',])

const visible = computed({
  get: () => props.show,
  set: (newVal) => emit('update:show', newVal)
})

const loading = ref(false)
const groupFormVisible = ref(false)
let isModify = ref(false)
const groupForm = reactive({
  id: null,
  name: '',
  index: ''
})

const rules = computed(() => {
  return {
    name: { required: true, message: '需输入分组名称', trigger: 'change' },
    index: { required: true, type: 'number', message: '需输入数字', trigger: 'change' }
  }
})

const updateFormRef = ref(null)

let groupList = computed(() => $store.groupList || [])
const hostList = computed(() => $store.hostList)

const list = computed(() => {
  return groupList.value.map(item => {
    const hosts = hostList.value.reduce((prev, next) => {
      if (next.group === item.id) {
        prev.count++
        prev.list.push(next)
      }
      return prev
    }, { count: 0, list: [] })
    return { ...item, hosts }
  })
})

let addGroup = () => {
  groupForm.id = null
  const maxIndex = Math.max(...groupList.value.map(item => item.index), 0)
  groupForm.index = maxIndex + 1
  groupFormVisible.value = true
  isModify.value = false
}

const handleChange = (row) => {
  Object.assign(groupForm, { ...row })
  groupFormVisible.value = true
  isModify.value = true
}

const updateForm = () => {
  updateFormRef.value.validate()
    .then(async () => {
      const { id, index, name } = groupForm
      if (isModify.value) {
        await $api.updateGroup(id, { index, name })
      } else {
        await $api.addGroup({ index, name })
      }
      await $store.getGroupList()
      groupFormVisible.value = false
      $message.success('success')
    })
}

const clearFormInfo = () => {
  nextTick(() => updateFormRef.value.resetFields())
}

const deleteGroup = ({ id, name }) => {
  $messageBox.confirm(`确认删除分组:${ name } (分组下实例将移动至默认分组)`, 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await $api.deleteGroup(id)
      await $store.getHostList()
      await $store.getGroupList()
      $message.success('success')
    })
}

</script>

<style lang="scss" scoped>
.group_container {
  .header {
    display: flex;
    align-items: center;
    justify-content: end;
  }
}

.host_count {
  display: block;
  width: 100px;
  text-align: center;
  font-size: 15px;
  color: #87cf63;
  cursor: pointer;
}
</style>