<template>
    <view class="ResultBox" style="
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;">
        <view style="width: 100%; height: 740rpx;">
            <canlendar v-if="showTimePopup" 
            :defaultDate="dayObj.start"  
            @finish="chosenDateResult" title="日期和人数"></canlendar>
        </view>
        <view class="column" style="background: #ecf1f4; padding: 20rpx 0 50rpx 0;height: 1rpx;flex: 1;">
            <view class="row line-flexBoxRoom">
                <text style="color: #111; font-weight: 500">房间</text>
                <text style="
                    font-size: 22rpx;
                    color: #999;
                    margin-left: 30rpx;
                    flex: 1;
                    "></text>
                <u-number-box size="28" :min="1" :max="100000" v-model="parameters.rooms"
                @change="getRooms"></u-number-box>
            </view>
            <scroll-view :scroll-y="true" class="hotelScroll" style="height: 1rpx;flex: 1;background: #fff;">
                <view class="line-flexBox">
                    <view class="column" v-for="(item,index) in parameters.searchroomGroup"
                    style="padding-bottom: 20rpx;">
                        <view class="line-flex line-flexTitle" style="height: 29px;margin-right: 10rpx;">房间{{ item.roomNum }}</view>
                        <view class="col">
                            <view class="row line-flex" style="margin-left: 20rpx;">
                            <view style="margin-right: 10rpx;">
                                <text style="font-size: 28rpx; color: #111; font-weight: 500">成人</text>
                                <!--
                                <text style="
                                    font-size: 22rpx;
                                    color: #999;
                                    margin-left: 30rpx;
                                    flex: 1;
                                    ">18周岁以上</text>-->
                            </view>
                            <u-number-box size="28" :min="1" 
                            :max="9" 
                            v-model="item.numberOfAdults"
                            @change="(e)=>{crChange(e,index)}"></u-number-box>
                            </view>
                            <view class="row row-sbas-n line-flex" style="margin-left: 20rpx;">
                                <view class="row">
                                    <view style="margin-right: 10rpx;">
                                        <text style="font-size: 28rpx; color: #111; font-weight: 500">儿童
                                            <!--(占床)-->
                                        </text>
                                        <!--
                                        <text style="
                                            font-size: 22rpx;
                                            color: #999;
                                            margin-left: 30rpx;
                                            flex: 1;
                                            ">2-18周岁(不含)</text>-->
                                    </view>
                                    <u-number-box size="28" :min="0" :max="5" 
                                    v-model="item.numberOfChildren"
                                    @change="(e)=>{etChange(e,index)}"
                                    style="margin-right: 10rpx;"
                                    ></u-number-box>
                                </view>
                                <view class="row">
                                    <view class="row" v-for="(items,indexs) in item.ChildAgeDetails" :key="indexs">
                                        <uni-section title="" type="line">
                                            <uni-data-select :ref="`select_${index}`"
                                                :placeholder="`年龄`"
                                                v-model="items.number"
                                                :localdata="TypeList"
                                                :clear="false"
                                                @change="(e)=>{ageChange(e,index,indexs)}"
                                            ></uni-data-select>
                                        </uni-section>
                                        <u-icon name="arrow" :size="32" color="#ccc" style="position: relative;top: -9rpx;"></u-icon>
                                    </view>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </scroll-view>
            
            
            <!--
            <view class="row line-flex" style="padding: 30rpx; background: #fff">
                    <text style="font-size: 28rpx; color: #111; font-weight: 500">儿童(不占床)</text>
                        <text style="
                    font-size: 22rpx;
                    color: #999;
                    margin-left: 30rpx;
                    flex: 1;
                    ">2-18周岁(不含)</text>
                    <u-number-box size="28" :min="0" :max="100000" v-model="parameters.childrenNumberBZC"></u-number-box>
            </view>-->
        </view>
    </view>
