<template>
    <app-layout minHeight="unset" btnType="back" title="服务列表" headerBackgroundColor="#FFFFFF" backgroundColor="#FFFFFF">
        <view class="cate">
            <app-cate
                :offsetHeight="0"
                :data="data"
                cateType="list"
                @clickItem="clickItem"
                @changeNumber="changeNumber"
                :activeId="currentId"
            />
        </view>
        <view class="common-bottom-components" :style="{ paddingBottom: pageConfig.safeAreaInsets.bottom + 'px' }">
            <view class="cart" @click="utils.toPage('/pages/service/cart')">
                <view class="cart-icon">
                    <text class="iconfont icon-gouwuche"></text>
                </view>
                <view class="cart-number">
                    <text class="text">{{ briefCart.count }}</text>
                </view>
            </view>
            <view class="order-btn" :class="{ active: briefCart.count > 0 }" @click="toCartPage">
                <text class="text">去下单</text>
            </view>
        </view>
    </app-layout>
</template>

<script>
import AppLayout from "@/components/layout/layout";
import AppCate from "@/components/cate/cate";
import { mapGetters } from "vuex";
export default {
    name: "service-list",
    data() {
        return {
            utils: this.$utils,
            pageConfig: {},
            data: [],
            currentId: 0,
        };
    },
    components: {
        AppLayout,
        AppCate,
    },
    computed: {
        ...mapGetters({
            isLogin: "user/isLogin",
            briefCart: "cart/briefCart",
        }),
    },
    onLoad(e) {
        this.pageConfig = getApp().globalData.pageConfig;
        if (!e.id) {
            return this.$utils.toast("参数错误");
        }
        this.currentId = e.id;
        // 初始化列表
        this.data = [];
        this.loadList(this.currentId);
    },
    onShow() {
        if (this.isLogin) {
            this.$store.dispatch("cart/update");
        }
    },
    onReady() {},
    onReachBottom() {},
    onPullDownRefresh() {},
    onShareTimeline() {},
    onShareAppMessage() {},
    methods: {
        /**
         * 加载列表
         */
        loadList(cateId) {
            this.$request({
                api: "service.list",
                query: "id=" + cateId,
            })
                .then((response) => {
                    response.data.forEach((item) => {
                        let goods = [];
                        item.goods.forEach((v) => {
                            goods.push({
                                id: v.id,
                                name: v.post_title,
                                times: v.post_hits,
                                cover: v.thumbnail,
                                price: v.money,
                            });
                        });
                        this.data.push({
                            id: item.id,
                            name: item.name,
                            cover: item.more,
                            child: goods,
                        });
                    });
                })
                .catch((e) => {});
        },
        /**
         * 详情
         */
        clickItem(parentId, id) {
            this.$utils.toPage("/pages/service/detail?id=" + id);
        },
        /**
         * 数量改变
         */
        changeNumber(e, id) {
            if (e.type == "nologin") {
                this.$store.commit("user/showLoginModal", true);
                return;
            }
            if (e.value > 0) {
                if (e.type == "add" && e.value == 1) {
                    this.$models.cart.toCart(id).then(() => {
                        this.$store.dispatch("cart/update", true);
                    });
                } else {
                    this.$models.cart.change(id, e.type).then(() => {
                        this.$store.dispatch("cart/update", true);
                    });
                }
            } else {
                this.$models.cart.delete(id).then(() => {
                    this.$store.dispatch("cart/update", true);
                });
            }
        },
        /**
         * 去下单
         */
        toCartPage() {
            if (this.briefCart.count > 0) {
                this.$utils.toPage("/pages/service/cart");
            }
        },
    },
};
</script>

<style lang="less" scoped>
.common-bottom-components {
    padding: 0 20rpx 0 50rpx;
    .cart {
        position: relative;
        width: 95rpx;
        height: 95rpx;
    }
    .cart-icon {
        position: absolute;
        top: -18rpx;
        left: 0;
        width: 95rpx;
        height: 95rpx;
        text-align: center;
        background-color: #ffffff;
        border: 1px solid #4b65ed;
        border-radius: 50%;
        .iconfont {
            color: #4b65ed;
            font-size: 50rpx;
            line-height: 95rpx;
        }
    }
    .cart-number {
        position: absolute;
        top: -24rpx;
        right: -16rpx;
        width: 40rpx;
        height: 40rpx;
        background-color: #ec7655;
        border-radius: 50%;
        text-align: center;
        font-size: 0;
        .text {
            font-size: 24rpx;
            line-height: 40rpx;
            color: #ffffff;
        }
    }
    .order-btn {
        width: 300rpx;
        height: 75rpx;
        text-align: center;
        border-radius: 38rpx;
        background: #e0e0e0;
        transition: all 0.35s;
        .text {
            font-size: 30rpx;
            color: #ffffff;
            line-height: 75rpx;
        }
    }
    .order-btn.active {
        background: linear-gradient(-90deg, #8194f2, #4c60c1);
    }
}
</style>