<template>
    <view class="content">
        <!-- tabs -->
        <u-sticky>
            <u-tabs class="tabs" :list="tabs" :scrollable="false" :current="index" lineColor="#34CE98" @click="onTabs"
                sticky />
        </u-sticky>
        <block v-if="array.length >= 1">
            <!-- 订单列表 -->
            <orderPin v-for="(item, arrayIndex) in array" :key="arrayIndex" :order-info="item" @onBtn="onType" />
            <!-- 加载更多 -->
            <view class="pages-load">
                <u-loadmore :status="status" />
            </view>
        </block>
        <block v-else>
            <view class="vertical order-null">
                <u-empty mode="order" icon="http://cdn.uviewui.com/uview/empty/order.png" text="暂无相关订单"
                    textColor="#999" />
            </view>
        </block>

        <!-- 分享 -->
        <uni-popup ref="popupShare" type="share" background-color="#fff">
            <uni-popup-share title="立即分享到" @select="select" />
        </uni-popup>
    </view>
</template>

<script>
    import {
        orders,
        del,
        cancel,
        sign
    } from '@/apis/interfaces/order'
    import {
        myPinList
    } from '@/apis/interfaces/pin'
    import eventBus from '../../../utils/eventBus.js';
    import orderPin from '@/components/oct-order-pin/index.vue'
    export default {
        components: {
            orderPin
        },
        data() {
            return {
                status: "loading",
                tabs: [{
                        name: "全部",
                        type: "",
                    },
                    {
                        name: "拼团中",
                        type: "3",
                    },
                    {
                        name: "拼团成功",
                        type: "1",
                    },
                    {
                        name: "拼团失败",
                        type: "2",
                    }
                ],
                index: '0',
                array: [],
                page: 1,
                shareInfo: {},
            };
        },
        onLoad() {
            this.getOrder()
        },
        onShow() {
            if (this.$store.getters.getRefresh == 1) {
                this.$store.commit('setRefresh', 0)
                this.array = []
                this.page = 1
                this.getOrder()
            }
        },
        methods: {
            getOrder() {
                myPinList({
                    state: this.tabs[this.index].type,
                    page: this.page
                }).then(res => {
                    console.log(res)
                    if (res.page.current === 1) {
                        this.array = []
                    }
                    let ordersArr = res.data.map(val => {
                        return {
                            no: val.order_no,
                            cover: val.item.cover,
                            name: val.item.name,
                            price: val.item.price,
                            sum: 1,
                            collage: val.collage,
                            shop: val.shop,
                            goods_id: val.item.goods_id,
                            unit: val.item.unit,
                            users: val.users,
                            invite: val.invite,
                            order: val.order,
                            url: val.url
                        }
                    })
                    this.array = this.array.concat(ordersArr)
                    this.status = res.page.has_more ? 'loadmore' : 'nomore'
                })
            },
            onTabs(e) {
                this.page = 1
                this.index = e.index
                this.getOrder()
            },

            onType(e) {
                let orderNo = e.order.no;
                let shopId = e.order.shop.shop_id;
                let goodsId = e.order.goods_id;
                switch (e.type) {
                    case 'share':
                        this.shareInfo = e.order
                        this.$refs.popupShare.open();
                        console.log('触发分享', this.shareInfo)
                        break;
                    case 'goInfo':
                        uni.navigateTo({
                            url:'/pages/group-book/detail/detail?id='+e.order.no
                        })
                        break;
                }
            },
            // 分享选项
            select(e) {
                console.log(this.shareInfo.url + this.shareInfo.collage.collage_id + 'invitation' + this
                    .shareInfo.invite, this.shareInfo.collage.surplus, this.shareInfo.cover, this.shareInfo.name)
                switch (e.item.name) {
                    case 'wxchum':
                        uni.share({
                            provider: 'weixin',
                            scene: 'WXSceneSession',
                            type: 0,
                            href: this.shareInfo.url + this.shareInfo.collage.collage_id + 'invitation' + this
                                .shareInfo.invite,
                            title: '【仅剩' + this.shareInfo.collage.surplus + '个名额】邀请您一起拼团',
                            summary: '好货不用一分钱，点点就能带回家【 ' + this.shareInfo.name + '】',
                            imageUrl: this.shareInfo.cover,
                            fail: (err) => {
                                uni.showToast({
                                    title: err.errMsg,
                                    icon: 'none'
                                })
                            },
                            success: (res) => {
                                this.shareInfo = {}
                            }
                        })
                        break;
                    case 'wxcircle':
                        uni.share({
                            provider: 'weixin',
                            scene: 'WXSceneTimeline',
                            type: 0,
                            href: this.shareInfo.url + this.shareInfo.collage.collage_id + 'invitation' + this
                                .shareInfo.invite,
                            summary: '好货不用一分钱，点点就能带回家【 ' + this.shareInfo.name + '】',
                            imageUrl: this.shareInfo.cover,
                            fail: (err) => {
                                uni.showToast({
                                    title: err.errMsg,
                                    icon: 'none'
                                })
                            },
                            success: (res) => {
                                this.shareInfo = {}
                            }
                        })
                        break;
                }
            },
        },
        onReachBottom() {
            if (this.status === 'loadmore') {
                this.page += 1
                this.status = 'loading'
                this.getOrder()
            }
        }
    }
</script>

<style lang="scss">
    .content {
        background: $window-color;
        min-height: 100vh;
    }

    .tabs {
        background: white;
    }

    // 数据列表空
    .order-null {
        height: 80vh;
    }

    // 加载分页
    .pages-load {
        padding-bottom: $padding;
    }
</style>
