fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div>
    <filter-dialog ref="table" :show.sync="isShow" :title="title" :tableData="tableData"
                   :total="total" :form="listQuery" :formLabel="formLabel" :loading="loading"
                   dialogWidth="70%" :multipleSelected="true" :closeBtn="false"
                   @page-info-change="handPageInfoChange" :selectable="selecetInit"
                   @selected="handleSelected" @close="handleDialogClose" :dataKey="'spuNum'"
                   :isClearCheckItem="false">
      <template slot="condition">
        <el-button type="primary" size="mini" @click="handleFilter">查询</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="商品主编码" prop="spuNum" width="220px" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="商品名称" prop="spuName" min-width="220px" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="规格型号" prop="spec" width="140px">
        </el-table-column>
        <el-table-column label="剩余库存" prop="stock" width="140px" show-overflow-tooltip>
        </el-table-column>
      </template>
    </filter-dialog>
  </div>
</template>
 
<script>
import ChannelInventoryApi from '@/api/stockManagement/channelInventory'
import filterDialog from '@/components/filterDialog/filterDialog.vue'
export default {
  name: "chooseProductsDialog",
  components: { filterDialog },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    channelId: {
      type: String,
      default: '',
    },
    selectedTable: {
      type: Array,
      default: function () {
        return []
      }
    },
  },
  data() {
    return {
      isShow: false,
      listQuery: {
        spuNum: null,//商品主编码
      },
      formLabel: [
        {
          model: 'spuNum',
          label: '商品主编码',
          type: 'input',
          labelWidth: '100px',
          sm: 8,
        }
      ],
      tableData: [],
      selectedData: [],
      selectedRows: null,
      total: 0,
      loading: false,
    }
  },
  watch: {
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    dialogVisible: function (newShow) {
      this.selectedRows = JSON.parse(JSON.stringify(this.selectedTable))
      this.isShow = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    isShow: function (newDialogShow, oldDialogShow) {
      this.$emit('update:dialogVisible', newDialogShow)
      if (!newDialogShow) {
        this.initData();
      }
    },
  },
  methods: {
    initData() {
      this.tableData = [];
      this.selectedData = [];
      this.total = 0;
      this.listQuery.spuNum = null;
    },
    /**
      * 分页信息改变时,列表查询
      */
    handPageInfoChange(pageInfo) {
      this.queryList(pageInfo)
    },
    /**
    * 查询列表
    */
    async queryList(pageInfo) {
      try {
        let params = {
          current: pageInfo.pageNum,
          size: pageInfo.pageSize,
          channel: this.channelId,
          spuNum: this.listQuery.spuNum
        }
        this.loading = true;
        const res = await ChannelInventoryApi.getList(params)
        if (res.code === '200') {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false;
          this.setCheckedSpuNumId();
        } else {
          this.loading = false;
        }
      } catch (error) {
      }
    },
    /**
     * 回显选择对应的商品行数据
     */
    setCheckedSpuNumId() {
      if (this.tableData && this.tableData.length) {
        let spuNums = this.selectedRows.map((item) => item.spuNum);
        this.tableData.forEach(item1 => {
          if (spuNums.indexOf(item1.spuNum) >= 0) {
            this.$refs.table.getTableNode().toggleRowSelection(item1, true);
          }
        })
      }
    },
    /**
     * 搜索条件变更处理
     */
    handleFilter() {
      this.$refs.table.changeCondition()
    },
    /**
      * 重置
      */
    resetQuery() {
      this.$refs.table.reloadCurrent()
    },
    /**
      * 关闭弹窗
      */
    handleDialogClose() {
      if (this.selectedRows) {
        this.$emit('hand-selected-row-data', this.selectedRows)
      }
    },
    /**
    *获取选中的
    */
    handleSelected(rows) {
      this.tableData.forEach((item1) => {
        //原有选中数据在当前页是否存在
        let _curTablePage = this.selectedRows.find((v) => v.spuNum === item1.spuNum);
        if (_curTablePage) {
          //存在
          const index = rows.find((v) => v.spuNum === item1.spuNum)
          //原有选中的数据在当前页存在但是未选中---删除当前数据
          if (!index) {
            let arr = [];
            this.selectedRows.forEach((v) => {
              if (v.spuNum != item1.spuNum) {
                arr.push(v)
              }
            })
            this.selectedRows = [...arr];
          }
        } else {
          //不存在
          if (rows.length) {
            //原有选中数据与新选中的数据进行合并
            rows.forEach((v1) => {
              let isE = this.selectedRows.find((v) => v.spuNum === v1.spuNum)
              if (!isE) {
                this.selectedRows = [...this.selectedRows, ...[v1]]
              }
            })
          }
        }
      })
    },
    /**
     * 初始化复选框
     */
    selecetInit(row, index) {
      if (row.stock === 0 || !row.stock) {
        return false // 不可勾选
      } else {
        return true // 可勾选
      }
    },
  }
}
</script>
 
<style>
</style>