selectCarDialog.vue 11.4 KB
<template>
    <GLDialog title="选择车系"
        mode="mid"
        :visible.sync="dialogVisible"
        :before-close="handleClose"
        :close-on-click-modal="false"
        @onOk="handleConfirm"
        @onCancel="handleClose">
        <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 carSeriesData" :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'
  export default {
    name: 'selectStore',
    data() {
      return {
        url,
        loading: true,
        carSeriesData: [],
        sonBrandList: [],
        carSeriesList: [],
        selectdData: {},
        curBrandInfo: {
            id: '',
            name: ''
        },
        curSonBrandInfo: {
            id: '',
            name: ''
        },
        isClearData: true
      }
    },
    computed:{
    },
    props: {
        dialogVisible:{
            type: Boolean,
            default: false
        },
        singleChoice:{
            type: Boolean,
            default: false
        },
        echoData: {
            type: Array,
            default: ()=>[]
        },
    },
    created(){
        this.echoDataFn()
    },
    mounted() {
        this.getCarSeriesData()
    },
  
    methods:{
        echoDataFn(){
            let obj = {}
            this.echoData.forEach(item => {
                let { brandId, brandName, childBrandId, childBrandName, vehicleSeriesId, vehicleSeriesName} = item
                if(obj[brandId]){
                    if(obj[brandId][childBrandId]){
                        obj[brandId][childBrandId].push({rootId:brandId, rootName: brandName, parentId: vehicleSeriesId, parentName : childBrandName, id: vehicleSeriesId, name: vehicleSeriesName })
                    }else{
                        obj[brandId][childBrandId] = []
                        obj[brandId][childBrandId].push({rootId:brandId, rootName: brandName, parentId: vehicleSeriesId, parentName : childBrandName, id: vehicleSeriesId, name: vehicleSeriesName })
                    }
                }else{
                    obj[brandId] = {}
                    obj[brandId][childBrandId] = []
                    obj[brandId][childBrandId].push({rootId:brandId, rootName: brandName, parentId: vehicleSeriesId, parentName : childBrandName, id: vehicleSeriesId, name: vehicleSeriesName })
                }
            })
            this.selectdData = obj
        },
        changeBrandStatus(e, row, idx){
            const { id: rootId, name: rootName } = row
            if(this.selectdData[rootId]){
                this.$delete(this.selectdData, rootId)
            }else{
                let 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
            }
            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{
                    let 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{
                let 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.sonBrandList = row?.childs || []
            this.carSeriesList = []
        },
        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){
                let 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
                }
            }).finally(end =>{
                this.loading = false
            })
        },
        handleConfirm(){
            console.log(this.selectdData)
            this.$emit('handleConfirm', this.selectdData)
        },
        handleClose(){
            this.$emit('handleClose')
        }
    }
  }
</script>
  
<style lang="scss">

.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>