</template>
<script>
import canlendar from "./index";
export default {
    props:['msg','showTimePopup'],
    components: {
        canlendar
    },
    data() {
        return {
            parameters: {
                rooms: 1,
                adultsNumber:2,
                childrenNumberZC:0,
                childrenNumberBZC:0,
                peoples: 2,
            },
            TypeList: [],
            current: -1,
            dayObj: {},
        }
    },
    watch:{
        msg: {
			handler (val, oldval) {
                if(val){
                    this.parameters = val
                }
            },
            deep: true,
        },
        showTimePopup: {
			handler (val, oldval) {
            },
            deep: false,
            immediate: true
        },
    },
    created(){
        for(let i=1;i<=18;i++){
            this.TypeList.push({
                text: i+'岁',
                value: i
            })
        }
    },
    mounted() {
        uni.getStorage({
            key: "Time",
            success: (res) => {
                let obj = JSON.parse(res.data);
                this.dayObj = obj
            },
        });
    },
    methods: {
        getRooms(e){
            if(e.value>this.parameters.rooms||e.value<this.parameters.rooms){
                if(e.value>this.parameters.rooms){
                    this.parameters.searchroomGroup.push({
                        roomNum: e.value,
                        numberOfAdults: 2,
                        numberOfChildren: 0,
                        ChildAgeTexts: [],
                        ChildAgeDetails: [],
                    })
                }
                if(e.value<this.parameters.rooms){
                    this.parameters.searchroomGroup.splice(e.value,1)
                }
                this.parameters.rooms = e.value;
                this.peopleChange()
            }
        },
        eitAge(index,indexs){
            this.current = indexs
            this.$refs[`select_${index}`][0].toggleSelector();
        },
        ageChange(e,index,indexs){
            this.parameters.searchroomGroup[index].ChildAgeDetails[indexs].number = e
            this.parameters.searchroomGroup[index].ChildAgeTexts = []
            this.parameters.searchroomGroup[index].ChildAgeDetails.forEach((item,indexs)=>{
            	this.parameters.searchroomGroup[index].ChildAgeTexts.push(item.number)		
            })
        },
        etChange(e,index){
            this.parameters.searchroomGroup[index].numberOfChildren = e.value;
            if(e.value<this.parameters.searchroomGroup[index].ChildAgeDetails.length){
                let num = this.parameters.searchroomGroup[index].ChildAgeDetails.length-e.value
                for(let i=0;i<num;i++){
                    let indexs = this.parameters.searchroomGroup[index].ChildAgeDetails.length-1
                    this.parameters.searchroomGroup[index].ChildAgeDetails.splice(indexs,1)
                    let length = this.parameters.searchroomGroup[index].ChildAgeDetails.length
                    if(length==0){
                        this.parameters.searchroomGroup[index].ChildAgeTexts = []
                    }else{
                        this.parameters.searchroomGroup[index].ChildAgeTexts = []
                        this.parameters.searchroomGroup[index].ChildAgeDetails.forEach((item,indexs)=>{
                            this.parameters.searchroomGroup[index].ChildAgeTexts.push(item.number)	
                        })
                    }
                }
            }else if(e.value>this.parameters.searchroomGroup[index].ChildAgeDetails.length){
                let num = e.value-this.parameters.searchroomGroup[index].ChildAgeDetails.length
                for(let i=0;i<num;i++){
                    this.parameters.searchroomGroup[index].ChildAgeDetails.push({
                        number: 1,
                    })
                    this.parameters.searchroomGroup[index].ChildAgeTexts.push(1)
                }
            }
            this.peopleChange()
        },
        crChange(e,index){
            this.parameters.searchroomGroup[index].numberOfAdults = e.value;
            this.peopleChange()
        },
        peopleChange(){
            this.parameters.peoples = 0
            this.parameters.adultsNumber = 0
            this.parameters.childrenNumberZC = 0
            this.parameters.searchroomGroup.forEach((item,index)=>{
                this.parameters.peoples+= (Number(item.numberOfAdults) + Number(item.numberOfChildren))
                this.parameters.adultsNumber+= Number(item.numberOfAdults)
                this.parameters.childrenNumberZC+= Number(item.numberOfChildren)
            })
        },
        chosenDateResult(obj) {
            for(let i=0;i<this.parameters.searchroomGroup.length;i++){
                let list = this.parameters.searchroomGroup[i]
                if(list.numberOfChildren>list.ChildAgeDetails.length){
                    return uni.showToast({
                        title: `请选择房间${i+1} 儿童年龄`,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
            obj.fewDays = this.dayObj.fewDays
            this.$emit('chosenDateResult',this.parameters,obj)
        },
    }
}
</script> 
<style lang="scss" scoped>
	@import url("@/asset/css/flex.css");
.line-flexBoxRoom{
    padding: 30rpx 30rpx 20rpx 30rpx;
    background: #fff;
    font-size: 32rpx;
}
.line-flexBox{
    padding: 0 30rpx;
    background: #fff;
}
.line-flex{
    font-size: 28rpx;
}
.line-flex.line-flexTitle{
    font-size: 32rpx;
    color: #B99846;
}

</style>