sendOutComp.vue 5.41 KB
<template>
  <h-dialog ref="myDialog" :title="title" :isVisible="isVisible" @confirm="handleSubmit" @cancel="handleCancel">
    <div class="formObj">
      <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.shippingEntity }}
              <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 purchaseApi from '@/api/purchase'
export default {
  components: { hDialog },
  data() {
    return {
      title: '发货主体选择',
      keyword: '',
      originalShippingEntityList: [],
      shippingEntityList: [],
      selectSendOutMerchantId: ''
    }
  },
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    sendOutMerchantId: {
      type: String,
      default: ''
    }
  },
  created() {
    this.selectSendOutMerchantId = this.sendOutMerchantId
    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
      }

      // 进行模糊搜索,匹配 shippingEntity 字段
      const filteredList = this.originalShippingEntityList.filter(item => {
        return item.shippingEntity.toLowerCase().includes(val.toLowerCase())
      })

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

      if (this.shippingEntityList.length) {
        this.handleFindIdAttribute()
      }
    },
    async selectShippingEntityInfoAll() {
      let result = await purchaseApi.selectShippingEntityInfoAll()
      console.log(result)
      if (result.code == 0) {
        result.data.forEach(item => {
            item.checked = false
        })
        this.shippingEntityList = result.data
        this.originalShippingEntityList = [...this.shippingEntityList]
        this.handleFindIdAttribute()
      }
    },
    handleFindIdAttribute() {
      this.shippingEntityList.forEach(item => {
        item.checked = false
      })
      this.shippingEntityList.forEach(item => {
        console.log(item.shippingEntityId == this.selectSendOutMerchantId,item.shippingEntityId,this.selectSendOutMerchantId)
        if (item.shippingEntityId == this.selectSendOutMerchantId) {
          item.checked = true
        }
      })
    },
    handleCheck(index) {
      this.shippingEntityList.forEach(item => {
        item.checked = false
      })
      this.shippingEntityList[index].checked = true
      this.selectSendOutMerchantId = this.shippingEntityList[index].shippingEntityId
      console.log(this.selectSendOutMerchantId)
    },
    handleSubmit() {
      let data = this.originalShippingEntityList.find(item => item.shippingEntityId == this.selectSendOutMerchantId) || {}
      this.$emit('submit', data)
      // 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>