const Parse = getApp().Parse;
const company = getApp().globalData.company
const dateF = require('../../../../../utils/date')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        orderTime: null,
        Time: null,
        day: null,
        order: null,
        time: null,
        show: false,
        qrcode: null,
        latitude: null,
        longitude: null,
        activeColor:""

    },
    async getQRCode(id) {
        let qrCodeUrl = `https://pwa.fmode.cn//gomini/rid=${id}`
        let that = this
        await wx.request({
            url: "https://server.fmode.cn/api/common/qrcode",
            data: {
                qrCode: qrCodeUrl,
                darkColor: "#000",
                lightColor: "#ffffff"
            },
             success(res) {
                console.log(res);
                if (res.data.code == 1) {
                    //把base64文件转图片
                    let fileSystemManager = wx.getFileSystemManager()
                    fileSystemManager.writeFile({
                        filePath: wx.env.USER_DATA_PATH + '/qr_code.png',
                        data: res.data.data.slice(22),
                        encoding: 'base64',
                        success: res => {
                            that.setData({
                                qrcode: wx.env.USER_DATA_PATH + '/qr_code.png'
                            })
                            console.log(that.data.qrcode)
                        },
                        fail(e) {
                            console.log('文件保存失败', e)
                        }
                    })
                } else {
                    wx.showToast({
                        title: '请先删除小程序重新授权后重试',
                        icon: 'none'
                    })
                }
            },
        });
    },
    shopadmin() {
        let id = this.data.id
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/shop-admin/index?id=' + id
        });
    },
    storepackage() {

        this.setData({
            show: true,

        })
    },
    async acceptResult(e) {
        let {
            order,
        } = this.data
        let that = this
        let {
            params,
            no
        } = e.detail;
        that.setData({
            show: false
        })
        try {
            if (params == "ok") {
                order.set("isPay", true)
                await order.save()
                this.setData({
                    isPay: true
                })
                wx.showToast({
                    title: '支付成功',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
            } else {
                wx.showToast({
                    title: '支付失败，取消订单',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
            }
        } catch (error) {
            console.log(error)
            wx.showToast({
                title: "支付失败",
                icon: "error",
                duration: 1500,
            });
            wx.hideLoading()
        }
    },

    taxi() {
        let latitude = this.data.shopOrder.shopStore.location.latitude;
        let longitude = this.data.shopOrder.shopStore.location.longitude;
        console.log(latitude, longitude);
        wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success() {

                wx.openLocation({
                    latitude,
                    longitude,
                    scale: 18
                })
            }
        })
    },
    phone() {

        let phone = this.data.order.shopStore.mobile
        console.log(phone);
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    async getShopOrder() {
        let id = this.data.id
        console.log(id);
        let ShopOrder = new Parse.Query('RoomOrder')
        ShopOrder.include('room')
        let shopOrder = await ShopOrder.get(id)
        let orderTime = dateF.formatTime("mm-dd", shopOrder.toJSON().startTime.iso)
        let time = dateF.formatTime("mm-dd", shopOrder.toJSON().createdAt)
        let Time = dateF.formatTime("mm-dd", shopOrder.toJSON().endTime.iso)
        let a = dateF.formatTime("YYYY-mm-dd HH:MM:SS", shopOrder.toJSON().startTime.iso)
        let e = dateF.formatTime("YYYY-mm-dd HH:MM:SS", shopOrder.toJSON().endTime.iso)
            //计算天数
        let day = parseInt((new Date(e.replace(/-/g, '/')).getTime() - new Date(a.replace(/-/g, '/')).getTime()) / (1000 * 60 * 60 * 24))
        console.log(day, Time, orderTime);
        this.setData({
            order: shopOrder.toJSON(),
            day: day,
            orderTime: orderTime,
            Time: Time,
            time: time

        })
        console.log(this.data.order);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let id = options.id
        this.setData({ id: id })
        this.getShopOrder()
        this.getQRCode(id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})