<template>
    <view class="bgF5"
    :class="[NewDataList.length==0||2>NewDataList.length?'height100vh':'height100']">
        <view class="bgFFF">
            <AirTicketHeader></AirTicketHeader>
            <view class="StartStopAddress" v-if="(AddressListAll&&AddressListAll.length>0)||(ArrivalCity&&ArrivalCity.length>0)">
                <view class="row-sb-n">
                    <view class="StartStopAddressText-box">
                        <view class="column">
                            <text class="fz24 color9999A6">出发地</text>
                            <view class="MT31 row-aic-n" @click="AddressSelection(1)">
                                <img class="inlineblock MR10" mode="widthFix" style="width: 18rpx;height: 22rpx"
                                src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1696743247000_254.png"/>
                                <text class="StartStopAddressText fz30 fontBold"
                                :class="[!OriginAddress.DepartCityName?'color9999A5':'']">
                                    {{OriginAddress.DepartCityName?OriginAddress.DepartCityName:'选择出发地'}}
                                </text>
                            </view>
                        </view>
                    </view>
                    <view class="flexS" style="width: 29rpx;height: 27rpx;margin: 0 37rpx;" 
                    @click="AddressSelection(0)">
                        <img class="inlineblock MR10" mode="widthFix" style="width: 29rpx;height: 27rpx"
                            src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1696737205000_556.png"/>
                    </view>
                    <view class="StartStopAddressText-box">
                        <view class="column">
                            <text class="fz24 color9999A6">目的地</text>
                            <view class="MT31 row-aic-n" @click="AddressSelection(2)">
                                <img class="inlineblock MR10" mode="widthFix" style="width: 18rpx;height: 22rpx"
                                src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1696743247000_254.png"/>
                                <text class="StartStopAddressText fz30 fontBold"
                                :class="[!OriginAddress.ArriveCityName?'color9999A5':'']">
                                {{OriginAddress.ArriveCityName?OriginAddress.ArriveCityName:'选择目的地'}}
                            </text>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="height-line1 MT20" ></view>
            </view>
            <view class="DepartureTime column">
                <text class="fz24 color9999A6">行程类型</text>
                <view class="fz30 fontBold MT31 MB20 row-aic-n">
                    <view v-for="(item,index) in typeList" 
                    :class="[index?'ML60':'']"
                    @click="getType(item)">
                        <uni-icons :color="msgAddress.TicketType==item.Id?'#2EC726':'#C6C1BC'" 
                        :type="msgAddress.TicketType==item.Id?'checkbox-filled':'circle'" size="18"></uni-icons>
                        <text class="ML20 fz30 fontBold">{{item.Name}}</text>
                    </view>
                </view>
                <view class="height-line1"></view>
                <view class="DepartureTimeList row-sb-n">
                    <view style="width: 590rpx;overflow: hidden;">
                        <scroll-view scroll-x style="width:100%;white-space: nowrap;">
                            <view class="row-aic-n" v-if="NewDateList&&NewDateList.length>0">
                                <template v-for="(item,index) in NewDateList">
                                    <view class="DepartureTimeListC flexS column-ajc fz20"
                                    :class="[StartDateObj.times==item.times?'bgE1C278':'active']"
                                    @click="clickSearch(item,1)">
                                        <text>{{item.time}}</text>
                                        <text class="MT10 MB10">周{{item.week}}</text>
                                        <text>
                                            <template v-if="item.Price>0&&item.B2BSellNum==0">售罄</template>
                                            <template v-else>{{item.Price>0?'¥'+item.Price:'无报价'}}</template>
                                        </text>
                                    </view>
                                </template>
                            </view>
                            <view class="row-w" v-else>
                                <view class="DepartureTimeListC flexS column-ajc fz20" style="width: 100%;">
                                    <text></text>
                                    <text class="MT10 MB10"></text>
                                    <text class="fz26 color9999A5">
                                        暂无查询到可出发日期
                                    </text>
                                </view>
                            </view>
                        </scroll-view>
                    </view>
                    <view style="height: 152rpx;width: 1px;background: #ECF1F4;box-shadow: -3px 0 3px -3px #aaa;"></view>
                    <view class="flexS row-ajc-w" style="width: 106rpx;height: 123rpx;"
                    @click="showTimePreviwe=true">
                        <img class="inlineblock" mode="widthFix" style="width: 30rpx;height: 66rpx"
                        src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1696744058000_738.png">
                    </view>
                </view>
            </view>
        </view>
        <view class="MT40"></view>
        <u-empty v-if="NewDataList.length==0" text="暂无数据" mode="data" padding-top="20"></u-empty>
        <scroll-view v-else :scroll-y="true" style="background-color: #f0f4f7;" 
            :enable-back-to-top="true" :enable-flex="true"
            @scrolltolower="lower" :style="{ 'height': `calc(100% - ${scrollHeight})`}">
            <AirTicketList :dataList="NewDataList"></AirTicketList>
            <view class="MT10 MB10">
                <u-loadmore :status="status" :load-text="loadText" :font-size="24" :margin-top="0" :margin-bottom="0" bg-color="#f0f4f7" />
            </view>
        </scroll-view>
        
        <u-popup
        v-model="showTimePreviwe"
        mode="bottom"
        border-radius="50"
        length="90%"
        :safe-area-inset-bottom="true"
        @close="OffPopup(1)">
            <canlendar
              @finish="chosenDateResult"
              :immediately="true"
              :defaultDate="StartDateObj.times"
              :priceList="dataList"
              title="选择日期"
            ></canlendar>
        </u-popup>
        <u-popup
        v-model="showAddressPreviwe"
        mode="bottom"
        border-radius="50"
        :safe-area-inset-bottom="true"
        @close="OffPopup(2)">
        <AddressGroup
            :OriginAddressObj="OriginAddress" 
            :dataList="AddressList"
            @RenewalAddres="RenewalAddres"></AddressGroup>
        </u-popup>

    </view>
