relateStoreDialog.vue 6.96 KB
<!-- 关联门店弹窗 -->
<template>
    <GLDialog bigTitle="选择门店" mode="big" :visible.sync="relateStoreVisible" okText="关联" :before-close="handleRelateStoreClose" @onOk="handleRelateStoreConfirm" @onCancel="handleRelateStoreClose" class="relateStore">
      <slot></slot>
      <el-row>
        <el-col :span="16">
          <div class="title">门店列表</div>
          <el-table ref="seletingTable" class="table hightline" :data="selectingData" height="460px" @select="handleChange" @select-all="handleAll">
            <el-table-column fixed="left" width="5">
              <template slot-scope="scope">
                <span :scope="scope"></span>
              </template>
            </el-table-column>
            <el-table-column type="selection" width="55" align="center" :selectable="selectable"></el-table-column>
            <el-table-column prop="merchantName"  width="300" label="门店名称" align="center"></el-table-column>
            <el-table-column prop="merchantId" label="商户号" width="300" align="center"></el-table-column>
          </el-table>
          <el-pagination v-if="hasPage" small background @current-change="handleCurrentChange" :current-page.sync="pageNum" :page-size="pageSize" layout="total, prev, pager, next" :total="total"></el-pagination>
        </el-col>
        <el-col :span="8">
          <div class="title mar_l10">已选门店</div>
          <div class="selected_box">
            <div class="box_top">
              <span>已选</span>
              <el-button type="text" @click="handelClearStore">清空</el-button>
            </div>
            <div class="box_content">
              <div class="select_item" v-for="(subItem, subIndex) in selectedData" :key="subIndex">
                <div class="text_value">{{ subItem.merchantName }}({{subItem.merchantId}})</div>
                <div class="select_clear" @click="handleDelete(subItem)"><i class="el-icon-error"></i></div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </GLDialog>
  </template>
  
  <script>
  export default {
    name: 'relateStoreDialog',
    data() {
      return {
        selectedData: [],
        selectingData: [],
        // 分页
        total: 0,
        pageNum: 1,
        pageSize: 20
      }
    },
  
    props: {
      relateStoreVisible: {
        type: Boolean,
        default: false
      },
      hasPage: {
        type: Boolean,
        default: true
      },
      // 页面来源
      // sourcePage: {
      //   type: String,
      //   default: ''
      // },
      storeApi: {
        type: Function,
        default: () => { }
      },
      storeSelected: {
        type: Array,
        default: () => []
      },
      storeSearchForm: {
        type: Object,
        default: () => { }
      }
      // 去掉表格中某些字段
      // noTableField: {
      //   type: Array,
      //   default: () => []
      // }
    },
  
    mounted() {
      this.selectedData = this.storeSelected?this.storeSelected:[]
      this.selectedData&&this.selectedData.forEach(item => {
        item.merchantId = item.merchantId ? item.merchantId + '' : item.merchantId
      })
      this.getSelectingList()
    },
  
    methods: {
      selectable(row, index) {
        var isSelect = row.hasOwnProperty('beServed') ? !row.beServed : true
        return isSelect
      },
      recoverySelection() {
        var exitIds = this.selectedData.map(item => item.merchantId + '')
        this.selectingData.forEach(item => {
          if (exitIds.includes(item.merchantId + '')) {
            this.$refs.seletingTable.toggleRowSelection(item, true)
          } else {
            this.$refs.seletingTable.toggleRowSelection(item, false)
          }
        })
      },
      getSelectingList() {
        this.storeApi({
          ...this.storeSearchForm,
          currentPage: this.pageNum,
          pageSize: this.hasPage ? this.pageSize : 99999,
          scene: 1,
          partnerMerchantId: this.$globalState?.session?.merchantId
        }).then(res => {
          this.selectingData = res.list || []
          this.total = res.total
          this.$nextTick(() => {
            this.recoverySelection()
          })
        })
      },
      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))
      },
      handleAll(selection) {
        this.selectingData?.forEach(item => {
          let isExit = this.selectedData.findIndex(subItem => +subItem.merchantId === +item.merchantId)
          var isServed = item.hasOwnProperty('beServed') ? item.beServed : false
          if (selection.length && isExit === -1 && !isServed) {
            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)
        this.recoverySelection()
      },
      handelClearStore() {
        this.selectedData = []
        this.recoverySelection()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.getSelectingList()
      },
      handleRelateStoreConfirm() {
        if (!this.selectedData.length) {
          this.$message({
            message: '请选择关联门店',
            type: 'warning',
            showClose: true,
            customClass: 'message-Z-zinde'
          })
          return true
        }
        this.$emit('handleRelateStoreConfirm', this.selectedData)
      },
      handleRelateStoreClose() {
        this.$emit('handleRelateStoreClose')
      }
    }
  }
  </script>
  <style lang="scss" scoped>
  .relateStore{
    .title{
      font-size: 16px;
      margin-bottom: 10px;
    }
    .mar_l10{
      margin-left: 10px;
    }
    .selected_box{
      margin-left: 10px;
      border: 1px solid #E5E5E5;
      height: 460px;
      .box_top{
        font-size: 16px;
        padding: 11px 26px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #E5E5E5;
      }
      .box_content{
        padding: 12px 8px;
        overflow-y: scroll;
        height: calc(100% - 40px);
        box-sizing: border-box;
        .select_item{
          display: flex;
          align-items: center;
          justify-content: space-between;
          height: 32px;
          padding: 0 8px;
          cursor: pointer;
          .text_value{
            width: calc(100% - 50px);
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          .select_clear{
            width: 12px;
            height: 12px;
            i{
              color: #A6A6A6;
            }
          }
        }
        .select_item:hover{
          background: #F2F1FF;
        }
      }
    }
  }
  </style>