<template>
    <div>
        <transition name='fade'>
            <div v-if="showSreen" class="box" @click.stop="changeShow">
                <div class="screenTop" v-if="changeType!='筛选'">
                    <div class="screen-item" :class="[changeType=='区域'?'active':'']" @click.stop="changechooseType('区域')">
                        <span>{{regionValue||chooseCity||'全'+this.locationInfo.fullName.split('市')[0]}}</span>
                        <img src="@/assets/icon_xiala_pre.png" v-if="changeType=='区域'" alt="">
                        <img src="@/assets/icon_xiala.png" v-else alt="">
                    </div>
                    <div class="screen-item" :class="[changeType=='户型'?'active':'']" @click.stop="changechooseType('户型')">
                        <span>户型</span>
                        <img src="@/assets/icon_xiala_pre.png" v-if="changeType=='户型'" alt="">
                        <img src="@/assets/icon_xiala.png" v-else alt="">
                    </div>
                    <div class="screen-item" :class="[changeType=='租金'?'active':'']" @click.stop="changechooseType('租金')">
                        <span>租金</span>
                        <img src="@/assets/icon_xiala_pre.png" v-if="changeType=='租金'" alt="">
                        <img src="@/assets/icon_xiala.png" v-else alt="">
                    </div>
                    <div class="screen-item" :class="[changeType=='筛选'?'active':'']" @click.stop="changechooseType('筛选')">
                        <span>筛选</span>
                        <img src="@/assets/icon_xiala_pre.png" v-if="changeType=='筛选'" alt="">
                        <img src="@/assets/icon_xiala.png" v-else alt="">
                    </div>
                </div>
            </div>
        </transition>

        <transition name='slide-up'>
            <div v-if="showSreen&&changeType=='区域'" class="selectContent slelectFlex">
                <div class="select-item">
                    <div :class="[chooseRegion=='附近'?'active':'']" @click.stop="chooseRegion='附近'">附近</div>
                    <div :class="[chooseRegion=='区域'?'active':'']" @click.stop="chooseRegion='区域'">区域</div>
                    <div :class="[chooseRegion=='地铁'?'active':'']" @click.stop="chooseRegion='地铁'">地铁</div>
                </div>
                <div class="select-item" v-if="chooseRegion=='附近'">
                    <div :class="[regionValue==''?'active':'']" @click.stop="region('')">不限</div>
                    <div :class="[regionValue=='3km'?'active':'']" @click.stop="region('3km')">3km</div>
                    <div :class="[regionValue=='2km'?'active':'']" @click.stop="region('2km')">2km</div>
                    <div :class="[regionValue=='1km'?'active':'']" @click.stop="region('1km')">1km</div>
                </div>
                <div class="select-item" v-if="chooseRegion=='区域'">
                    <div :class="[regionValue==''?'active':'']" @click.stop="region('')">不限</div>
                    <div v-for="item in cityList" :class="[(regionValue==item.fullName||chooseCity==item.fullName)?'active':'']" @click.stop="regionNotColse(item.fullName)">{{item.fullName}}</div>
                </div>
                <div class="select-item" v-if="chooseCity&&chooseRegion=='区域'">
                    <div :class="[regionValue==''?'active':'']" @click.stop="region(chooseCity)">不限</div>
                    <div v-for="item in cityList[nowChooseCityIndex].children" :class="[regionValue==item.fullName?'active':'']" @click.stop="region(item.fullName)">{{item.fullName}}</div>
                </div>
                <div class="select-item" v-if="chooseRegion=='地铁'">
                    <div :class="[regionValue==''?'active':'']" @click.stop="region('')">不限</div>
                    <div v-for="item in metroList" :class="[(regionValue==item.metroName||chooseCity==item.metroName)?'active':'']" @click.stop="regionNotColse(item.metroName)">{{item.metroName}}</div>
                </div>
                <div class="select-item" v-if="chooseCity&&chooseRegion=='地铁'">
                    <div :class="[regionValue==''?'active':'']" @click.stop="region(chooseCity)">不限</div>
                    <div v-for="item in metroList[nowChooseCityIndex].children" :class="[regionValue==item.stationName?'active':'']" @click.stop="region(item.stationName)">{{item.stationName}}</div>
                </div>
            </div>
        </transition>
        <transition name='slide-up'>
            <div v-if="showSreen&&changeType=='户型'" class="selectContent typeselectContent">
                <div class="typeList">
                    <div class="typeTitle">
                        出租类型
                    </div>
                    <div class="typeContent">
                        <div class="typeItem" :class="[!myrentType?'active':'']" @click.stop="myrentType=''">不限</div>
                        <div class="typeItem" :class="[myrentType==item?'active':'']" v-for="item in rentTypes" @click.stop="myrentType=item">{{item}}</div>
                    </div>
                </div>
                <div class="typeList">
                    <div class="typeTitle">
                        户型
                    </div>
                    <div class="typeContent">
                        <div class="typeItem" :class="[!myhuoseTypes?'active':'']" @click.stop="myhuoseTypes=''">不限</div>
                        <div class="typeItem" :class="[myhuoseTypes==item?'active':'']" v-for="item in huoseTypes" @click.stop="myhuoseTypes=item">{{item}}</div>
                    </div>
                </div>
                <div style="flex-grow:1"></div>
                <div class="btncontent">
                    <gm-button :type='"primary"' @click="changeHouseType">确认</gm-button>
                </div>
            </div>
        </transition>
        <transition name='slide-up'>
            <div v-if="showSreen&&changeType=='租金'" class="selectContent rentContent">
                <div class="rentList">
                    <div class="rent-item" @click.stop="changeRent('')" :class="[(myRent==''&&!chooseRent)?'active':'']">不限</div>
                    <div class="rent-item" :class="[(myRent==item&&!chooseRent)?'active':'']" v-for="item in rentList" @click.stop="changeRent(item)">{{item}}</div>
                </div>
                <div class="rent">{{chooseRent?chooseRent:nowRent}}</div>

                <gm-slide :section='nowRent' @changeNowRent='changeNowRent'></gm-slide>
                <div style="flex-grow:1"></div>
                <div class="btncontent">
                    <gm-button :type='"primary"' @click="changeRent(chooseRent)">确认</gm-button>
                </div>
            </div>
        </transition>
        <transition name='slide-right'>
            <div v-if="showSreen&&changeType=='筛选'" class="selectContent screenAll">
                <div class="chooseContent">
                    <div class="selectList">
                        <div class="selectTitle">朝向</div>
                        <div class="content">
                            <div class="item" :class="[faced?'':'active']" @click.stop="faced=''">不限</div>
                            <div class="item" :class="[faced==item.facedName?'active':'']" @click.stop="faced=item.facedName" v-for="item in facedList">{{item.facedName}}</div>
                        </div>
                    </div>
                    <div class="selectList">
                        <div class="selectTitle">面积</div>
                        <div class="content">
                            <div class="item" :class="[area?'':'active']" @click.stop="area=''">不限</div>
                            <div class="item" :class="[area.name==item.name?'active':'']" @click.stop="area=item" v-for="item in areaList">{{item.name}}</div>
                        </div>
                    </div>
                    <div class="selectList">
                        <div class="selectTitle">房源特色</div>
                        <div class="content">
                            <div class="item" :class="[houseFeature.length!=0?'':'active']" @click.stop="houseFeature=[]">不限</div>
                            <div class="item" :class="[houseFeature.indexOf(item.code)!=-1?'active':'']" @click.stop="clickHouseFeature(item.code)" v-for="item in houseFeatureList">{{item.name}}</div>
                        </div>
                    </div>
                </div>
                <div class="btn">
                    <gm-button style="width:28%" type='dark' @click="changeShow">取消</gm-button>
                    <gm-button style="width:68%" type='primary' @click="sureSelect">确认</gm-button>
                </div>
            </div>
        </transition>
    </div>

