CarType.vue 6.84 KB
<!-- components navbar-->
<template>
  <div class="com-car-type-container">
    <van-field
      :value="comValue"
      readonly
      :label="label"
      :placeholder="title"
      class="van-ellipsis"
      clearable
      :right-icon="clearable ? 'replay' : 'arrow'"
      input-align="right"
      @click-input="show = true"
      @click-right-icon="clear"
      :required="isRequire"
    />
    <van-popup v-model="show" round position="bottom" :style="{ height: '90%' }">
      <van-search v-model="txtSearch" placeholder="搜索" input-align="center" @input="onSearch" />
      <van-tabs v-model="active" @change="onTabChange" color="#3975C6" :key="active" :animated="true" duration="0.1">
        <van-tab :title="select.brand.label" :disabled="!brands.length">
          <van-cell-group v-for="o in brands" :key="o.value">
            <van-cell :value="o.label" @click="brandClickHandle(o)" class="cell-item">
              <template #right-icon v-if="select.brand.value === o.value">
                <van-icon name="success" />
              </template>
            </van-cell>
          </van-cell-group>
        </van-tab>
        <van-tab :title="select.series.lineName" :disabled="!series.length">
          <van-cell-group v-for="o in series" :key="o.lineCode">
            <van-cell :value="o.lineName" @click="seriesClickHandle(o)" class="cell-item">
              <template #right-icon v-if="select.series.lineCode == o.lineCode">
                <van-icon name="success" />
              </template>
            </van-cell>
          </van-cell-group>
        </van-tab>
        <van-tab :title="select.model.modelName" :disabled="!models.length">
          <van-cell-group v-for="o in models" :key="o.modelCode">
            <van-cell @click="modelsClickHandle(o)" class="cell-item">
              <template>
                <div>{{ o.modelName }}</div>
                <div class="model-code">{{ o.modelCode }}</div>
              </template>
              <template #right-icon v-if="select.model.modelCode == o.modelCode">
                <van-icon name="success" />
              </template>
            </van-cell>
          </van-cell-group>
        </van-tab>
      </van-tabs>
    </van-popup>
  </div>
</template>

<script>
import { debounce, tools } from 'utils'
let initData = function() {
  return tools.deepClone({
    brand: {
      label: '品牌',
      value: ''
    },
    series: {
      lineName: '车系',
      lineCode: ''
    },
    model: {
      modelName: '车型',
      modelCode: ''
    }
  })
}
export default {
  name: 'CarType',
  props: ['label', 'title', 'value', 'info', 'onChange', 'isRequire', 'clearable'],
  data() {
    return {
      active: 0,
      txtSearch: '',
      show: false,
      bak: [],
      curDataName: 'brands',
      brands: [],
      series: [],
      models: [],
      select: initData()
    }
  },
  computed: {
    comValue() {
      return this.value
    }
  },
  mounted() {
    this.getCarBrands()
  },
  watch: {
    active() {
      this.txtSearch = ''
    }
  },
  methods: {
    onTabChange(i) {},
    getCarBrands() {
      return new Promise(resolve => {
        this.api.common.getCarBrands().then(({ data: brands }) => {
          this.brands = brands
          this.bak[0] = _copy(brands)
          resolve()
        })
      })
    },
    getCarLines(brandid) {
      return new Promise(resolve => {
        this.api.common.getCarLines(brandid).then(({ data: series }) => {
          this.series = series
          this.bak[1] = _copy(series)
          resolve()
        })
      })
    },
    getCarModels(seriesid) {
      return new Promise((resolve, reject) => {
        this.api.common.getCarModels(seriesid).then(({ data: models = [] }) => {
          if (models.length) {
            this.models = models
            this.bak[2] = _copy(models)
            resolve()
          } else {
            reject('暂无对应车型')
          }
        })
      })
    },
    brandClickHandle(brand) {
      if (brand.value === this.select.brand.value) {
        return
      }
      this.select = Object.assign(initData(), { brand })
      this.series = []
      this.models = []
      this.getCarLines(brand.value).then(() => {
        this.active = 1
      })
    },
    seriesClickHandle(series) {
      if (series.lineCode === this.select.series.lineCode) {
        return
      }
      this.model = ''
      this.models = []
      Object.assign(this.select, { series, model: { modelName: '车型', modelCode: '' } })
      this.getCarModels(series.lineCode)
        .then(() => {
          this.active = 2
        })
        .catch(err => {
          this.$notify(err)
        })
    },
    modelsClickHandle(model) {
      this.show = false
      Object.assign(this.select, { model })
      const { brand, series, model: o } = this.select
      const info = {
        brandName: brand.label,
        brandCode: brand.value,
        lineName: series.lineName,
        lineCode: series.lineCode,
        modelName: o.modelName,
        modelCode: o.modelCode
      }
      this.$emit('update:info', info)
      this.$emit('update:value', o.modelName)
      this.onChange && this.onChange(info)
    },
    onSearch: debounce(function(v) {
      this[['brands', 'series', 'models'][this.active]] = this.bak[this.active].filter(item => item[['label', 'lineName', 'modelName'][this.active]].toUpperCase().includes(v.toUpperCase()))
    }),
    reset() {},
    clear() {
      if (!this.clearable) {
        this.show = true
        return
      }
      this.$emit('update:info', {})
      this.$emit('update:value', '')
      this.active = 0
      this.txtSearch = ''
      this.show = false
      this.curDataName = 'brands'
      this.select = initData()
    }
  }
}
function _copy(obj) {
  return JSON.parse(JSON.stringify(obj))
}
</script>
<style lang="scss" scoped>
.com-car-type-container {
  /deep/.van-popup {
    display: flex;
    flex-direction: column;
    .van-tabs {
      flex: 1;
      overflow: auto;
      display: flex;
      flex-direction: column;
      background-color: $background-color;
      &__wrap {
        box-shadow: 0px 1px 2px 0px #ccc;
      }
      &__content {
        flex: 1;
        margin-top: 5px;
        overflow: auto;
      }
    }
  }
  .cell-item {
    align-items: center;
    .van-icon-success {
      color: $main-color;
    }
    .model-code {
      @include font(12px, 400, #999);
    }
  }
  /deep/.van-cascader {
    &__header {
      margin-top: 10px;
      display: flex;
    }
    &__title {
      flex: 1;
      @include flexcenter();
      .van-search {
        flex: 1;
        padding: 0;
      }
      .reset {
        // flex: 0 0 50px;
        // text-align: right;
        position: absolute;
        right: 14px;
        top: 72px;
        z-index: 111;
        // transform: translateX(20px);
        @include font(15px, 800, $main-color);
      }
    }
    &__close-icon {
      flex: 0 0 30px;
      top: -50px;
    }
  }
}
</style>