storeDialog.vue 6.46 KB
<!-- 关联门店弹窗 -->
<template>
  <d2-dialog
    title="选择门店"
    :is-visible.sync="relateStoreVisible"
    width="70%"
    btn-ok-label="确定"
    @confirmDialog="handleRelateStoreConfirm"
    @closeDialog="handleRelateStoreClose"
    class="relateStore"
  >
    <el-form :model="storeSearchForm" ref="relate" :inline="true">
      <el-form-item label="门店名称:">
        <el-input
          v-model.trim="storeSearchForm.brandName"
          placeholder="门店名称"
          max-length="20"
          @input="handleStoreInput"
          @keydown.native="handleStoreInput"
          clearable
        />
      </el-form-item>
      <el-button type="primary" @click="handleSearchStore">搜索</el-button>
    </el-form>
    <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="merchantNo" label="门店商户号" align="center"></el-table-column>
          <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="464px"
      >
        <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>
  </d2-dialog>
</template>

<script>
import { debounce } from "../../../index/debounce"
import { deepClone } from "@/common/util"
import { getCarBrandIds } from "@api/contentManager"
export default {
  name: "RelateStoreDialog",
  data() {
    return {
      selectedData: [],
      selectingData: [],
      // 分页
      total: 0,
      currentPage: 1,
      pageSize: 20,
      storeSearchForm: {},
      allData: []
    }
  },

  props: {
    relateStoreVisible: {
      type: Boolean,
      default: false
    },
    storeSelected: {
      type: Array,
      default: () => []
    },
    brandData: {
      type: Array,
      default: () => []
    }
  },
  created() {
    // 创建防抖版本的搜索函数
    this.debouncedSearch = debounce(this.handleSearchStore, 300);
  },
  mounted() {
    console.log(this.brandData)
    this.allData = deepClone(this.storeSelected)
    this.selectedData = this.storeSelected
    this.getSelectingList()
  },

  methods: {
    recoverySelection() {
      var exitIds = this.selectedData.map((item) => item.merchantNo)
      this.selectingData.forEach((item) => {
        exitIds.includes(item.merchantNo) &&
          this.$refs.seletingTable &&
          this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    getSelectingList() {
      // const _first = this.brandData.filter(item => item.children).map(item => item.id)
      const _second = this.brandData.filter(item => !item.children).map(item => item.id)
      const params = {
        // firstBrandIdList: _first,
        secBrandIdList: _second,
        scene: 0
      }
      getCarBrandIds(params).then((res) => {
        this.allData = deepClone(res)
        this.selectingData = res
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    handleStoreInput(e) {
      this.debouncedSearch()
    },
    handleSearchStore() {
      console.log(this.storeSearchForm.brandName)
      const {brandName} = this.storeSearchForm
      if(brandName) {
        this.selectingData = this.selectingData.filter(item => {
        return item.merchantName.includes(brandName)
      })
      } else {
        this.selectingData = deepClone(this.allData)
      }
      
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find((item) => item.merchantNo === row.merchantNo)
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(
            (item) => item.merchantNo !== row.merchantNo
          ))
    },
    handleAll(selection) {
      this.selectingData.forEach((item) => {
        var isExit = this.selectedData.findIndex(
          (subItem) => subItem.merchantNo === item.merchantNo
        )
        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.merchantNo !== row.merchantNo)
      const obj = this.selectingData.find((v) => v.merchantNo == row.merchantNo)
      this.$refs.seletingTable.toggleRowSelection(obj)
    },
    // handleCurrentChange(val) {
    //   this.currentPage = val
    //   this.getSelectingList()
    // },
    handleRelateStoreConfirm() {
      this.$emit("handleRelateStoreConfirm", this.selectedData)
    },
    handleRelateStoreClose() {
      this.$emit("handleRelateStoreClose")
    }
  }
}
</script>
<style lang="scss">
.relateStore .table {
  margin: 0 auto 20px !important;
  width: 98%;
}
.relateStore .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>