const app = getApp();
const Parse = getApp().Parse;
const company = getApp().globalData.company
const dateF = require('../../../../../utils/date')
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        navigationBarHeight,
        orders: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let orders = await this.getRoomOrders()
        console.log(orders);
        // let shopOrders = this.getShopOrders()
        // let roomOrders = this.getRoomOrders()
        // let orders = shopOrders.concat(roomOrders).sort((a,b)=> {return a.updatedAt - b.updatedAt})
        this.setData({
            orders
        })
    },
    async onChange(event) {
        let active = event.detail.name;
        let orders;
        if(active == 0){
            let shopOrders = await this.getShopOrders()
            let roomOrders = await this.getRoomOrders()
            orders = shopOrders.concat(roomOrders).sort((a,b)=> {return a.updatedAt - b.updatedAt})
        }
        if(active == 1){
            orders = await this.getShopOrders('meal')
        }
        if(active == 2){
            orders = await this.getShopOrders('shop')
        }
        if(active == 3){
            orders = await this.getRoomOrders()
            console.log(orders);
        }
        this.setData({
            active,
            orders
        })
        console.log(this.data.orders);
    },
    async getRoomOrders() {
        let userId = Parse.User.current().id;
        let Order1 = new Parse.Query('RoomOrder')
        let Order2 = new Parse.Query('RoomOrder')
        let Order3 = new Parse.Query('RoomOrder')
        let Order4 = new Parse.Query('RoomOrder')
        Order1.equalTo("status", 500)
        Order2.equalTo("status", 601)
        Order3.equalTo("status", 602)
        Order4.equalTo("status", 700)
        console.log(Order1,Order2,Order3,Order4);
        let mainQuery = Parse.Query.or(Order1,Order2,Order3,Order4);
        console.log(mainQuery);
        mainQuery.equalTo('company', company)
        mainQuery.equalTo('user', userId)
        mainQuery.include('room')
        mainQuery.include('shopStore')
        console.log(mainQuery);
        let res = await mainQuery.find()
        console.log(res);
        let list = []
        if(res&&res.length){
            res.forEach(item => {
                let activitys = item.toJSON()
                activitys.orderTime = dateF.formatTime("mm-dd", activitys.startTime.iso)
                activitys.Time = dateF.formatTime("mm-dd", activitys.endTime.iso)
                activitys.startTime = dateF.formatTime("YYYY/mm/dd", activitys.startTime.iso)
                activitys.endTime = dateF.formatTime("YYYY/mm/dd", activitys.endTime.iso)
                    //计算天数
                activitys.day = parseInt((new Date(activitys.endTime.replace(/-/g, '/')).getTime() - new Date(activitys.startTime.replace(/-/g, '/')).getTime()) / (1000 * 60 * 60 * 24));
                list.push(activitys)
            })
        }
        console.log(list);
        return list
            
    },
    async getShopOrders() {
        let userId = Parse.User.current().id;
        let Order1 = new Parse.Query('ShopOrder')
        let Order2 = new Parse.Query('ShopOrder')
        let Order3 = new Parse.Query('ShopOrder')
        let Order4 = new Parse.Query('ShopOrder')
        Order1.equalTo("status", 500)
        Order2.equalTo("status", 601)
        Order3.equalTo("status", 602)
        Order4.equalTo("status", 700)
        console.log(Order1,Order2,Order3,Order4);
        let mainQuery = Parse.Query.or(Order1,Order2,Order3,Order4);
        console.log(mainQuery);
        mainQuery.equalTo('company', company)
        mainQuery.equalTo('user', userId)
        mainQuery.include('shopStore')
        console.log(mainQuery);
        let res = await mainQuery.find()
        let list = []
        res.forEach(item => {
            let activitys = item.toJSON()
            activitys.orderTime = dateF.formatTime("mm-dd", activitys.startTime.iso)
            activitys.Time = dateF.formatTime("mm-dd", activitys.endTime.iso)
            activitys.a = dateF.formatTime("YYYY-mm-dd HH:MM:SS", activitys.startTime.iso)
            activitys.e = dateF.formatTime("YYYY-mm-dd HH:MM:SS", activitys.endTime.iso)
                //计算天数
            activitys.day = parseInt((new Date(activitys.e.replace(/-/g, '/')).getTime() - new Date(activitys.a.replace(/-/g, '/')).getTime()) / (1000 * 60 * 60 * 24));
            list.push(activitys)
        })
         return list;  
    },
    phone() {

        let phone = this.data.shopOrder.shopStore.mobile
        console.log(phone);
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})