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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<template>
  <div class="edit-dropdown-box">
    <el-dialog
      :title="'编辑下拉框>' + editDropdownConfig.title"
      :visible.sync="editConfig.editVisible"
      :width="dialogWidth"
      :before-close="handleClose"
      :append-to-body="true"
    >
      <el-table :data="tableData" style="width: 100%" :header-cell-style="{ background: '#f4f8fe' }">
        <el-table-column label="名称" prop="name">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name" size="mini"></el-input>
          </template>
        </el-table-column>
        <!-- <el-table-column label="设为默认" prop="setDefault">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.setDefault"
              active-color="#2E68DB"
              inactive-color="#AEB9CA"
              size="mini"
              @change="setDefaultClick(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" width="110px">
          <template slot-scope="scope">
            <i
              v-if="scope.$index !== tableData.length - 1"
              class="el-icon-bottom common-style"
              title="下移"
              @click="moveDownClick(scope.$index, scope.row)"
            ></i>
            <i
              v-if="scope.$index !== 0"
              class="el-icon-top common-style"
              title="上移"
              @click="moveUpClick(scope.$index, scope.row)"
            ></i>
            <i class="el-icon-delete common-style" title="删除" @click="deleteClick(scope.$index, scope.row)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px">
        <el-button type="text" size="mini" @click="addDropdown">新增下拉框</el-button>
      </div>
      <div slot="footer">
        <el-button type="primary" size="small" @click="saveClick">保存</el-button>
        <el-button size="small" @click="editConfig.editVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import { getSupplierTypeList, updateSupplierType, updateIndustry, getIndustryList } from "@/api/supplierManage/supplier"
export default {
  name: "EditDropdownDialog",
  props: {
    editDropdownConfig: {
      type: Object,
      default: () => {
        return {
          editVisible: false,
          title: "",
          infomation: {
            name: "",
            color: "",
            setDefault: ""
          }
        }
      }
    }
  },
  components: {},
  computed: {},
  data() {
    return {
      dialogWidth: "20%",
      editConfig: this.editDropdownConfig,
      tableData: [],
      isName: false
    }
  },
  created() {
    this.setList()
  },
  methods: {
    setList() {
      if (this.editConfig.title === "供应商类型") {
        this.getSupplierTypeList()
      } else if (this.editConfig.title === "所属行业") {
        this.getIndustryList()
      }
    },
    // 供应商类型
    async getSupplierTypeList() {
      await getSupplierTypeList({
        page: 1,
        pageSize: 100
      }).then((res) => {
        console.log(res.data)
        this.tableData = res.data.list
      })
    },
    // 所属行业
    async getIndustryList() {
      await getIndustryList({
        page: 1,
        pageSize: 100
      }).then((res) => {
        console.log(res.data)
        this.tableData = res.data.list
      })
    },
    handleClose() {
      this.editConfig.editVisible = false
    },
    // 颜色选择
    colorClick(row) {
      console.log(row)
    },
    // 设为默认
    setDefaultClick(row) {
      if (row.setDefault) {
        if (this.tableData !== null) {
          this.tableData = this.tableData.map((item) => {
            if (item.name !== row.name) {
              item.setDefault = false
            }
            return item
          })
        }
      }
    },
    // 上移
    moveUpClick(index) {
      var that = this
      const upData = that.tableData[index - 1]
      that.tableData.splice(index - 1, 1)
      that.tableData.splice(index, 0, upData)
    },
    // 下移
    moveDownClick(index) {
      var that = this
      const downData = that.tableData[index + 1]
      that.tableData.splice(index + 1, 1)
      that.tableData.splice(index, 0, downData)
    },
    // 删除
    deleteClick(row) {
      let id = this.tableData.findIndex((item) => {
        if (item.name === row.name) {
          return true
        }
      })
      this.tableData.splice(id, 1)
    },
    // 新增下拉框
    addDropdown() {
      this.tableData.push({
        ID: 0,
        name: ""
      })
    },
    // 判断添加name是否为空
    determineNameEmpty(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].name.length === 0) {
          this.isName = true
          break
        } else {
          this.isName = false
        }
      }
    },
    async saveClick() {
      console.log(this.tableData)
      this.determineNameEmpty(this.tableData)
      if (this.isName) {
        this.$message.error("名称不能为空")
      } else {
        this.tableData.map((ite) => {
          ite.ID = 0
          return { ...ite }
        })
        if (this.editConfig.title === "供应商类型") {
          updateSupplierType({
            supplierTypes: this.tableData
          }).then((res) => {
            console.log(res)
            if (res.code === 200) {
              this.$message.success("编辑成功")
              this.$parent.getSupplierTypeList()
              this.handleClose()
            }
          })
        } else if (this.editConfig.title === "所属行业") {
          updateIndustry({
            industries: this.tableData
          }).then((res) => {
            console.log(res)
            if (res.code === 200) {
              this.$message.success("编辑成功")
              this.$parent.getIndustryList()
              this.handleClose()
            }
          })
        }
      }
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
// .edit-dropdown-box {
// }
.common-style {
  font-size: 16px;
  margin-left: 10px;
  cursor: pointer;
}
.first {
  margin-left: 0px;
}
</style>