</template>
<script>
import { mapState } from "vuex";
import GmButton from '@/components/Button'
import GmSlide from '@/components/Slide'
export default {
    name: 'screenRegion',
    props: ['showSreen', 'changeType', 'regionValue', 'rentType', 'huoseType', 'rent', 'faced', 'area', 'houseFeature'],
    data() {
        return {
            chooseRegion: '附近',
            rentTypes: ['整租', '合租', '品牌公寓'],
            huoseTypes: ['一室', '二室', '三室', '四室及以上'],
            rentList: ['1000元以下', '1000-2000元', '2000-2500元', '2500-3000元', '3000元以上'],
            myrentType: '',
            myhuoseTypes: '',
            myRent: '',
            chooseRent: '',
            cityList: [],
            chooseCity: "",
            metroList: [],
            facedList: ['东', '南', '西', '北', '南北'],
            areaList: [{
                name: '≤30m²',
                minArea: 0,
                maxArea: 30
            }, {
                name: '30-60m²',
                minArea: 30,
                maxArea: 60
            }, {
                name: '60-90m²',
                minArea: 60,
                maxArea: 90
            }, {
                name: '90-120m²',
                minArea: 90,
                maxArea: 120
            }, {
                name: '≥120m²',
                minArea: 120,
                maxArea: 99999
            }],
            houseFeatureList: []
        }
    },
    watch: {
        rentType() {
            this.myrentType = this.rentType
        },
        huoseType() {
            this.myhuoseTypes = this.huoseType
        },
        rent() {
            this.myRent = this.rent
        },
        locationInfo() {
            if (this.locationInfo.code) {
                this.$http.get({
                    url: '/districtInfo/all',
                    data: {
                        cityCode: this.locationInfo.code
                    }
                }).then(res => {

                    if (res.success) {
                        res.data.forEach(item => {
                            item.children = []
                        })
                        this.cityList = res.data
                    }
                });

                this.$http.get({
                    url: '/metroInfo/all',
                    data: {
                        cityCode: this.locationInfo.code
                    }
                }).then(res => {

                    if (res.success) {
                        res.data.forEach(item => {
                            item.children = []
                        })
                        this.metroList = res.data
                    }
                });
            }
        },
        chooseCity() {
            if (this.chooseCity && this.chooseRegion == '区域') {
                this.$http.get({
                    url: '/districtInfo/all',
                    data: {
                        districtCode: this.cityList[this.nowChooseCityIndex].code
                    }
                }).then(res => {

                    if (res.success) {
                        this.cityList.forEach((item, index) => {
                            if (item.fullName == this.chooseCity) {
                                this.cityList[index].children = res.data
                            }
                        })

                    }
                });
            }
            if (this.chooseCity && this.chooseRegion == '地铁') {
                this.$http.get({
                    url: '/metroStation/all',
                    data: {
                        metroCode: this.metroList[this.nowChooseCityIndex].code
                    }
                }).then(res => {

                    if (res.success) {
                        this.metroList[this.nowChooseCityIndex].children = res.data
                    }
                });
            }
        }
    },
    created() {
        // document.body.appendChild(this.$el);
        console.log(this.rent)
        this.myRent = this.rent
        this.myhuoseTypes = this.huoseType
        this.myrentType = this.rentType

        setTimeout(() => {
            if (this.locationInfo.code) {
                this.$http.get({
                    url: '/districtInfo/all',
                    data: {
                        cityCode: this.locationInfo.code
                    }
                }).then(res => {

                    if (res.success) {
                        res.data.forEach(item => {
                            item.children = []
                        })
                        this.cityList = res.data
                    }
                });

                this.$http.get({
                    url: '/metroInfo/all',
                    data: {
                        cityCode: this.locationInfo.code
                    }
                }).then(res => {
                    if (res.success) {
                        res.data.forEach(item => {
                            item.children = []
                        })
                        this.metroList = res.data
                    }
                });
            }
        }, 500)

        this.$http.get({
            url: '/houseFeature/all',
            data: {}
        }).then(res => {
            if (res.success) {
                this.houseFeatureList = res.data
            }
        });

        this.$http.get({
            url: '/facedInfo/all',
            data: {
            }
        }).then(res => {
            this.$store.commit('updateLoading', false)
            if (res.success) {
               this.facedList=res.data
            }
        });


    },
    computed: {
        ...mapState(["locationInfo"]),
        nowRent() {
            var value = '0元-无限'
            if (this.myRent) {
                value = this.myRent
            }
            return value
        },
        nowChooseCityIndex() {
            var cityIndex = 0
            if (this.chooseRegion == '区域') {
                if (this.chooseCity) {
                    this.cityList.forEach((item, index) => {
                        if (item.fullName == this.chooseCity) {
                            cityIndex = index
                        }
                    })
                }
            }
            else if (this.chooseRegion == '地铁') {
                if (this.chooseCity) {
                    this.metroList.forEach((item, index) => {
                        if (item.metroName == this.chooseCity) {
                            cityIndex = index
                        }
                    })
                }
            }


            return cityIndex
        }
    },
    methods: {
        changeShow() {
            this.$emit('changeShow')
        },
        changechooseType(value) {
            this.$emit('changechooseType', value)
        },
        region(value) {

            if (value == '') {
                this.chooseCity = ''
            }
            var jsonp = this.getCode(value)
            jsonp.name = value
            this.$emit('changeRegion', jsonp)
            this.changeShow()
        },
        regionNotColse(value) {
            this.chooseCity = value
            this.regionValue = ''
            // this.$emit('changeRegion', value)
        },
        changeHouseType() {
            this.$emit('changeHouseType', { rentType: this.myrentType, huoseType: this.myhuoseTypes })
            this.changeShow()
        },
        changeRent(value) {
            console.log(value)
            if (this.rentList.indexOf(value) != -1 || !value) {
                this.chooseRent = ''
            }
            this.$emit('changeRent', value)
            this.changeShow()
        },
        changeNowRent(value) {
            this.chooseRent = value
        },
        getCode(value) {
            var jsonp = ''
            if (this.chooseRegion == '区域') {
                this.cityList.forEach(item => {
                    if (value == item.fullName) {
                        jsonp = item
                        jsonp.SearchType = 'districtCode'
                    }
                })
                if (!jsonp) {
                    this.cityList[this.nowChooseCityIndex].children.forEach(item => {
                        if (value == item.fullName) {
                            jsonp = item
                            jsonp.SearchType = 'streetCode'
                        }
                    })
                }
            }
            else if (this.chooseRegion == '地铁') {
                this.metroList.forEach(item => {
                    if (value == item.metroName) {
                        jsonp = item
                        jsonp.SearchType = 'lineCode'
                    }
                })
                if (!jsonp) {
                    this.metroList[this.nowChooseCityIndex].children.forEach(item => {
                        if (value == item.stationName) {
                            jsonp = item
                            jsonp.SearchType = 'stationCode'
                        }
                    })
                }
            }
            if (!jsonp) {
                jsonp = {}
            }
            return jsonp
        },
        sureSelect() {
            var jsonp = {
                faced: this.faced,
                area: this.area,
                houseFeature: this.houseFeature,
            }

            this.$emit('changeSelect', jsonp)
            this.changeShow()
        },
        clickHouseFeature(code) {
            if (this.houseFeature.indexOf(code) == -1) {
                this.houseFeature.push(code)
            }
            else {
                this.houseFeature.splice(this.houseFeature.indexOf(code), 1)
            }

        }

    },
    destroyed() {
        // document.body.removeChild(this.$el);
    },
    components: { GmButton, GmSlide }
}
</script>
<style lang="less" scoped>
.box {
    position: fixed;
    z-index: 500;
    background: rgba(0, 0, 0, 0.5);
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
}

