sendOutComp.vue 6.25 KB
<template>
  <h-dialog ref="myDialog" :title="title" :isVisible="isVisible" @confirm="handleSubmit" @cancel="handleCancel">
    <div class="formObj" v-if="isFlag">
      <div class="search-pop-input">
        <van-field v-model="keyword" placeholder="搜索" left-icon="https://img02.glsx.com.cn/weapp/resource/saas/scrm/search-icon.png" @input="onSearch" />
      </div>
      <template v-if="shippingEntityList.length > 0">
        <div class="lists-wrap">
          <template v-for="(item, index) in shippingEntityList">
            <div :class="['list', item.checked ? 'active' : '']" @click="handleCheck(index)" :key="index">
              {{ item.merchantName }}
              <img v-if="item.checked" src="@/assets/images/mark.png" alt="" class="icon-mark" />
            </div>
          </template>
        </div>
      </template>
      <div v-else class="no-data">
        <img src="@/assets/images/no-data.png" />
        <span class="no-data-text">暂无数据</span>
      </div>
    </div>
  </h-dialog>
</template>

<script>
import hDialog from './dialog'
import utils from '@/utils/util'
import allocateApi from '@/api/allocate'
export default {
  components: { hDialog },
  data() {
    return {
      isFlag: false,
      title: '调出门店仓库',
      keyword: '',
      originalShippingEntityList: [],
      shippingEntityList: [],
      selectSendOutMerchantId: ''
    }
  },
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    sendOutMerchantId: {
      type: String,
      default: ''
    },
    isYq: {
      type: Number,
      default: 1
    },
    yqMerchantId: {
      type: String,
      default: ''
    },
    yqMerchantName: {
      type: String,
      default: ''
    }
  },
  created() {
    this.selectSendOutMerchantId = this.sendOutMerchantId
    this.title = this.isYq == 2 ? '调出仓库选择' : '调出门店选择'
    console.log('created', this.sendOutMerchantId)
    this.selectShippingEntityInfoAll()
  },
  methods: {
    onSearch(val) {
      const debouncedSearch = utils.debounce(
        val => {
          this.handleSearch(val)
        },
        200,
        true
      )

      // 调用防抖函数
      debouncedSearch(val)
    },
    handleSearch(val) {
      // 如果搜索值为空,显示全部数据
      if (!val || val.trim() === '') {
        this.shippingEntityList = this.originalShippingEntityList || this.shippingEntityList
        return
      }
      // console.log()
      // 进行模糊搜索,匹配 shippingEntity 字段
      const filteredList = this.originalShippingEntityList.filter(item => {
        return item.merchantName.toLowerCase().includes(val.toLowerCase())
      })

      // 更新列表数据
      this.shippingEntityList = filteredList

      if (this.shippingEntityList.length) {
        this.handleFindIdAttribute()
      }
    },
    getMethodsByYq() {
      const methodMap = {
        1: 'selectServicemerchantList',
        2: 'selectYqMerchantList'
      }
      return methodMap[this.isYq]
    },
    async selectShippingEntityInfoAll() {
      let methodName = this.getMethodsByYq()
      let params = {}
      if (this.isYq == 1) params.yqMerchantId = this.yqMerchantId
      let result = await allocateApi[methodName](params)
      this.isFlag = true
      if (result.code == 0) {
        result.data.forEach(item => {
          item.checked = false
        })
        this.shippingEntityList = result.data
        let _headOffice = { merchantId: this.yqMerchantId, merchantName: this.yqMerchantName, checked: false }
        if (this.isYq == 1) {
          this.shippingEntityList.unshift(_headOffice)
        }
        this.originalShippingEntityList = [...this.shippingEntityList]
        this.handleFindIdAttribute()
      }
    },
    handleFindIdAttribute() {
      let key = this.isYq == 1 ? 'merchantId' : 'merchantNo'
      this.shippingEntityList.forEach(item => {
        item.checked = false
      })
      this.shippingEntityList.forEach(item => {
        if (item[key] == this.selectSendOutMerchantId) {
          item.checked = true
        }
      })
    },
    handleCheck(index) {
      let key = this.isYq == 1 ? 'merchantId' : 'merchantNo'
      this.shippingEntityList.forEach(item => {
        item.checked = false
      })
      this.shippingEntityList[index].checked = true

      this.selectSendOutMerchantId = this.shippingEntityList[index][key]
      console.log(this.selectSendOutMerchantId)
    },
    handleSubmit() {
      let key = this.isYq == 1 ? 'merchantId' : 'merchantNo'
      let data = this.originalShippingEntityList.find(item => item[key] == this.selectSendOutMerchantId) || {}
      this.$emit('submit', { ...data, isYq: this.isYq })
      // this.$refs.myDialog.stopLoading();
    },
    handleCancel() {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang="scss" scoped>
.formObj {
  margin: 0 16px;
  font-size: 14px;
  .lists-wrap {
    box-sizing: border-box;
    overflow: scroll;
    height: calc(100% - 52px);
    .list {
      display: inline-block;
      height: 46px;
      line-height: 46px;
      color: #292d33;
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid #eff0f2;
    }
    .active {
      color: #347bff;
    }
    .icon-mark {
      width: 16px;
      height: 16px;
    }
  }
  .search-pop-input {
    margin: 8px 0;
    position: relative;
    z-index: 99;
    width: 343px;
    ::v-deep(.van-cell) {
      width: 100%;
      background: #f2f3f5;
      height: 36px;
      border-radius: 253px;
    }

    ::v-deep(.van-field__left-icon) {
      position: absolute;
      width: 16px;
      height: 16px;
      margin: 4px 6px 8px 12px;
      z-index: 999;
      .van-icon {
        color: #999ea8;
      }
    }

    ::v-deep(.van-field__body) {
      position: absolute;
      width: 343px;
      border-radius: 16px;
      left: 0;
    }
    ::v-deep(.van-field__control) {
      padding-left: 33px;
      font-size: 14px;
    }
  }
  .no-data {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-items: center;
    img {
      width: 72px;
      height: 72px;
    }
    .no-data-text {
      color: #999ea8;
      font-size: 12px;
      margin-top: 16px;
    }
  }
}
</style>