selectCarDialog.vue 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
<template>
  <GLDialog title="选择车系" mode="mid" :visible.sync="dialogVisible" :before-close="handleClose"
            :close-on-click-modal="false" @onOk="handleConfirm" @onCancel="handleClose">
    <div class="search-wrap">
      <!-- <el-select clearable v-model="curbrandName" @change="changeFilterItem" filterable placeholder="请选择">
                <el-option
                v-for="item in searchList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                </el-option>
            </el-select> -->
      <el-autocomplete clearable :debounce="1000" v-model="curbrandName" :fetch-suggestions="querySearchAsync"
                       placeholder="请输入内容" @select="changeFilterItem">
      </el-autocomplete>
    </div>
    <div v-if="loading" class="loading-wrap">
      <img class="loading-img" :src="url" alt="" />
    </div>
    <div class="car-container" v-else>
      <div class="wrap">
        <div class="item son-brand-wrap" v-for="(item, index) in filterBrandList" :key="item.id">
          <el-checkbox v-if="!singleChoice" :disabled="!(item.childs && item.childs.length)"
                       :indeterminate="isIndeterminate(item, 1)" :value="isChecked(item, 1)"
                       @change="changeBrandStatus($event, item, index)"></el-checkbox>
          <span :class="[
            'son-brand-label',
            { curItem: curBrandInfo.id == item.id }
          ]" @click="showSonBrandList(item)">
            {{ item.name }}
            <i v-if="item.childs && item.childs.length" class="el-icon-arrow-right"></i>
          </span>
        </div>
      </div>
      <div class="wrap">
        <div class="item son-brand-wrap" v-for="(item, index) in sonBrandList" :key="item.id">
          <el-checkbox :disabled="!(item.childs && item.childs.length)" :indeterminate="isIndeterminate(item, 2)"
                       :value="isChecked(item, 2)" @change="changeSonBrandStatus($event, item, index)"></el-checkbox>
          <span :class="[
            'son-brand-label',
            { curItem: curSonBrandInfo.id == item.id }
          ]" @click="showCarSeriesList(item)">
            {{ item.name }}
            <i v-if="item.childs && item.childs.length" class="el-icon-arrow-right"></i>
          </span>
        </div>
      </div>
      <div class="wrap">
        <el-checkbox class="item" v-for="item in carSeriesList" :key="item.id" :value="isChecked(item, '3')"
                     @change="changeCarSeriesStatus($event, item, index)">{{ item.name }}</el-checkbox>
      </div>
    </div>
  </GLDialog>
</template>

