add.vue 9.97 KB
<template>
  <div class="add-wrap">
    <div class="container edit-container" style="min-height: 520px !important">
      <p class="edit-title" id="basic">{{merchantId?'编辑门店':'新增门店'}}</p>
      <glsx-combi-form class="edit-form-container" ref="editFormRef" :isEdit="true" :isBlock="true" :formRef="editChildRef" :formObj="editForm" :formData="editFormData" :formRules="editRule">
        <div slot="merchantHtml">
          <el-button type="default" class="el-icon-plus" v-if="!isDisabled" @click="handleRelateStoreClose">选择门店</el-button>
          <div class="merchantNameLabel" v-if="editForm.merchantName">
            <p>所属门店</p>
            <div class="merchantName ellipsis-1">{{ editForm.merchantName }}</div>
          </div>
          <!-- <div class="el-form-item__error mrt5" v-if="merchantFlag">请选择门店</div> -->
        </div>
        <div slot="channelHtml">
          <el-checkbox-group v-model="editForm.channelIds" @change="handleCheckedChannelChange">
            <el-checkbox v-for="channel in channelList" :label="channel.channelType" :key="channel.channelType">{{ channel.channelName }}</el-checkbox>
          </el-checkbox-group>
        </div>
        <div slot="carSeriesHtml">
          <el-button type="default" class="el-icon-plus" v-if="!isDisabled" @click="handleCarSeriesClose">选择车系</el-button>
          <div class="car-series-wrap" v-if="editForm.vehicleSeriesRequests && editForm.vehicleSeriesRequests.length">
            <div class="car-series-item" v-for="item in editForm.vehicleSeriesRequests" :key="item.vehicleSeriesId">{{ item.vehicleSeriesName }}</div>
          </div>
        </div>
      </glsx-combi-form>
    </div>
    <select-store v-if="relateStoreVisible" ref="relateStore" :dialogVisible="relateStoreVisible" :storeSelected="selectedList" :merchantInfo="editForm.merchantId" @handleRelateStoreConfirm="handleRelateStoreConfirm" @handleRelateStoreClose="handleRelateStoreClose"></select-store>
    <selectCarDialog v-if="carDialogVisible" singleChoice @handleClose="handleClose" :echoData="editForm.vehicleSeriesRequests" @handleConfirm="handleConfirm" :dialogVisible="carDialogVisible"></selectCarDialog>
    <GLFooter>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave">确定</el-button>
    </GLFooter>
  </div>
