File size: 5,778 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
<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.scripts.list.length !== 0"
              placement="left"
              :width="350"
              trigger="hover"
            >
              <template #reference>
                <u class="script_count">{{ row.scripts.count }}</u>
              </template>
              <ul>
                <li v-for="item in row.scripts.list" :key="item.id">
                  <span>{{ item.name }}</span>
                  -
                  <span>{{ item.description }}</span>
                </li>
              </ul>
            </el-popover>
            <u v-else class="script_count">0</u>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="160px">
          <template #default="{ row }">
            <template v-if="row.id !== 'builtin'">
              <el-button type="primary" @click="handleChange(row)">修改</el-button>
              <el-button
                v-show="row.id !== 'default'"
                type="danger"
                @click="deleteGroup(row)"
              >
                删除
              </el-button>
            </template>
            <template v-else>
              <span>--</span>
            </template>
          </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"
          label-width="100px"
        >
          <el-form-item label="分组名称" prop="name">
            <el-input v-model="groupForm.name" />
          </el-form-item>
          <el-form-item label="序号" prop="index">
            <el-input v-model.number="groupForm.index" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="groupFormVisible = false">取消</el-button>
            <el-button type="primary" @click="updateForm">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </el-dialog>
</template>

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

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

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

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

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

const loading = ref(false)
const groupFormVisible = ref(false)
const isModify = ref(false)
const updateFormRef = ref()

const groupForm = ref({
  id: null,
  name: '',
  index: 0
})

const rules = {
  name: [{ required: true, message: '请输入分组名称', trigger: 'blur' },],
  index: [{ type: 'number', message: '序号必须为数字值' },]
}

const scriptList = computed(() => $store.scriptList)
const groupList = computed(() => $store.scriptGroupList)

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

const addGroup = () => {
  groupForm.value.id = null
  groupForm.value.index = groupList.value.reduce((max, group) =>
    Math.max(max, Number(group.index) || 0), 0) + 1
  groupFormVisible.value = true
  isModify.value = false
}

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

const updateForm = () => {
  updateFormRef.value.validate()
    .then(async () => {
      const { id, index, name } = groupForm.value
      if (isModify.value) {
        await $api.updateScriptGroup(id, { index, name })
      } else {
        await $api.addScriptGroup({ index, name })
      }
      await $store.getScriptGroupList()
      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.deleteScriptGroup(id)
      await $store.getScriptList()
      await $store.getScriptGroupList()
      emit('group-deleted', id)
      $message.success('success')
    })
}
</script>

<style lang="scss" scoped>
.group_container {
  padding: 0 20px 20px 20px;

  .header {
    display: flex;
    align-items: center;
    justify-content: end;
  }
}

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