CarModelList.vue 7.12 KB
<template>
  <view class="car-model-box">
    <scroll-view
      class="scroll-view"
      scroll-y
      :scroll-into-view="letter"
    >
      <template v-for="(i, index) in (searchStatus ? searchList : list)">
        <view
          class="item-box"
          :key="index"
          :id="i.letter"
        >
          <view class="letter-title flex">{{i.letter}}</view>
          <template v-for="(c,cindex) in i.list">
            <view
              class="car-model"
              :key="cindex"
            >
              <view class="brand-name">{{c.name}}</view>
              <view class="brand-list">
                <template v-for="(j, jindex) in c.childs_list">
                  <view
                    :key="jindex"
                    :class="[
                    {'sub-brand-name':j.isTitle},
                    {'item flex':!j.isTitle},
                    {active: (currentId ? j.id == currentId : j.id == data.customer[params.idKey]) && !j.isTitle}
                    ]"
                    @click="choose(j,c)"
                  >
                    <view
                      class="sprite-icon icon-tag-checked"
                      v-if="!j.isTitle"
                    ></view>
                    {{j.name}}
                  </view>
                </template>
              </view>
            </view>
          </template>

        </view>
      </template>
    </scroll-view>
    <view class="letter-list">
      <template v-for="(i, index) in list">
        <view
          class="letter-item"
          :key="index"
          @click="letter = i.letter"
        >
          {{i.letter}}
        </view>
      </template>
    </view>
  </view>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'CarModelList',
  inject: ['userInfo'],
  data () {
    return {
      list: [],
      currentId: '',
      searchList: [],
      letter: 'A',
      searchStatus: false,
    }
  },
  props: {
    params: Object,
    data: Object,
  },
  mounted () {
  },
  methods: {
    async getList (searchValue) {
      const { userInfo, params } = this,
        { cacheData } = params
      this.currentId = ''
      if (cacheData && cacheData.length) {
        this.initCarList(cacheData)
        if (searchValue) this.handleSearch(searchValue)
        return
      }
      const { shopMerchantId } = userInfo,
        data = await uni.$api.popupApi.getMerchantCarBrandList({
          merchantId: shopMerchantId
        })
      if (data && data.length) {
        this.initCarList(data)
        params.cacheData = data
        this.$emit('update:params', params)
      }
    },
    initCarList (carData) {
      let { params: { idKey }, data: { customer } } = this,
        letterList = Array.from(new Set([...carData.map((e) => e.letter)])),
        carModelList = letterList.map(e => {
          let list = []
          carData.map(i => {
            if (e === i.letter) {
              let c_list = []
              i.childs.map(c => {
                if (c.childs) {
                  c.childs.map((s) => {
                    s.carChildsBrandId = c.id;
                    s.carChildsBrandName = c.name;
                    if (customer && customer[idKey]) {
                      if (s.id == customer[idKey]) {
                        s.active = true;
                        this.letter = i.letter;
                      }
                    }
                  });
                  let carSecondInfo = {
                    carChildsBrandId: c.id,
                    carChildsBrandName: c.name,
                    name: c.name,
                    isTitle: true,
                  };
                  c_list = [...c_list, carSecondInfo, ...c.childs];
                }
              })
              i.childs_list = c_list;
              list.push(i)
            }
          })
          return {
            letter: e,
            list,
          }
        })
      this.list = carModelList
      this.$emit('onComplete')
    },
    handleSearch (searchValue) {
      if (!searchValue) {
        this.searchStatus = false
        this.letter = 'A'
      } else {
        searchValue = searchValue.toUpperCase()
        let { list } = this,
          carBrand = JSON.parse(JSON.stringify(list))
        let arr = [];
        carBrand.map(c => {
          c.list.map(i => {
            let childs_list = [],
              i_name = i.name.toUpperCase()
            i.childs_list.map((k) => {
              let k_name = k.name.toUpperCase()
              if (k_name.indexOf(searchValue) != -1) {
                childs_list.push(k);
              }
            });

            if (i_name.indexOf(searchValue) != -1 || childs_list.length > 0) {
              i.childs_list = childs_list.length > 0 ? childs_list : i.childs_list;
              arr.push(i);
            }
          })
        })
        let letterList = Array.from(new Set([...arr.map((e) => e.letter)]));
        let searchList = letterList.map((l) => {
          let _list = [];
          arr.map((i) => {
            if (l === i.letter) {
              _list.push(i);
            }
          })
          return {
            letter: l,
            list: _list,
          }
        })
        this.searchList = searchList
        this.searchStatus = true

      }
    },
    choose (that, item) {
      if (that.isTitle) return
      let chooseItem = {
        carBrandId: item.id,
        carBrand: item.name,
        carChildsBrandId: that.carChildsBrandId,
        carChildsBrandName: that.carChildsBrandName,
        carSeriesId: that.id,
        carSeriesName: that.name,
      }
      this.currentId = that.id
      this.$emit('choose', chooseItem)
    }
  }
}
</script>

<style scoped lang='scss'>
.car-model-box {
  position: relative;
  height: 100%;
  width: 100%;
  .scroll-view {
    height: 100%;
  }
  .letter-list {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 0;
    color: #888888;
    font-size: 28rpx;
    line-height: 40rpx;
    padding: 0 18rpx;
    text-align: center;
  }
  .item-box {
    margin-bottom: 32rpx;
    .letter-title {
      color: #888888;
      font-size: 28rpx;
      height: 40rpx;
      background: #f5f7fa;
      padding-left: 48rpx;
    }
    .car-model {
      margin: 0 48rpx;
      .brand-name {
        color: #333333;
        font-size: 32rpx;
        line-height: 44rpx;
        margin: 16rpx 0;
      }
      .sub-brand-name {
        color: #888888;
        font-size: 28rpx;
        line-height: 40rpx;
        width: 100%;
        flex-shrink: 0;
      }
    }
  }
  .brand-list {
    // display: grid;
    // grid-template-columns: repeat(4, 1fr);
    gap: 32rpx;
    display: flex;
    // gap: 32rpx 0;
    flex-wrap: wrap;
    .item {
      width: calc((100% - 96rpx) / 4);
      justify-content: center;
      height: 72rpx;
      border-radius: 8rpx;
      border: 2rpx solid #dce0e6;
      box-sizing: border-box;
      position: relative;
      color: #333333;
      font-size: 32rpx;
      .sprite-icon {
        display: none;
        position: absolute;
        top: 0;
        right: 0;
      }
    }
    .active {
      border-color: #2c66f7;
      background: #ecf1ff;
      color: #2c66f7;
      .sprite-icon {
        display: block;
      }
    }
  }
}
</style>