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

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        id: "",
        shopOrder: null,
        orderTime: null,
        qrcode: null,
        latitude: null,
        longitude: null,
    },
    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"
            },
            async success(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'
                    })
                }
            },
        });
    },
    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(res) {
                wx.openLocation({
                    latitude,
                    longitude,
                    scale: 18
                })
            }
        })
    },
    phone() {

        let phone = this.data.shopOrder.shopStore.mobile
        console.log(phone);
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    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()
        }
    },

    async getShopOrder() {
        let id = this.data.id
        console.log(id);
        let ShopOrder = new Parse.Query('ShopOrder')
        ShopOrder.include('shopStore')
        ShopOrder.include('goods')
        let shopOrder = await ShopOrder.get(id)
        let orderTime = dateF.formatTime("YYYY-mm-dd HH:MM:SS", shopOrder.createdAt)
        console.log(orderTime);
        shopOrder = shopOrder.toJSON()
        shopOrder.goods.details = rechText.formatRichText(shopOrder.goods.details)
        this.setData({
            shopOrder: shopOrder,
            orderTime: orderTime
        })
        console.log(this.data.shopOrder);
    },
    async acceptResult(e) {
        let { activeOrder } = this.data
        let that = this
        let {
            params,
            no
        } = e.detail;
        that.setData({
            show: false
        })
        try {
            if (params == "ok") {
                activeOrder.set("status", 200)
                activeOrder.set("isPay", true)
                await activeOrder.save()
                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()
        }
    },

    shopadmin() {
        let id = this.data.id
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/shop-admin/index?id=' + id
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let id = options.id
        this.getQRCode(id)
        this.setData({
            id: id
        })
        this.getShopOrder()

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})