.screenTop {
    height: 0.88rem;
    background: rgba(255, 255, 255, 1);
    display: flex;
    border-bottom: 0.02rem solid #f2f4f5;
    box-sizing: border-box;

    .screen-item {
        flex-grow: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        span {
            font-size: 0.26rem;
            color: rgba(51, 51, 51, 1);
            line-height: 0.36rem;
        }
        img {
            width: 0.28rem;
            height: 0.28rem;
            margin-left: 0.04rem;
        }

        &.active {
            span {
                color: rgba(21, 168, 170, 1);
            }
        }
    }
}

.selectContent {
    height: 8rem;
    background: rgba(255, 255, 255, 1);
    position: fixed;
    width: 100%;
    top: 0.88rem;
    z-index: 550;
    overflow: hidden;
}

.typeselectContent {
    display: flex;
    flex-direction: column;
    .typeList {
        padding: 0.4rem 0.3rem 0;
        .typeTitle {
            font-size: 0.32rem;
            font-weight: bold;
            color: rgba(51, 51, 51, 1);
            line-height: 0.44rem;
        }

        .typeContent {
            display: flex;
            // justify-content: space-between;
            flex-wrap: wrap;

            .typeItem {
                text-align: center;
                line-height: 0.72rem;
                background: rgba(242, 244, 245, 1);
                border-radius: 0.08rem;
                font-size: 0.28rem;
                color: rgba(51, 51, 51, 1);
                width: 30%;
                margin-right: 4.5%;
                margin-top: 0.2rem;

                &:nth-child(3n) {
                    margin-right: 0;
                }

                &.active {
                    background: rgba(21, 168, 170, 1);
                    color: #fff;
                }
            }
        }
    }

    .btncontent {
        margin: 0.3rem;
    }
}

