carBandDialog.vue 10 KB
<!-- 车型选择 -->
<template>
  <GLDialog bigTitle="选择适用车型" mode="big" :visible.sync="visible" okText="确定" :before-close="handleRelateStoreClose"
    @onOk="handleRelateStoreConfirm" @onCancel="handleRelateStoreClose" class="relateStore">
    <div v-loading="carLoading">
      <el-form :model="storeSearchForm" ref="relate" :inline="true" label-width="90px">
        <el-form-item label="车型名称:" prop="carTypeName">
          <el-input :size="size" v-model.trim="storeSearchForm.carTypeName" placeholder="请输入车型名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="车系:" prop="merchantName">
          <el-cascader :size="size" :props="props" v-model="storeSearchForm.seriesId" :options="options"
            @change="handleChangeCar" filterable placeholder="搜索/请先选择车系"></el-cascader>
          <div class="carTips" v-if="!storeSearchForm.seriesId.length">请先选择车系</div>
        </el-form-item>
        <el-form-item>
          <el-button :size="size" type="primary" @click="handleSearchStore">搜索</el-button>
          <!-- <el-button :size="size" @click="handleSearchReset">重置</el-button> -->
        </el-form-item>
      </el-form>
      <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="name" label="车型" align="center"></el-table-column>
            <el-table-column prop="seriesName" label="所属车系" align="center"></el-table-column>
            <el-table-column prop="childName" label="所属子品牌" align="center"></el-table-column>
            <el-table-column prop="brandName" 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="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.carTypeName }}</div>
                <div class="select_clear" @click="handleDelete(subItem)"><i class="el-icon-error"></i></div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

  </GLDialog>
</template>

<script>
export default {
  name: 'carBandDialog',
  data() {
    return {
      props: {
        children: 'childs',
        label: 'name',
        value: 'id',
        expandTrigger: 'hover'
      },
      storeSearchForm: {
        carTypeName: '',
        seriesId: ''
      },
      selectedData: [],
      selectingData: [],
      // 分页
      total: 0,
      pageNum: 1,
      pageSize: 20,
      size: 'small',
      carLoading: false,
      options: []
    }
  },

  props: {
    relateStoreVisible: {
      type: Boolean,
      default: false
    },
    hasPage: {
      type: Boolean,
      default: true
    },
    // 页面来源
    // sourcePage: {
    //   type: String,
    //   default: ''
    // },
    storeApi: {
      type: Function,
      default: () => { }
    },
    storeSelected: {
      type: Array,
      default: () => []
    },
    upgradeGoodsInfo: {
      type: Object,
      default: () => { }
    },
    // 去掉表格中某些字段
    // noTableField: {
    //   type: Array,
    //   default: () => []
    // }
  },
  computed: {
    visible: {
      get() {
        return this.relateStoreVisible
      },
      set(val) {
        this.$emit('update:relateStoreVisible', val)
      }
    }
  },
  mounted() {
    console.log('this.storeSelected', this.upgradeGoodsInfo)
    this.selectedData = this.$common.utils.deepClone(this.upgradeGoodsInfo.suitCarTypeList || [])
    this.selectedData.forEach(item => {
      item.carTypeId = item.carTypeId ? item.carTypeId + '' : item.carTypeId
    })
    // 获取车型
    this.getSelectingList()
  },

  methods: {

    handleSearchStore() {
      let [id, id2, id3] = this.storeSearchForm.seriesId
      if (id3) {
        this.handleChangeCar(this.storeSearchForm.seriesId)
      } else {
        return this.$message.error('请选择车系');
      }
    },
    handleSearchReset() {
      this.storeSearchForm = {
        carTypeName: '',
        seriesId: ''
      }
      // this.$nextTick(() => {
      //   this.handleChangeCar()
      // })
    },
    selectable(row, index) {
      var isSelect = row.hasOwnProperty('beServed') ? !row.beServed : true
      return isSelect
    },
    recoverySelection() {
      var exitIds = this.selectedData.map(item => item.carTypeId + '')
      this.selectingData.forEach(item => {
        if (exitIds.includes(item.carTypeId + '')) {
          this.$refs.seletingTable.toggleRowSelection(item, true)
        } else {
          this.$refs.seletingTable.toggleRowSelection(item, false)
        }
      })
    },
    getSelectingList() {
      const upgradeGoodsId = this.upgradeGoodsInfo.upgradeGoodsId || this.upgradeGoodsInfo.id
      if (!upgradeGoodsId) {
        this.carLoading = false
        return
      }
      this.carLoading = true
      this.$apis.purchaseShopping.getCanSelectCarBrandList({ upgradeGoodsId }).then(res => {
        console.log(res, 'getSelectingList')
        this.carLoading = false
        this.options = res.data
      })
    },
    // 获取列表
    handleChangeCar() {
      console.log(this.storeSearchForm.seriesId, 'storeSearchForm.brandId')
      let [id, id2, seriesId] = this.storeSearchForm.seriesId
      let params = {
        seriesId,
        carTypeName: this.storeSearchForm.carTypeName
      }
      seriesId && this.$apis.purchaseShopping.carTypePage({
        // ...this.storeSearchForm,
        ...params,
        currentPage: this.pageNum,
        pageSize: this.hasPage ? this.pageSize : 99999,
      }).then(res => {
        console.log(res, 'carTypePage')
        if (res.code == 0) {
          res.data.list.map(item => {
            item.carTypeId = item.id
            item.carTypeName = item.name
            item.childName = item.brandName
            item.childId = item.brandId
            item.brandName = item.mainBrandName
            item.brandId = item.mainBrandId
          })
          this.selectingData = res.data.list || []
          this.total = res.data.total
          this.$nextTick(() => {
            this.recoverySelection()
          })
        }

      })
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(item => +item.carTypeId === +row.carTypeId)
      addOrDelete ? this.selectedData.push(row) : (this.selectedData = this.selectedData.filter(item => item.carTypeId !== row.carTypeId))
    },
    handleAll(selection) {
      this.selectingData?.forEach(item => {
        let isExit = this.selectedData.findIndex(subItem => +subItem.carTypeId === +item.carTypeId)
        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.carTypeId !== +row.carTypeId)
      this.recoverySelection()
    },
    handelClearStore() {
      this.selectedData = []
      this.recoverySelection()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.handleChangeCar()
    },
    handleRelateStoreConfirm() {
      if (!this.selectedData.length) {
        this.$message({
          message: '请选择关联车型',
          type: 'warning',
          showClose: true,
          customClass: 'message-Z-zinde'
        })
        return true
      }
      console.log(this.selectedData, ' this.selectedData')
      this.$emit('handleRelateStoreConfirm', this.selectedData)
      this.visible = false
    },
    handleRelateStoreClose() {
      // this.$emit('handleRelateStoreClose')
      this.visible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.carTips {
  // margin-left: 20px;
  height: 12px;
  margin: 0;
  color: #EB4E4E;
  font-size: 12px;
}

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

      min-height: calc(100% - 40px);
      max-height: calc(100% - 40px);
      box-sizing: border-box;

      .select_item {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        min-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>