// pages/groupmeallist/myOrder/myOrder.js
import order from '../../../api/order'
const cache = require('../../../utils/cache.js');
import Activity from '../../../api/activity';
const app = getApp()
import storeGoods from '../../../api/storeGoods'
import base from "../../../utils/base";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showAllSku: false,
        orderInfo: {},
        orderSn: '',
        orderGoodList: [], //spu-sku商品信息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.sn) {
            this.reloadPage(options.sn)
        }
    },

    // // 时间戳转化
    // getLocalTime(nS) {
    //     return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
    // },

    dateFormat(timestamp, format) {
        if (String(timestamp).length === 10) {
            timestamp = timestamp * 1000
        }
        var date = new Date(timestamp)
        var Y = date.getFullYear()
        var M = date.getMonth() + 1
        var D = date.getDate()
        var hour = date.getHours()
        var min = date.getMinutes()
        var sec = date.getSeconds()
        if (format === 'YYYY') {
            return Y // 2021
        } else if (format === 'YYYY-MM') { // 2021-07
            return Y + '-' + (M < 10 ? '0' + M : M)
        } else if (format === 'YYYY-MM-DD') { // 2021-07-12
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D)
        } else if (format === 'HH:mm:ss') { // 10:20:35
            return (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else if (format === 'YYYY-MM-DD HH:mm') { // 2021-07-12 10:20
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min)
        } else if (format === 'YYYY-MM-DD HH:mm:ss') { // 2021-07-12 10:20:35
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else {
            return '--'
        }
    },


    reloadPage(orderSn) {
        this.setData({
            orderSn
        })

        this.getOrderdetail();
    },
    async getOrderdetail() {
        let _self = this;
        try {
            let orderRes = await order.getOrderDetail({
                orderSn: _self.data.orderSn,
                mobile: cache.getMobile()
            });
            if (orderRes.code == 200 && orderRes.msg == "success") {
                let orderInfo = orderRes.data;
                orderInfo.originPrice = (parseFloat(orderInfo.payPrice) / 100).toFixed(2)
                orderInfo.orderRefund.createTime = _self.dateFormat(orderInfo.orderRefund.createTime, 'YYYY-MM-DD HH:mm:ss')
                _self.setData({
                    orderInfo: orderInfo,
                })
                //组建spu sku
                _self.createOrderGoodList(orderInfo.orderGoodsList)
            }
        } catch (error) {
            console.log(error);
        }
    },

    /**
* 组建spu sku
* @param {*} list 
*/
    createOrderGoodList(list) {

        console.log("进入详情")
        console.log(list)
        let _self = this;
        if (list.length == 0) {
            return;
        }
        let orderGoodList = [];
        for (let a = 0; a < list.length; a++) { //最外层
            //某一个商品种类
            let goodList = list[a];
            //sku-list
            let orderGoodsSkuList = goodList.orderGoodsSkuList;
            for (let b = 0; b < orderGoodsSkuList.length; b++) {
                let orderGood = {};
                orderGood.productName = goodList.productName;
                let orderGoodsSku = orderGoodsSkuList[b];
                let text = '';
                if (orderGoodsSku.comboSkuList != null && orderGoodsSku.comboSkuList.length > 0) {
                    console.log("进入套餐拼接")
                    text = "套餐:"
                    let productName = ''
                    let specValue = []
                    let num = ''
                    orderGoodsSku.comboSkuList.forEach(function (comboSkuList, index) {
                        comboSkuList.skuList.forEach(function (skuList, skuindex) {
                            productName = skuList.productName
                            num = skuList.num
                            if (skuList.specList != null) {
                                let specList = JSON.parse(skuList.specList);
                                if (base.isArray(specList)) {
                                    specList.forEach(function (specList, skuindex) {

                                        specValue.push(specList.specValue + " X" + num)

                                    })
                                }
                            } else {
                                specValue.push(productName + " X" + num)
                            }
                        })
                    }
                    )
                    specValue = specValue.join('、') || ""
                    text = text + "(" + specValue + ")"

                }
                orderGood.num = orderGoodsSkuList[b].num;
                orderGood.allFee = base.fenToYuan(orderGoodsSkuList[b].price * orderGoodsSkuList[b].num)
                orderGood.skuText = text
                orderGoodList.push(orderGood)
            }
        }
        _self.setData({
            orderGoodList: orderGoodList
        })

        console.log(this.data.orderGoodList)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})