.slelectFlex {
    display: flex;
    .select-item {
        flex-grow: 1;
        overflow: auto;
        div {
            padding: 0.28rem 0.3rem;
            font-size: 0.3rem;
            color: rgba(51, 51, 51, 1);
            line-height: 0.42rem;

            &.active {
                background: rgba(242, 244, 245, 1);
                font-size: 0.3rem;
                color: rgba(21, 168, 170, 1);
            }
        }

        &:not(:last-child) {
            border-right: 1px solid #f2f4f5;
        }
    }
}

.rentContent {
    display: flex;
    flex-direction: column;
    .rentList {
        height: 4.8rem;
        overflow: auto;
        .rent-item {
            height: 0.8rem;
            text-align: center;
            line-height: 0.8rem;
            font-size: 0.3rem;
            color: rgba(51, 51, 51, 1);

            &.active {
                color: rgba(21, 168, 170, 1);
            }
        }
    }
    .rent {
        height: 0.78rem;
        text-align: center;
        line-height: 0.78rem;
        font-size: 0.26rem;
        font-weight: bold;
        color: rgba(21, 168, 170, 1);
        border-top: 0.02rem solid #f2f4f5;
    }

    .btncontent {
        margin: 0.3rem;
    }
}

.screenAll {
    width: 6.6rem;
    height: 100%;
    top: 0;
    right: 0;
    display: flex;
    flex-direction: column;

    .chooseContent {
        flex-grow: 1;
        overflow-y: auto;
    }

    .btn {
        display: flex;
        padding: 0.12rem 0.3rem;
        box-shadow: 0rem -0.04rem 0.08rem 0rem rgba(0, 0, 0, 0.1);
        background: rgba(255, 255, 255, 1);
        justify-content: space-between;
    }
}
.selectList {
    padding: 0 0 0 0.3rem;
    .selectTitle {
        font-size: 0.32rem;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
        line-height: 0.44rem;
        padding: 0.4rem 0 0;
    }
    .content {
        display: flex;
        flex-wrap: wrap;

        .item {
            height: 0.72rem;
            background: #f2f4f5;
            border-radius: 0.08rem;
            line-height: 0.72rem;
            text-align: center;
            width: 30%;
            margin-right: 3%;
            margin-top: 0.2rem;
            color: rgba(51, 51, 51, 1);
            font-size: 0.26rem;

            &.active {
                background: rgba(21, 168, 170, 1);
                color: rgba(255, 255, 255, 1);
            }
        }
    }
}

.slide-up-enter,
.slide-up-leave-active {
    height: 0rem;
}

.slide-up-enter-active,
.slide-up-leave-active {
    transition: all 0.5s;
}

.slide-right-enter,
.slide-right-leave-active {
    width: 0rem;
}

.slide-right-enter-active,
.slide-right-leave-active {
    transition: all 0.5s;
}

.fade-enter,
.fade-leave-active {
    opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
    transition: all 0.5s;
}
</style>