</template>
<script>
    import AirTicketHeader from "./components/airTicketHeader.vue";
    import canlendar from "./components/sign/index.vue";
    import Address from "./components/address/index.vue";
    import AddressGroup from "./components/address/indexGroup.vue";
    import AirTicketList from "./components/airTicketList.vue";
    import { gegerateDates,getBeforeDate,getDateWeek } from "./dates.js";
    export default {
        components: {
            AirTicketHeader,
            canlendar,
            Address,
            AddressGroup,
            AirTicketList
        },
        data() {
            return {
                typeList:[
                    {Name:'往返',Id:2},
                    {Name:'单程',Id:1},
                ],
                AddressListAll:[],
                AddressList:[],
                AddressType: null,//1出发 2目的地
                OriginAddress:{
                    DepartCityName: '',
                    DepartCityId: 0,//343
                    ArriveCityName: '',
                    ArriveCityId: 0,//653
                },
                msgAddress:{
                    StartDate: '',
                    EndDate: '',
                    ArriveCityId: 0,
                    DepartCityId: 0,
                    AirLineID:'',
                    TicketType:2,//1单程 2往返
                },
                StartDateObj:{},
                NewDateList:[],
                dateList:[],
                priceList:[],
                activeId: 1,
                showAddressPreviwe: false,
                loading: false,
                msg:{
                    // pageIndex: 1,
                    // pageSize: 10,
                    StartDate: '',
                    EndDate: '',
                    DepartCityId: 0,//343
                    ArriveCityId: 0,//653
                    AirLineID: 0,
                    TicketType: 2,//1单程 2往返
                },
                pageCount: 0,
                showTimePreviwe: false,
                ConditionList:[
                    {Name:'综合排序',Id:1},
                    {Name:'耗时长短',Id:2},
                    {Name:'价格高低',Id:3},
                    {Name:'不要中转',Id:4},
                ],
                currentPrice:{},
                filterDateList: [],
                NewDataList:[],
                dataList:[],
                status: 'nomore',
                loadText: {
					loadmore: "轻轻上拉，加载更多",
					loading: "努力加载中",
					nomore: "没有更多了",
				},
                scrollHeight: 0,
                U: {},
                isToken: false,
            }
        },
        onLoad(options){
            this.msg.DepartCityId = this.msgAddress.DepartCityId = this.OriginAddress.DepartCityId = options.DepartCityId?options.DepartCityId:0,
            this.msg.ArriveCityId = this.msgAddress.ArriveCityId = this.OriginAddress.ArriveCityId = options.ArriveCityId?options.ArriveCityId:0,
            this.msg.TicketType = this.msgAddress.TicketType = options.TicketType>0?options.TicketType:2,
            
            this.U = uni.getStorageSync("mall_UserInfo")
            if(this.U&&this.U.Token){
                this.isToken = true
            }
        },
        created() {},
        mounted() {
            this.msg.StartDate = this.msgAddress.StartDate = getBeforeDate(-1, new Date()),
            // this.msg.EndDate = getBeforeDate(-183, new Date()),
            this.msgAddress.EndDate = getBeforeDate(-183, new Date()),
            this.getDataAddressList()
            this.getDateList(184)
        },
        methods: {
            getType(item){
                this.msg.TicketType = this.msgAddress.TicketType = item.Id
                this.getDataAddressList()
                this.getDataList()
            },
            AddressSelection(AddressType){
                // 1去程地址 2返程地址 0调换地址
                if(AddressType){
                    this.showAddressPreviwe = true,
                    this.AddressType = AddressType
                }else{
                    return
                    let obj = {
                        DepartCityName: this.OriginAddress.ArriveCityName,
                        DepartCityId: this.OriginAddress.ArriveCityId,
                        ArriveCityName: this.OriginAddress.DepartCityName,
                        ArriveCityId: this.OriginAddress.DepartCityId,
                    }
                    this.OriginAddress = JSON.parse(JSON.stringify(obj))
                    this.msg.DepartCityId = this.OriginAddress.DepartCityId
                    this.msg.ArriveCityId = this.OriginAddress.ArriveCityId
                    if(this.msg.DepartCityId||this.msg.ArriveCityId){
                        this.getDataList()
                    }
                }
            },
            RenewalAddres(SelectObj){
                this.OriginAddress.DepartCityName = SelectObj.DepartCityName,
                this.msg.DepartCityId = this.msgAddress.DepartCityId = this.OriginAddress.DepartCityId = SelectObj.DepartCityId
                this.OriginAddress.ArriveCityName = SelectObj.ArriveCityName,
                this.msg.ArriveCityId = this.msgAddress.ArriveCityId = this.OriginAddress.ArriveCityId = SelectObj.ArriveCityId
                this.showAddressPreviwe = false
                this.getDataList()
            },
             //滚动加载
			lower(e) {
				if (this.msg.pageIndex < this.pageCount) {
					this.msg.pageIndex++;
					this.status = "loading";
                    this.getDataList()
				} else {
					this.status = "nomore";
				}
			},
            getDataList(){
                uni.showLoading()
                this.apipost("AirTicket_get_GetTicketProduct",this.msg,
                (res) => {
                    if(res.resultCode==1){
                        // this.pageCount = res.data.pageCount
                        this.NewDateList = []
                        this.dataList = res.data
                        let arr = function(list){
                            list.forEach(x=>{
                                x.Price = 0
                                x.frequency = 0
                                x.frequencyA = 0
                                x.startDate = '',
                                x.remainNum = 0,
                                x.b2BPrice = 0,
                                x.b2CPrice = 0
                            })
                        }
                        arr(this.dataList)
                        this.dateList.forEach(x => {
                        this.dataList.forEach(y=>{
                            y.remainNum = y.B2BSellNum
                            y.startDate = y.FlightDate
                            y.b2BPrice = y.B2BPrice
                            y.b2CPrice = y.B2CPrice

                            y.Price = this.isToken?y.B2BPrice:y.B2CPrice
                            y.NewFlightList = []
                           if(y.FlightList.length>0&&y.FlightList.length<3){
                                y.FlightList.forEach((z,index)=>{
                                    let obj = {}
                                    obj = {
                                        duration:`${y.DepartureTime.Days>0?y.DepartureTime.Days+'d ':''}${y.DepartureTime.Hours>0?y.DepartureTime.Hours+'h ':''}${y.DepartureTime.Minutes>0?y.DepartureTime.Minutes+'m ':''}`,
                                        Days: 1,
                                        ...z
                                    }
                                    if(index){
                                        obj = {
                                            duration:`${y.ArrivalTime.Days>0?y.ArrivalTime.Days+'d ':''}${y.ArrivalTime.Hours>0?y.ArrivalTime.Hours+'h ':''}${y.ArrivalTime.Minutes>0?y.ArrivalTime.Minutes+'m ':''}`,
                                            Days: 1,
                                            ...z
                                        }
                                    }
                                    y.NewFlightList.push(obj)
                                })
                           }else if(y.FlightList.length>2){
                                y.frequency = 0
                                y.frequencyA = 0
                                let FlightDates = y.FlightList.map(z=>{ return z.FlightDate})
                                let NewFlightDates = []
                                let Days = []
                                if(FlightDates&&FlightDates.length>1){
                                    NewFlightDates = FlightDates.slice(0,FlightDates.length-1)
                                    Days = new Set(NewFlightDates)
                                }
                                y.FlightList.forEach((z,i)=>{
                                    if(z.FlightType!=3&&y.FlightList.length>2&&i){
                                        y.frequency+=1
                                    }if(z.FlightType==3){
                                        y.frequencyA+=1
                                    }
                                })
                                let obj = {
                                    duration:`${y.DepartureTime.Days>0?y.DepartureTime.Days+'d ':''}${y.DepartureTime.Hours>0?y.DepartureTime.Hours+'h ':''}${y.DepartureTime.Minutes>0?y.DepartureTime.Minutes+'m ':''}`,
                                    frequency: y.frequency-1,
                                    FlightType: y.FlightList[1].FlightType,
                                    Days: Array.from(Days).length>1?Array.from(Days).length:1,
                                    ...y.FlightList[0]
                                }
                                let obj2 = {
                                    duration:`${y.ArrivalTime.Days>0?y.ArrivalTime.Days+'d ':''}${y.ArrivalTime.Hours>0?y.ArrivalTime.Hours+'h ':''}${y.ArrivalTime.Minutes>0?y.ArrivalTime.Minutes+'m ':''}`,
                                    Days: 1,
                                    frequency: y.frequencyA,
                                    ...y.FlightList[y.FlightList.length-1]
                                }
                                y.NewFlightList.push(obj)
                                y.NewFlightList.push(obj2)
                            }
                                if(x.times==y.FlightDate){
                                    x.Price = this.isToken?y.B2BPrice:y.B2CPrice
                                    x.B2BSellNum = y.B2BSellNum
                                    x.show = true
                                    this.NewDateList.push(x)
                                }
                            })
                        });
                        this.NewDataList = JSON.parse(JSON.stringify(this.dataList))
                        this.StartDateObj = this.NewDateList.length>0&&this.NewDateList.length==1?this.NewDateList[0]:{}
                    }
                    uni.hideLoading()
                });
            },
            // 当前起止时间
            getDateList(day){
                for(let i=0;i<day;i++){
                    let dates = getBeforeDate(-i, new Date())
                    let date = dates.split('-')
                    let obj = {
                        times: dates,
                        time: date[1]+'-'+date[2],
                        math: date[1]>=10?date[1]:date[1][1],
                        date: date[2]>=10?date[2]:date[2][1],
                        week: getDateWeek(dates),
                        Price: 0,
                        B2BSellNum: 0,
                        show: false,
                    }
                    this.dateList.push(obj)
                }
                this.getDataList()
            },
            clickSearch(item,type){
                if(type==1){
                    uni.showLoading()
                    if(this.StartDateObj.times==item.times){
                        this.StartDateObj = {}
                        this.NewDataList = JSON.parse(JSON.stringify(this.dataList))
                        setTimeout(()=>{
                            uni.hideLoading()
                        },300)
                    }else{
                        this.StartDateObj = item
                        this.msg.StartDate = item.times
                        this.NewDataList = this.dataList.filter(x=> {
                            return x.FlightDate==item.times
                        })
                        setTimeout(()=>{
                            uni.hideLoading()
                        },300)
                    }
                    
                }else{
                    this.activeId = item
                }
            },
            chosenDateResult(obj) {
                let NewDateList =this.NewDateList.filter(x=> {
                    return x.times==obj.start
                })
                this.StartDateObj = NewDateList.length>0?NewDateList[0]:{}
                this.NewDataList = this.dataList.filter(x=> {
                    return x.FlightDate==obj.start
                })
                setTimeout(()=>{
                    uni.hideLoading()
                },300)
                this.showTimePreviwe = false
            },
            OffPopup(){
                
            },
            getDataAddressList(){
                this.apipost("AirTicket_get_GetTicketCity",this.msgAddress,
                (res) => {
                    if(res.resultCode==1){
                        this.AddressListAll = []
                        res.data.forEach(x=>{
                            let obj = {
                                Name: x.Name,
                                ID: x.ID,
                            }
                            this.AddressListAll.push(obj)
                            x.CityList.forEach(y=>{
                                let obj2 = {
                                    Name: y.Name,
                                    ID: y.ID,
                                }
                                this.AddressListAll.push(obj2)
                                if(this.msg.ArriveCityId==y.ID){
                                    this.msg.DepartCityId = x.ID
                                }
                            })
                        })
                        this.AddressList = res.data
                        if(this.msg.DepartCityId){
                            let DepartCityName = this.AddressListAll.filter(x=>{
                                return x.ID==this.msg.DepartCityId
                            })
                            this.OriginAddress.DepartCityName = DepartCityName.length>0?DepartCityName[0].Name:''
                        }
                        if(this.msg.ArriveCityId){
                            let ArriveCityName = this.AddressListAll.filter(x=>{
                                return x.ID==this.msg.ArriveCityId
                            })
                            this.OriginAddress.ArriveCityName = ArriveCityName.length>0?ArriveCityName[0].Name:''
                        }
                        
                    }
                });
            },
        }
    }
</script>
<style scoped>
@import url("@/asset/css/flex.css");
@import url("./style.css");
.StartStopAddress{
    padding: 61rpx 54rpx 0 54rpx;
}
.StartStopAddressText-box{
    width: 539rpx;
}
.StartStopAddressText{
    width: 209rpx;
}
.DepartureTime{
    padding: 34rpx 54rpx 27rpx 54rpx;
}
.DepartureTimeList{
    padding: 30rpx 0 0 0;
}
.DepartureTimeListC{
    width: 106rpx;
    height: 123rpx;
    border-radius: 20rpx;
    margin-right: 48rpx;
}
.DepartureTimeListC.active{
    background: linear-gradient(180deg, #DEBF7B, #FFFFFF);
}
.ScreeningCondition{
    margin-top: 7rpx;
    margin-bottom: 39rpx;
    padding: 18rpx 54rpx;
}
.ScreeningConditionC{
    width: 134rpx;
    height: 64rpx;
    line-height: 64rpx;
    text-align: center;
    border-radius: 20rpx;
    border: 1px solid #F5F5F5;
}
.ScreeningConditionC.active{
    border-color: #DEBF7B;
}

</style>