serviceItemsDialog.vue 8.02 KB
<!-- 关联门店弹窗 -->
<template>
  <d2-dialog title="选择服务项目"
             :is-visible.sync="relateServiceVisible"
             width="70%"
             btn-ok-label="确定"
             @confirmDialog="handleRelateServiceConfirm"
             @closeDialog="handleRelateServiceClose"
             class="relateService">
    <slot />
    <el-row class="tableCon">
      <div class="tableBox">
        <el-table ref="seletingTable"
                  :data="selectingData"
                  height="464px"
                  class="table"
                  border
                  @select="handleChange">
          <el-table-column type="selection"
                           :selectable="selectable"
                           width="55"
                           align="center" />
          <el-table-column
            width="55"
            label="序号"
            type="index"
            align="center" />
          <el-table-column
            prop="serviceName"
            label="服务项目"
            :show-overflow-tooltip="true"
            width="190"
            align="center">
          </el-table-column>
          <el-table-column prop="serviceGrade"
                           label="等级"
                           align="center">
          </el-table-column>
          <el-table-column prop="servicePeriod"
                           label="年限"
                           align="center">
          </el-table-column>
          <el-table-column prop="standardSalesCost"
                           label="销售成本/元"
                           width="140"
                           align="center">
          </el-table-column>
          <el-table-column prop="serviceOverview"
                           width="210"
                           :show-overflow-tooltip="true"
                           label="描述"
                           align="center">
            <template slot-scope="scope">
              <span v-html="scope.row.serviceOverview" class="text_hide"></span>
            </template>
          </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>
      <div class="selectedBox">
        <div class="title">
          <div class="title_txt">已选</div>
          <div class="title_btn" @click="clearSelect">清空</div>
        </div>
        <div class="content">
          <div class="item" v-for="(subItem, subIdx) in selectedData" :key="subIdx">
            <div class="item_txt"><span>{{ subItem.uniqueValue }}</span></div>
            <div class="del_item" @click="delItem(subItem)"><i class="el-icon-error"></i></div>
          </div>
        </div>
      </div>
    </el-row>
  </d2-dialog>
</template>

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

  props: {
    relateServiceVisible: {
      type: Boolean,
      default: false
    },
    serviceApi: {
      type: Function,
      default: () => { }
    },
    serviceSelected: {
      type: Array,
      default: () => []
    },
    serviceSearchForm: {
      type: Object,
      default: () => { }
    }
  },

  mounted() {
    this.selectedData = deepClone(this.serviceSelected)
    this.getSelectingList()
  },

  methods: {
    selectable(row) {
      const { selectedData } = this
      // 每个服务项目code只能选中一个 按等级和年限区分
      if (selectedData.filter(v => v.uniqueCode == row.uniqueCode).length) {
        return true
      } else if (selectedData.filter(v => v.serviceCode == row.serviceCode).length) {
        return false
      } else {
        return true
      }
    },
    recoverySelection() {
      var exitIds = this.selectedData.map(item => item.uniqueCode)
      this.selectingData.forEach(item => {
        exitIds.includes(item.uniqueCode) && this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    getSelectingList() {
      this.serviceApi({
        ...this.serviceSearchForm,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        res?.list?.forEach(val => {
          val.uniqueValue = `${val.serviceName}-${val.serviceGrade}/${val.servicePeriod}`
          val.uniqueCode = `${val.serviceCode}-${val.serviceGrade}/${val.servicePeriod}`
        })
        this.selectingData = res.list
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(item => item.uniqueCode === row.uniqueCode)
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(item => item.uniqueCode !== row.uniqueCode))
    },
    handleAll(selection) {
      this.selectingData.forEach(item => {
        var isExit = this.selectedData.findIndex(subItem => subItem.serviceCode === item.serviceCode)
        if (selection.length && (isExit === -1)) {
          this.selectedData.push(item)
        } else if (!selection.length && (isExit !== -1)) {
          this.selectedData.splice(isExit, 1)
        }
      })
    },
    clearSelect() {
      this.selectedData = []
      this.$refs.seletingTable.clearSelection()
    },
    delItem(row) {
      this.selectedData = this.selectedData.filter(item => item.uniqueCode !== row.uniqueCode)
      const obj = this.selectingData.find(v => v.uniqueCode == row.uniqueCode)
      if (obj) this.$refs.seletingTable.toggleRowSelection(obj)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSelectingList()
    },
    handleRelateServiceConfirm() {
      this.$emit('handleRelateServiceConfirm', this.selectedData)
    },
    handleRelateServiceClose() {
      this.$emit('handleRelateServiceClose')
    }
  }
}

</script>
<style lang="scss" scoped>
.relateService .table {
  margin: 0 auto 20px !important;
  width: 100%;
}
.relateService .tableCon {
  margin-top: 20px;
  display: flex;
  .tableBox {
    flex: 2;
  }
  .selectedBox{
    flex: 1;
    height: 462px;
    border: 1px solid #E5E5E5;
    margin-left: 10px;
    .title{
      height: 38px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 8px 0 16px;
      border-bottom: 1px solid #E5E5E5;
      .title_txt{
        font-size: 16px;
        font-weight: 600;
        color: #333333;
      }
      .title_btn{
        cursor: pointer;
        font-size: 14px;
        color: #185EFE;
      }
    }
    .content{
      padding: 16px 10px;
      height: calc(100% - 75px);
      overflow-y: scroll;
      .item{
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 32px;
        color: #666666;
        padding: 0 8px;
        .del_item{
          cursor: pointer;
        }
      }
      .item:hover{
        background: #F6F9FF;
      }
    }
  }
}
.service-service .relateService .el-table .cell,
.d2-form .relateService .el-table .cell {
  line-height: 26px !important;
}
.service-service .relateService .el-table--medium td,
.d2-form .relateService .el-table--medium td {
  padding: 6px 8px !important;
}
.relateService .red {
  color: red;
}
.relateService .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;
}
.text_hide{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

::v-deep .el-table__header-wrapper .has-gutter .el-checkbox{
  display: none;
}
::v-deep .text_hide p{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0;
}
</style>