appliedGoodsDialog.vue 5.63 KB
<!-- 应用商品弹窗 -->
<template>
  <d2-dialog
    :title="title"
    :is-visible.sync="appliedGoodsVisible"
    width="50%"
    btn-ok-label="确定"
    @confirmDialog="handleAppliedGoodsConfirm"
    @closeDialog="handleAppliedGoodsClose"
    class="appliedGoods">
    <slot />
    <el-row class="tableCon">
      <div class="tableBox">
        <el-table
          ref="seletingTable"
          :data="selectingData"
          height="464px"
          class="table"
          border
          @select="handleChange"
          @select-all="handleAll">
          <el-table-column type="selection"
                           width="55"
                           align="center" />
          <el-table-column prop="upgradeGoodsName" label="商品名称" align="center">
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total" />
      </div>
      <el-table
        class="tableBox table"
        :data="selectedData"
        border
        height="464px">
        <el-table-column prop="upgradeGoodsName" label="商品名称" align="center">
          <template slot-scope="scope">
            <span :class="{'gray': scope.row.merchantGoodsStatus != 1}">{{ scope.row.upgradeGoodsName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="删除" align="center" width="50px">
          <template slot-scope="scope">
            <el-button
              class="el-icon-delete"
              @click="handleDelete(scope.row)"
              type="text" />
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </d2-dialog>
</template>

<script>
import { deepClone } from '@/common/util'
export default {
  name: "RelateStoreDialog",
  data() {
    return {
      selectedData: [],
      selectingData: [],
      // 分页
      total: 0,
      currentPage: 1,
      pageSize: 20
    }
  },

  props: {
    title: {
      type: String,
      default: ""
    },
    appliedGoodsVisible: {
      type: Boolean,
      default: false
    },
    goodsApi: {
      type: Function,
      default: () => {}
    },
    goodsSearchForm: {
      type: Object,
      default: () => {}
    },
    addGoodsRow: {
      type: Object,
      default: () => {}
    }
  },

  mounted() {
    this.selectedData = deepClone(this.addGoodsRow?.goodsData)
    console.log(this.selectedData)
    this.getSelectingList()
  },

  methods: {
    recoverySelection() {
      var exitIds = this.selectedData.map((item) => item.merchantGoodsCode)
      this.selectingData.forEach((item) => {
        if (exitIds.includes(item.merchantGoodsCode)) {
          this.$nextTick(() => {
            this.$refs.seletingTable.toggleRowSelection(item, true)
          })
        }
      })
    },
    getSelectingList() {
      this.goodsApi({
        ...this.goodsSearchForm,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        merchantId: this.addGoodsRow?.merchantId
      }).then((res) => {
        res.list?.forEach(v => {
          v.merchantGoodsStatus = v.status
        })
        this.selectingData = res.list
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(
        (item) => item.merchantGoodsCode === row.merchantGoodsCode
      )
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(
          (item) => item.merchantGoodsCode !== row.merchantGoodsCode
        ))
    },
    handleAll(selection) {
      this.selectingData.forEach((item) => {
        var isExit = this.selectedData.findIndex(
          (subItem) => subItem.merchantGoodsCode === item.merchantGoodsCode
        )
        if (selection.length && isExit === -1) {
          this.selectedData.push(item)
        } else if (!selection.length && isExit !== -1) {
          this.selectedData.splice(isExit, 1)
        }
      })
    },
    handleDelete(row) {
      this.selectedData = this.selectedData.filter(
        (item) => item.merchantGoodsCode !== row.merchantGoodsCode
      )
      const obj = this.selectingData.find((v) => v.merchantGoodsCode == row.merchantGoodsCode)
      this.$nextTick(() => {
        this.$refs.seletingTable.toggleRowSelection(obj)
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSelectingList()
    },
    handleAppliedGoodsConfirm() {
      this.selectedData.forEach((val) => {
        if (!val.goodsData) val.goodsData = []
      })
      this.$emit("handleAppliedGoodsConfirm", this.selectedData)
    },
    handleAppliedGoodsClose() {
      this.$emit("handleAppliedGoodsClose")
    }
  }
}
</script>
<style lang="scss">
.appliedGoods .table {
  margin: 0 auto 20px !important;
  width: 98%;
}
.appliedGoods .tableCon {
  margin-top: 20px;
  display: flex;
  .tableBox {
    flex: 1;
    .gray{
      color: #66666642;
    }
  }
}
.service-store .appliedGoods .el-table .cell,
.d2-form .appliedGoods .el-table .cell {
  line-height: 26px !important;
}
.service-store .appliedGoods .el-table--medium td,
.d2-form .appliedGoods .el-table--medium td {
  padding: 6px 8px !important;
}
.appliedGoods .red {
  color: red;
}
.appliedGoods .tip-icon {
  position: absolute;
  display: inline-block;
  height: 10px;
  width: 10px;
  line-height: 10px;
  color: #ffffff;
  background-color: red;
  border: 1px solid red;
  border-radius: 50%;
  cursor: pointer;
  font-size: 6px;
  margin-left: 2px;
}
</style>