index.vue 7.48 KB
<template>
  <d2-dialog class="relateStore" :appendToBody="true" :is-visible="dialogVisible" title="选择门店" width="50%"
    @confirmDialog="handleAddConfirm" @closeDialog="handleClose">
    <!-- <div class="tableBox"> -->
    <div>
      <span>门店名称: </span>
      <el-input v-model="realSearchForm.merchantName" placeholder="搜索门店名称" maxlength="20" clearable size="small"
        style="width: 300px; margin-right: 12px" />
      <el-button size="small" type="primary" @click="searchList">
        查询
      </el-button>
    </div>
    <!-- <d2-table ref="merchantTable" :search-form="realSearchForm" :table-params="tableParams" :get-list-api="getListApi"
        :before-search-fn="beforeSearchFn" @setTableFinishFun="setTableFinishFun">
      </d2-table> -->
    <el-row class="tableCon">
      <div class="tableBox">
        <el-table ref="seletingTable" :data="selectingData" height="365px" class="table" border @select="handleChange"
          @select-all="handleAll">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column prop="merchantName" 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="365px">
        <el-table-column prop="merchantName" label="门店名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.merchantName }}</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>
    <!-- </div> -->
  </d2-dialog>
</template>

<script>
import { deepClone } from "@/common/util"
import {
  getAllMerchantList,
  getMerchantListByScene
} from "@api/contentManager"
export default {
  name: "SelectGoodsTypeDialog",
  props: {
    config: {
      type: Object,
      require: true
    },
    dialogVisible: {
      type: Boolean,
      default: false
    },
    selectedRecord: {
      type: Array,
      default: () => []
    },
    selectedList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      getListApi: getAllMerchantList,
      selectedData: [],
      selectingData: [],
      // 分页
      total: 0,
      currentPage: 1,
      pageSize: 10,
      realSearchForm: {
        merchantName: ""
      },
      tableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: "list",
        mutiSelectUniqueKey: "id",
        mutiSelectName: "merchantIndex",
        tableColumns: [
          {
            label: "门店名称",
            prop: "merchantName"
          }
        ]
      }
    }
  },
  mounted() {
    this.selectedData = deepClone(this.selectedList)
    this.getSelectingList()
  },
  methods: {
    recoverySelection() {
      var exitIds = this.selectedData.map((item) => item.merchantId)
      this.selectingData.forEach((item) => {
        exitIds.includes(item.merchantId) &&
          this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    handleAll(selection) {
      this.selectingData.forEach((item) => {
        var isExit = this.selectedData.findIndex(
          (subItem) => subItem.merchantId === item.merchantId
        )
        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.merchantId !== row.merchantId
      )
      const obj = this.selectingData.find((v) => v.merchantId == row.merchantId)
      this.$refs.seletingTable.toggleRowSelection(obj)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSelectingList()
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(
        (item) => item.merchantId === row.merchantId
      )
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(
          (item) => item.merchantId !== row.merchantId
        ))
    },
    getSelectingList() {
      getMerchantListByScene({
        ...this.realSearchForm,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then((res) => {
        // res?.list?.forEach((val) => {
        //   // 账户中心门店返回商户号字段是merchantNo
        //   if (val.merchantNo) val.merchantId = Number(val.merchantNo)
        //   val?.sceneList?.forEach((subVal) => {
        //     if (subVal.scene == this.selectedScene) {
        //       val.yqMerchantId = subVal?.yqMerchant?.merchantId || "-"
        //       val.yqMerchantName = subVal?.yqMerchant?.merchantName || "-"
        //     }
        //   })
        // })
        this.selectingData = res.list
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    // recoverySelection(list) {
    //   var exitIds = this.selectedList.map((item) => +item.merchantId)
    //   console.log(exitIds)
    //   list.forEach((item) => {
    //     console.log(item.merchantId, "item.merchantId")
    //     exitIds.includes(item.merchantId) &&
    //       this.$refs.merchantTable.$refs.merchantIndexMutiTable.toggleRowSelection(
    //         item,
    //         true
    //       )
    //   })
    // },
    // beforeSearchFn() {
    //   return new Promise((resolve) => {
    //     if (this.config.scene == 0 || this.config.scene == 4) {
    //       this.getListApi = getAllMerchantList
    //     } else {
    //       this.getListApi = getMerchantListByScene
    //       this.realSearchForm.scene = this.config.scene
    //     }
    //     resolve()
    //   })
    // },
    setTableFinishFun(data) {
      this.$nextTick(() => {
        this.recoverySelection(data)
      })
    },
    searchList() {
      // this.page = {
      //   pageSize: this.pageSize,
      //   currentPage: 1
      // }
      this.currentPage = 1
      this.getSelectingList()
      // this.$refs?.merchantTable?.refreshTable(true, true)
    },
    handleAddConfirm() {
      const selectItemList = this.selectedData
      if (selectItemList.length) {
        this.$emit("confirmDialogFn", selectItemList)
      } else {
        this.$message.error("请勾选门店")
      }
    },
    handleClose() {
      this.$emit("closeDialogFn")
    }
  }
}
</script>

<style lang="scss" scoped>
.table {
  margin: 0 auto 20px !important;
  width: 98%;
}

.tableCon {
  margin-top: 20px;
  display: flex;

  .tableBox {
    flex: 1;
  }
}

.service-store .relateStore .el-table .cell,
.d2-form .relateStore .el-table .cell {
  line-height: 26px !important;
}

.service-store .relateStore .el-table--medium td,
.d2-form .relateStore .el-table--medium td {
  padding: 6px 8px !important;
}

.relateStore .red {
  color: red;
}

.relateStore .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>