<script>
import url from "@/assets/loading.png"
// import { mapState, mapActions } from 'vuex'
import { getCarBrandList } from "@api/commodity.js"
export default {
  name: "SelectCarDialog",
  data() {
    return {
      url,
      loading: false,
      restaurants: [
        { value: "三全鲜食(北新泾店)", address: "长宁区新渔路144号" },
        {
          value: "Hot honey 首尔炸鸡(仙霞路)",
          address: "上海市长宁区淞虹路661号"
        }
      ],
      curbrandName: "",
      searchList: [], // 搜索列表
      filterBrandList: [], // 品牌数据
      sonBrandList: [], // 子品牌数据
      carSeriesList: [], // 车系数据
      selectdData: {}, // 已选的数据
      carSeriesData: [],
      curBrandInfo: {
        id: "",
        name: ""
      },
      curSonBrandInfo: {
        id: "",
        name: ""
      },
      isClearData: true // 品牌为单选的时候,是否需要清空数据
    }
  },
  // computed: {
  //   ...mapState({
  //     carSeriesData: (state) => state.channel.carSeriesData, // 品牌源数据
  //     searchList: (state) => state.channel.searchList
  //   })
  // },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    singleChoice: {
      type: Boolean,
      default: true
    },
    echoData: {
      type: Array,
      default: () => []
    }
  },
  created() {
    // this.filterBrandList = this.carSeriesData
    // // this.flatData(this.carSeriesData)
    // this.echoDataFn()
    this.getCarBrandList()
  },
  mounted() {
    // this.getCarSeriesData()
  },
  methods: {
    getCarBrandList() {
      getCarBrandList({ needCarSeries: true })
        .then((res) => {
          console.log(res)
          this.filterBrandList = res
          this.carSeriesData = res
          const payload = res
          const list = []
          payload?.forEach(item => {
            list.push({ id: `${item.id}`, value: item.name })
            item.childs?.forEach(ele => {
              list.push({ id: `${item.id}-${ele.id}`, value: ele.name })
              ele.childs?.forEach(o => {
                list.push({ id: `${item.id}-${ele.id}-${o.id}`, value: o.name })
              })
            })
          })
          this.searchList = list

          this.echoDataFn()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    querySearchAsync(queryString, cb) {
      var restaurants = this.searchList
      // this.createStateFilter(queryString)
      console.log(queryString, restaurants, 'queryString')
      var results = queryString
        ? restaurants.filter(state => {
          console.log(state, 'state')
          return state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1
        })
        : []
      console.log(results, 'results')
      if (!queryString) this.filterBrandList = this.carSeriesData
      if (results?.length > 0) cb(results)
    },
    createStateFilter(queryString) {
      // console.log(queryString, 'queryString')
      // debugger
      return (state) => {
        console.log(state, 'state')
        return state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1
      }
    },
    handleSelect(e) {
      console.log(e)
    },
    echoDataFn() {
      const obj = {}
      this.echoData.forEach((item) => {
        const {
          brandId,
          brandName,
          childBrandId,
          childBrandName,
          vehicleSeriesId,
          vehicleSeriesName
        } = item
        if (obj[brandId]) {
          if (obj[brandId][childBrandId]) {
            obj[brandId][childBrandId].push({
              rootId: brandId,
              rootName: brandName,
              parentId: childBrandId,
              parentName: childBrandName,
              id: vehicleSeriesId,
              name: vehicleSeriesName
            })
          } else {
            obj[brandId][childBrandId] = []
            obj[brandId][childBrandId].push({
              rootId: brandId,
              rootName: brandName,
              parentId: childBrandId,
              parentName: childBrandName,
              id: vehicleSeriesId,
              name: vehicleSeriesName
            })
          }
        } else {
          obj[brandId] = {}
          obj[brandId][childBrandId] = []
          obj[brandId][childBrandId].push({
            rootId: brandId,
            rootName: brandName,
            parentId: childBrandId,
            parentName: childBrandName,
            id: vehicleSeriesId,
            name: vehicleSeriesName
          })
        }
      })
      this.selectdData = obj
      console.log(this.echoData, this.selectdData, 123)
    },
    changeFilterItem(e) {
      const _idList = e.id?.split("-") || []
      const _target = this.carSeriesData.find((item) => item.id == _idList[0])
      if (!e.id) {
        this.filterBrandList = this.carSeriesData
        return
      }
      this.$nextTick(() => {
        if (_target) {
          this.curBrandInfo = {
            id: _target?.id,
            name: _target?.name
          }
          this.filterBrandList = [_target]
          if (_idList[1]) {
            const _sonTarget = _target.childs.find(
              (item) => item.id == _idList[1]
            )
            if (_sonTarget) {
              this.curSonBrandInfo = {
                id: _sonTarget.id,
                name: _sonTarget.name
              }
              this.sonBrandList = [_sonTarget]
              if (_idList[2]) {
                const _carTarget = _sonTarget.childs.find(
                  (item) => item.id == _idList[2]
                )
                if (_carTarget) this.carSeriesList = [_carTarget]
              } else {
                this.carSeriesList = _sonTarget?.childs || []
              }
            }
          } else {
            this.curSonBrandInfo = {
              id: _target?.childs?.[0]?.id,
              name: _target?.childs?.[0]?.name
            }
            this.sonBrandList = _target.childs || []
            this.carSeriesList = this.sonBrandList?.[0]?.childs || []
          }
        }
      })
    },
    changeBrandStatus(e, row, idx) {
      this.curBrandInfo = {
        id: row.id,
        name: row.name
      }
      this.sonBrandList = row?.childs || []
      this.curSonBrandInfo = {
        id: row?.childs?.[0]?.id,
        name: row?.childs?.[0]?.name
      }
      this.carSeriesList = row?.childs?.[0]?.childs || []
      const { id: rootId, name: rootName } = row
      if (this.selectdData[rootId]) {
        this.$delete(this.selectdData, rootId)
      } else {
        const targetObj = {}
        row?.childs.forEach((item) => {
          const { id: parentId, name: parentName } = item
          targetObj[parentId] = []
          item?.childs?.forEach((ele) => {
            targetObj[item.id].push({
              rootId,
              rootName,
              parentId,
              parentName,
              id: ele.id,
              name: ele.name
            })
          })
        })
        this.$set(this.selectdData, rootId, targetObj)
      }
      console.log(this.selectdData)
    },
    changeSonBrandStatus(e, row, idx) {
      if (this.singleChoice && this.isClearData) {
        this.selectdData = {}
        this.isClearData = false
      }
      this.curSonBrandInfo = {
        id: row.id,
        name: row.name
      }
      this.carSeriesList = row?.childs
      const { id: rootId, name: rootName } = this.curBrandInfo
      if (this.selectdData[rootId]) {
        if (this.selectdData[rootId][row.id]) {
          this.$delete(this.selectdData[rootId], row.id)
        } else {
          const carList = []
          row?.childs?.forEach((ele) => {
            carList.push({
              rootId,
              rootName,
              parentId: row.id,
              parentName: row.name,
              id: ele.id,
              name: ele.name
            })
          })
          this.$set(this.selectdData[rootId], row.id, carList)
        }
      } else {
        const targetObj = {
          [row.id]: []
        }
        row?.childs?.forEach((ele) => {
          targetObj[row.id].push({
            rootId,
            rootName,
            parentId: row.id,
            parentName: row.name,
            id: ele.id,
            name: ele.name
          })
        })
        this.$set(this.selectdData, rootId, targetObj)
      }
    },
    changeCarSeriesStatus(e, row, idx) {
      if (this.singleChoice && this.isClearData) {
        this.selectdData = {}
        this.isClearData = false
      }
      const { id: rootId, name: rootName } = this.curBrandInfo
      const { id: parentId, name: parentName } = this.curSonBrandInfo
      if (this.selectdData[rootId]) {
        if (this.selectdData[rootId][parentId]) {
          const targetIdx = this.selectdData[rootId][parentId]?.findIndex(
            (item) => item.id == row.id
          )
          if (targetIdx > -1) {
            this.selectdData[rootId][parentId].splice(targetIdx, 1)
          } else {
            this.selectdData[rootId][parentId].push({
              rootId,
              rootName,
              parentId,
              parentName,
              id: row.id,
              name: row.name
            })
          }
        } else {
          this.$set(this.selectdData[rootId], parentId, [
            {
              rootId,
              rootName,
              parentId,
              parentName,
              id: row.id,
              name: row.name
            }
          ])
        }
      } else {
        this.$set(this.selectdData, rootId, {
          [parentId]: [
            {
              rootId,
              rootName,
              parentId,
              parentName,
              id: row.id,
              name: row.name
            }
          ]
        })
      }
    },
    showSonBrandList(row) {
      if (row.id == this.curBrandInfo.id) return
      this.isClearData = true
      this.curBrandInfo = {
        id: row.id,
        name: row.name
      }
      this.curSonBrandInfo = {
        id: row?.childs?.[0]?.id,
        name: row?.childs?.[0]?.name
      }
      this.sonBrandList = row?.childs || []
      this.carSeriesList = row?.childs?.[0]?.childs || []
    },
    showCarSeriesList(row) {
      if (row.id == this.curSonBrandInfo.id) return
      this.curSonBrandInfo = {
        id: row.id,
        name: row.name
      }
      this.carSeriesList = row?.childs
    },
    isChecked(item, type) {
      const { id } = item
      const { id: rootId } = this.curBrandInfo
      const { id: parentId } = this.curSonBrandInfo
      if (type == 1) {
        const keyList = Object.keys(this.selectdData[id] || [])
        return keyList.length > 0
      } else if (type == 2 && rootId && this.selectdData[rootId]) {
        return this.selectdData[rootId][id]?.length > 0
      } else if (
        type == 3 &&
        rootId &&
        parentId &&
        this.selectdData[rootId] &&
        this.selectdData[rootId][parentId]
      ) {
        return !!this.selectdData[rootId][parentId]?.find(
          (item) => item.id == id
        )
      } else {
        return false
      }
    },
    isIndeterminate(item, type) {
      const { id } = item
      const len = item?.childs?.length
      if (type == 1 && this.selectdData[id]) {
        return (
          Object.keys(this.selectdData[id])?.length != len &&
          Object.keys(this.selectdData[id])?.length != 0
        )
      } else if (
        type == 2 &&
        this.curBrandInfo.id &&
        this.selectdData[this.curBrandInfo.id] &&
        this.selectdData[this.curBrandInfo.id][id]
      ) {
        return (
          Object.keys(this.selectdData[this.curBrandInfo.id][id])?.length !=
          len &&
          Object.keys(this.selectdData[this.curBrandInfo.id][id])?.length != 0
        )
      } else {
        return false
      }
    },
    // 获取数据源
    // getCarSeriesData() {
    //     this.$apis.case.getCarSeriesApi({needCarSeries:true}).then(res => {
    //         if(res?.length){
    //             this.carSeriesData = res
    //             this.filterBrandList = res
    //             this.flatData(res)
    //         }
    //     }).finally(end =>{
    //         this.loading = false
    //     })
    // },
    // flatData(list, parentId){
    //     list?.forEach(item =>{
    //         let id = `${parentId ? parentId + '-': ''}${item.id}`
    //         this.searchList.push({id, name:item.name})
    //         this.flatData(item.childs, id)
    //     })
    // },
    handleConfirm() {
      // console.log(this.selectdData)
      this.$emit("handleConfirm", this.selectdData)
    },
    handleClose() {
      this.$emit("handleClose")
    }
  }
}
</script>

<style lang="scss" scoped>
.search-wrap {
  margin-bottom: 12px;

  .el-input {

    &,
    .el-input__inner {
      height: 36px;
      width: 432px;
    }
  }
}

.loading-wrap {
  height: 200px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .loading-img {
    width: 50px;
    height: 50px;
    animation: loading 2s linear infinite;
  }

  @keyframes loading {
    from {
      transform: rotate(0deg);
    }

    to {
      transform: rotate(360deg);
    }
  }
}

.car-container {
  border: 1px solid #e8e8ea;
  height: 476px;
  display: flex;

  .el-checkbox {
    display: block;
  }

  .wrap {
    flex: 1;
    border-left: 1px solid #e8e8ea;
    overflow-y: auto;
    padding: 8px 0px 8px 12px;

    &:first-child {
      border: none;
    }

    .item {
      height: 28px;
      line-height: 28px;
      font-size: 14px;
      font-weight: 400;
      color: #333333;
      cursor: pointer;
    }

    .flex-center {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .son-brand-wrap {
      width: 100%;
      display: flex;
      align-items: center;

      .son-brand-label {
        margin-left: 4px;
        flex: 1;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    // .son-brand-checkbox{
    //     &, .el-checkbox__label{
    //         width: 100%;
    //     }
    //     &, .son-brand-label{
    //         display: flex;
    //         align-items: center;
    //     }
    //     .son-brand-label{
    //         justify-content: space-between;
    //     }
    // }
  }

  // .curItem {
  //   color: #5850ff !important;
  // }
}
</style>