</template>
<script>
import selectStore from './components/selectStore'
import selectCarDialog from './components/selectCarDialog'
export default {
  components: {
    selectStore,
    selectCarDialog
  },
  data() {
    return {
      size: 'small',
      editChildRef: 'editChildRef',
      merchantId: this.$route.query.merchantId ? this.$route.query.merchantId : '',
      editForm: {
        channelIds: [],
        vehicleSeriesRequests: [],
        merchantId: '',
        merchantName: ''
      },
      merchantFlag: false,
      editFormData: [
        {
          label: '选择门店',
          name: 'merchantId',
          addHtml: 'merchantHtml'
        },
        {
          label: '频道名称',
          name: 'channel',
          addHtml: 'channelHtml'
        },
        {
          label: '主营品牌车系',
          name: 'carId',
          addHtml: 'carSeriesHtml',
          isHide: true
        },
      ],
      selectedList: [],
      channelList: [],
      relateStoreVisible: false, // 选择门店
      carDialogVisible: false, //选择车系
      editRule: {
        merchantId: [{ required: true,validator: (rule, value, callback) => {
              if(this.editForm.merchantId){
                callback()
              }else{
                callback(new Error(`请选择门店`))
              }
            }, trigger: 'change' }],
        channel: [{ required: true,validator: (rule, value, callback) => {
          if(this.editForm.channelIds && this.editForm.channelIds.length ){
            callback()
          }else{
            callback(new Error(`请选择频道`))
          }
        }, trigger: 'change' }],
        carId: [{ required: true,  validator: (rule, value, callback) => {
              if(this.editForm.vehicleSeriesRequests && this.editForm.vehicleSeriesRequests.length){
                callback()
              }else{
                callback(new Error(`请选择主营品牌车系`))
              }
            }, trigger: 'change' }]
        }
    }
  },
  mounted() {
    this.getChannelList()

    if (this.merchantId) {
      this.getconfigMerchant()
      this.getMerchantData()
      this.getCategoryDetail()
    }
  },
  methods: {
    //获取频道名称
    getChannelList() {
      this.$apis.channel
        .getChannelListApi({
          pageSize: 100,
          pageNum: 1
        })
        .then(res => {
          this.channelList = res.list
        })
    },
    getCategoryDetail(){
      this.$apis.store.getCategoryByMerchant({merchantId: this.merchantId}).then(res => {
        if(res?.vehicleSeriesList?.length){
          this.editForm.vehicleSeriesRequests = res.vehicleSeriesList
        } 
      })
    },
    getMerchantData() {
      var params = {
        merchantId: this.merchantId,
        pageSize: 100,
        currentPage: 1
      }

      this.$apis.common.getMerchantList(params).then(res => {
        if (res.list.length) {
          this.editForm.merchantId = res.list[0].merchantNo
          this.editForm.merchantName = res.list[0].merchantName
        }
      })
    },
    //获取频道配置
    getconfigMerchant() {
      this.$apis.store.getconfigMerchant({ merchantId: this.merchantId }).then(res => {
        this.editForm.channelIds = []
        res.forEach(item => {
          this.editForm.channelIds.push(item.channelId)
          if(item.channelId==5) {
            const _target = this.editFormData.find(item => item.name == 'carId')
            _target.isHide = false
          }
        })
      })
    },
    handleCancel() {
      this.$router.go(-1)
    },
    handleSave() {
      this.$refs.editFormRef.$refs[this.editChildRef].validate(valid => {
        if (!this.editForm.merchantName) {
          this.merchantFlag = true
          return
        }
        if (valid) {
          let params = {
            merchantId: this.editForm.merchantId,
            merchantName: this.editForm.merchantName,
            channelIds: this.editForm.channelIds,
            vehicleSeriesRequests: this.editForm?.channelIds?.includes(5) ?  this.editForm.vehicleSeriesRequests : []
          }
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          this.$apis.store.configMerchant(params).then(res => {
            if (res.code == 0) {
              this.loading = false
              this.$router.go(-1)
            }

            this.$message({
              message: res.code == '0' ? '编辑成功' : res.message,
              type: res.code == '0' ? 'success' : 'error'
            })
          }).finally(end =>{
            loading.close()
          })
        }
      })
    },
    handleCheckedChannelChange(val) {
      const _target = this.editFormData.find(item => item.name == 'carId')
      if(val?.includes(5)){
        _target.isHide = false
      }else{
        _target.isHide = true
        this.editForm.vehicleSeriesRequests = []
      }
    },
    //选择车系
    handleCarSeriesClose(){
      this.carDialogVisible = !this.carDialogVisible
    },
    // 选择门店
    handleRelateStoreClose() {
      this.relateStoreVisible = !this.relateStoreVisible
    },
    handleRelateStoreConfirm(selectedData) {
      this.$refs.editFormRef.$refs[this.editChildRef].clearValidate('merchantId')
      selectedData.merchantId && (this.editForm.merchantId = selectedData.merchantId)
      selectedData.merchantName && ((this.editForm.merchantName = selectedData.merchantName), (this.merchantFlag = false))
      this.handleRelateStoreClose()
    },
    handleClose(){
      this.carDialogVisible = false
    },
    handleConfirm(e={}){
      let carList = []
      for(const key in e){
        for(const sonKey in e[key]){
          carList = [...carList, ...e[key][sonKey]]
        }
      }
      this.editForm.vehicleSeriesRequests = carList.map(item =>{
        const {id, name, parentId, parentName, rootId, rootName} = item
        return {
          brandId: rootId,
          brandName: rootName,
          childBrandId: parentId,
          childBrandName: parentName,
          vehicleSeriesId: id,
          vehicleSeriesName: name
        }
      })
      this.handleClose()
      this.$refs.editFormRef.$refs[this.editChildRef].validateField('carId')
    }
  }
}
</script>
<style scoped lang="scss">
.add-wrap {
  .container {
    min-height: 680px;
    padding: 0px 0px 20px;
    margin-bottom: 16px;
  }
  .edit-title {
    font-size: 16px;
    font-weight: 600;
    height: 58px;
    line-height: 58px;
    border-bottom: 1px solid #e7eaec;
    margin: 0 0;
    padding: 0 0 0 20px;
  }
  .edit-form-container {
    margin-top: 20px;
  }
  .assignLabel {
    color: #5850ff;
  }
  .merchantNameLabel {
    width: 510px;
    color: #1d1d1d;
    // margin-left: 50px;
    padding: 16px;
    margin-top: 8px;
    border: 1px solid #e6e6e6;
    border-radius: 8rpx;
    background: #f9f9f9;
    .merchantName {
      box-sizing: border-box;
      width: 284px;
      height: 32px;
      background: #ffffff;
      border: 1px solid #e6e6e6;
      border-radius: 4px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 14px;
      color: #999999;
      line-height: 32px;
      padding: 0 8px;
    }
  }
  .car-series-wrap{
    margin-top: 8px;
    width: 580px;
    height: 80px;
    background: #FFFFFF;
    border: 1px solid #E8E8EA;
    border-radius: 4px;
    padding: 6px 8px;
    overflow-y: auto;
    .car-series-item{
        display: inline-block;
        padding: 0 8px;
        height: 24px;
        color: #666666;
        line-height: 24px;
        background: #FAFBFE;
        border: 1px solid #E5E5E5;
        border-radius: 4px;
        margin-right: 4px;
    }
  }
  
}
</style>