index.vue 5.82 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="12">
        <el-table ref="seletingTable" class="table hightline" :data="selectingData" height="464px" @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" label="商户名称" 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="12">
        <el-table :data="selectedData" class="table hightline" height="464px">
          <el-table-column fixed="left" width="5">
            <template slot-scope="scope">
              <span :scope="scope"></span>
            </template>
          </el-table-column>
          <el-table-column prop="merchantName" label="商户名称" align="center"></el-table-column>
          <el-table-column label="删除" align="center" width="70">
            <template slot-scope="scope">
              <el-button style="padding: 0;" class="el-icon-delete" @click="handleDelete(scope.row)" type="text"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </GLDialog>
</template>

<script>
export default {
  name: 'relateStoreDialog',
  data() {
    return {
      selectedData: [],
      selectingData: [],
      // needTip: ['editProduct'],
      // 分页
      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.selectedData.forEach(item => {
      item.merchantNo = item.merchantId ? item.merchantId + '' : item.merchantNo
    })
    this.getSelectingList()
  },

  methods: {
    selectable(row, index) {
      var isSelect = row.hasOwnProperty('beServed') ? !row.beServed : true
      return isSelect
    },
    recoverySelection() {
      var exitIds = this.selectedData.map(item => item.merchantNo + '')
      this.selectingData.forEach(item => {
        if (exitIds.includes(item.merchantNo + '')) {
          this.$refs.seletingTable.toggleRowSelection(item, true)
        } else {
          this.$refs.seletingTable.toggleRowSelection(item, false)
        }
      })
    },
    getSelectingList() {
      this.storeApi({
        ...this.storeSearchForm,
        pageNum: this.pageNum,
        currentPage: this.pageNum,
        pageSize: this.hasPage ? this.pageSize : 99999
        // pageSize: this.pageSize
      }).then(res => {
        this.selectingData = res.list
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    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 => {
        let isExit = this.selectedData.findIndex(subItem => +subItem.merchantNo === +item.merchantNo)
        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.merchantNo !== +row.merchantNo)
      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>
.relateStore .table {
  margin: 0 auto 20px !important;
  width: 98%;
}
/* .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>