<template>
    <div class="page shop-cart-page" :class="{ page_red: theme === 1 }">
        <div class="page__top">
            <div class="search">
                <i class="iconfont icontubiao- search__icon"></i>
                <input
                    class="search__text"
                    v-model="searchVal"
                    @confirm="search()"
                    type="text"
                    placeholder="搜索你想要的商品"
                />
                <i
                    class="iconfont iconclose_icon search__icon remove-icon"
                    @click="closeSearchVal"
                ></i>
            </div>
        </div>
        <div class="page__content">
            <div class="page__box">
                <scroll-view class="category" :scroll-y="true">
                    <ul class="category__list">
                        <li
                            class="category__item"
                            :class="{
                                'category__item--active': cateId === 'all',
                            }"
                            @click="setCategory('all')"
                        >
                            全部
                        </li>
                        <li
                            class="category__item"
                            :class="{
                                'category__item--active':
                                    cateId === item.categoryId,
                            }"
                            v-for="(item, index) in classifyList"
                            :key="index"
                            @click="setCategory(item)"
                        >
                            {{ item.name }}
                        </li>
                    </ul>
                </scroll-view>
                <div class="page__right">
                    <!-- <div class="status-tab">
                        <span
                            :class="{ on: display === 1 }"
                            @click="steDisplay(1)"
                            >展示中</span
                        >
                        <span
                            :class="{ on: display === 0 }"
                            @click="steDisplay(0)"
                            >已隐藏</span
                        >
                    </div> -->

                    <div class="goods">
                        <scroll-view
                            class="goods__scroll"
                            :scroll-y="true"
                            @scrolltolower="getMoreActivityList"
                        >
                            <ul class="goods__list">
                                <li
                                    class="goods__item"
                                    v-for="(item, index) in activityList"
                                    :key="index"
                                >
                                    <div class="main" @click="goDetail(item)">
                                        <my-image
                                            class="goods__img"
                                            :src="item.brandLogo"
                                        ></my-image>
                                        <div class="goods__info">
                                            <div class="goods__box">
                                                <div class="goods__title">
                                                    {{ item.brandName }}
                                                </div>
                                                <div class="goods__sale black">
                                                    {{
                                                        item.endTime | viewDate
                                                    }}
                                                </div>
                                                <div class="goods__sale flex">
                                                    <span>
                                                        {{
                                                            item.onlineProductCount
                                                        }}款
                                                    </span>

                                                    <span>
                                                        最多赚：{{
                                                            item.maxCommission /
                                                            100
                                                        }}
                                                    </span>
                                                </div>
                                            </div>
                                            <!-- <div
                                            class="goods__price"
                                            @click="edit(item)"
                                        >
                                            <div class="goods__present">
                                                价格：{{ "¥" + item.price / 100 }}
                                            </div>
                                            <div
                                                class="goods__original"
                                                v-if="item.stock !== 0"
                                            >
                                                库存：{{ "¥" + item.stock }}
                                            </div>
                                        </div> -->
                                            <!-- 登录 -->
                                        </div>
                                    </div>

                                    <div class="btns">
                                        <div
                                            class="btn"
                                            @click="setActivityTop(item)"
                                        >
                                            {{
                                                item.top === 0
                                                    ? "置顶"
                                                    : "取消置顶"
                                            }}
                                        </div>
                                        <div
                                            class="btn"
                                            @click="setActivityDisplay(item)"
                                        >
                                            {{
                                                item.display === 0
                                                    ? "上线"
                                                    : "下线"
                                            }}
                                        </div>
                                        <div
                                            class="btn"
                                            @click="pushGive(item)"
                                        >
                                            编辑
                                        </div>
                                        <div class="btn" @click="item(item)">
                                            删除
                                        </div>
                                    </div>
                                </li>
                            </ul>
                            <div
                                class="more-text"
                                v-if="activityList.length && !isMoreEnd"
                            >
                                - 没有更多品牌了 -
                            </div>
                            <div
                                class="null"
                                v-if="loading && !activityList.length"
                            >
                                <img
                                    class="null__img"
                                    src="/static/common/goods_null.jpg"
                                    alt
                                />
                                <div class="null__title">
                                    抱歉！没有找到相关品牌~
                                </div>
                            </div>
                        </scroll-view>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import MyImage from "../../components/image/index";
import plugin from "../../common/js/plugin";
export default {
    name: "",
    components: { MyImage },
    // 数据
    data() {
        return {
            cateId: "all",
            shopId: "",
            display: 1,
            searchVal: "",
            classifyList: [],
            activityPageIndex: 1,
            activityList: [],
            isMoreEnd: false,
            otherUrl: "https://bot.ixiaokejia.com/",
        };
    },
    filters: {
        viewDate(dateStr) {
            if (!dateStr) {
                return "";
            }
            if (isNaN(Date.parse(dateStr))) {
                return dateStr;
            }
            dateStr = dateStr.replace(/-/g, "/");
            let dateNow = Date.parse(dateStr);

            let mius = dateNow - Date.now();

            let minute = 1000 * 60;
            let hour = minute * 60;
            let day = hour * 24;
            let month = day * 30;
            let quarter = month * 3;
            let year = quarter * 12;
            if (mius < 0) {
                return `结束时间：${plugin.dateFormat(dateStr, "MM-dd hh:mm")}`;
            } else if (mius < minute) {
                return `距结束还有${Math.floor(mius / 1000)}秒`;
            } else if (mius < hour) {
                return `距结束还有${Math.floor(mius / minute)}分钟`;
            } else if (mius < day) {
                return `距结束还有${Math.floor(mius / hour)}小时`;
            } else if (mius < month) {
                return `距结束还有${Math.floor(mius / day)}天`;
            } else {
                return `结束时间：${plugin.dateFormat(dateStr, "MM-dd hh:mm")}`;
            }
        },
    },

    onLoad(opts) {
        this.cateId = opts.cateId;
    },

    async onShow() {
        this.getClassify();
        this.getActivityList();
    },

    onPullDownRefresh() {
        this.getClassify();
        this.getActivityList();
    },

    // 函数
    methods: {
        // 获取店铺信息
        getShopInfo() {
            this.api
                .get("/Shop/GetInfo", {}, { pass: true })
                .then((infoRes) => {
                    this.shopId = infoRes.data.id;
                });
        },

        // 改变活动显示隐藏tab
        steDisplay(val) {
            this.display = val;
            this.getActivityList();
        },

        // 获取商品分类
        getClassify() {
            this.api.get("/Supplier/GetCateList", {}).then((res) => {
                this.classifyList = res.data;
            });
        },

        // 设置商品分类
        setCategory(val) {
            this.cateId = val.categoryId;
            this.activityPageIndex = 1;
            this.getActivityList();
        },

        // 获取活动列表
        getActivityList() {
            this.isMoreEnd = false;
            this.activityPageIndex = 1;
            let sendData = {
                // cateId: this.cateId,
                pageIndex: this.activityPageIndex++,
                // display: this.display,
            };
            if (this.cateId !== "all") {
                sendData.cateId = this.cateId;
            }
            if (this.searchVal) {
                sendData.keyword = this.searchVal;
            }
            uni.showLoading({
                title: "加载中...",
            });
            this.api.get("/Supplier/GetActivityList", sendData).then((res) => {
                uni.stopPullDownRefresh();
                uni.hideLoading();
                this.activityList = res.data.data;
                if (res.data.length) {
                } else {
                    this.isMoreEnd = true;
                }
            });
        },

        // 获取活动列表
        getMoreActivityList() {
            if (this.isMoreEnd) {
                return;
            }
            let sendData = {
                cateId: this.cateId,
                pageIndex: this.activityPageIndex++,
                display: this.display,
            };
            if (this.searchVal) {
                sendData.keyword = this.searchVal;
            }
            this.api.get("/Supplier/GetActivityList", sendData).then((res) => {
                if (res.data.length) {
                    this.activityList = this.activityList.concat(res.data.data);
                } else {
                    this.isMoreEnd = true;
                }
            });
        },

        //  设置活动置顶
        setActivityTop(val) {
            if (this.user.roleId !== "0") {
                return this.fn.showToast("需管理员账号才能执行此操作");
            }
            this.api
                .get(
                    "/Supplier/SetActivityTop",
                    {
                        aid: val.activityNo,
                        untop: val.top > 0 ? true : false,
                    },
                    { pass: true }
                )
                .then((res) => {
                    if (res.success) {
                        if (val.top > 0) {
                            val.top = 0;
                        } else {
                            val.top = 999;
                        }
                        this.$forceUpdate();
                        this.fn.showToast("设置成功");
                    } else {
                        this.fn.showModal({
                            title: "设置失败",
                            content: res.message,
                            showCancel: false,
                        });
                    }
                });
        },

        // 设置活动显示或隐藏
        setActivityDisplay(val) {
            let offline = true;
            if (val.display === 1) {
            } else {
                offline = false;
            }
            this.api
                .get(
                    "/Supplier/SetActivityOffline",
                    {
                        id: val._id,
                        offline,
                    },
                    { pass: true }
                )
                .then((res) => {
                    if (res.success) {
                        val.display === 0
                            ? (val.display = 0)
                            : (val.display = 1);
                        this.$forceUpdate();
                        this.fn.showToast("设置成功");
                    } else {
                        this.fn.showModal({
                            title: "设置失败",
                            content: res.message,
                            showCancel: false,
                        });
                    }
                });
        },

        // 删除
        remove(val) {
            let sendData = {
                id: val._id,
            };
            this.api
                .get("/Supplier/RemoveActivity", sendData, {
                    pass: true,
                })
                .then((res) => {
                    if (res.success) {
                        let arr = [];
                        for (let item of this.activityList) {
                            if (item._id !== val._id) {
                                arr.push(item);
                            }
                        }
                        this.activityList = [...this.activityList];
                        this.fn.showToast("删除成功");
                    } else {
                        this.fn.showModal({
                            title: "删除失败",
                            content: res.message,
                            showCancel: false,
                        });
                    }
                });
        },

        // 搜索
        search() {
            if (this.searchVal) {
                this.getActivityList();
            }
        },

        // 取消搜索
        closeSearchVal() {
            this.searchVal = "";
            this.getActivityList();
        },

        // 去详情
        goDetail(val) {
            this.$store.commit("common/update", {
                activityData: val,
            });
            this.router.push({
                path: "/pages/yx/good",
                query: {
                    aid: val.aid,
                },
            });
        },
    },

    // 数据计算
    computed: {
        // 判断主题  1:红色  2：绿色
        theme() {
            return this.$store.state.common.theme;
        },
        user() {
            return this.$store.state.user.user;
        },
    },

    // 数据监听
    watch: {},
};
</script>

<style>
page {
    height: 100%;
    background-color: #fff;
}
</style>

<style lang="scss" scoped>
.page {
    display: flex;
    flex-direction: column;
    height: 100%;

    &__top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: px(145);
        padding: 0 px(50);
        background-color: #fff;
        // box-shadow: 0 px(-5) px(10) #333;
    }

    &__content {
        position: relative;
        flex: 1;
        margin-top: px(5);
    }

    &__box {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1;
        display: flex;
        justify-content: space-between;
    }

    &__right {
        display: flex;
        flex-direction: column;
        flex: 1;
        position: relative;
    }
}

.search {
    display: flex;
    align-items: center;
    width: 100%;
    height: px(86);
    padding: 0 px(35);
    background-color: #f8f8f8;
    border-radius: px(43);

    &__icon {
        margin-top: px(10);
        font-size: px(60);
        color: #999;
    }

    &__text {
        margin-left: px(15);
        font-size: px(36);
        color: #999;
        width: 100%;
    }
}

.service {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    &__icon {
        font-size: px(50);
        color: #666;
    }

    &__text {
        font-size: px(30);
        color: #666;
    }
}

.category {
    width: px(225);
    height: 100%;
    background-color: #f8f6f7;

    &__item {
        position: relative;
        display: flex;
        justify-content: center;
        font-size: px(40);
        color: #888;
        background-color: #f8f6f7;
        padding: px(60) 0;

        &--active {
            // font-weight: 700;
            color: #333;
            background-color: #fff;
            &:before {
                content: "";
                position: absolute;
                top: 50%;
                left: 0;
                z-index: 1;
                display: block;
                width: px(8);
                height: 100%;
                transform: translateY(-50%);
                border-radius: px(4);
                background-color: #27a34f;
            }
        }
    }
}

.category-sub {
    position: relative;
    height: px(130);
    background-color: #fff;
    margin-right: px(140);
    margin-left: px(20);
    &__scroll {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1;
    }

    &__list {
        display: flex;
        align-items: center;
        height: 100%;
    }

    &__toggle {
        position: absolute;
        top: 0;
        right: px(-140);
        z-index: 2;
        width: px(140);
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: px(30);
        transform: rotate(90deg);
        &.active {
            transform: rotate(-90deg);
        }
    }

    &__item {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        flex: none;
        height: 100%;
        font-size: px(34);
        width: px(220);
        color: #333;
        height: px(70);
        border-radius: px(35);
        background-color: #f6f6f6;
        border: 1px solid #f6f6f6;
        color: #888;
        margin: 0 px(10);
        &--active {
            color: #262626;
            background-color: rgba(39, 163, 79, 0.2);
            border: 1px solid #27a34f;
            color: #27a34f;
        }
    }
}

.category-sub-all {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    z-index: 10;
    margin-top: px(130);
    &__bg {
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.3);
    }
    &__scroll {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 10;
    }
    &__list {
        display: flex;
        flex-wrap: wrap;
        background-color: #fff;
        padding: px(60) px(40);
    }
    &__item {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: none;
        height: 100%;
        font-size: px(34);
        width: px(220);
        color: #333;
        height: px(70);
        border-radius: px(35);
        background-color: #f6f6f6;
        border: 1px solid #f6f6f6;
        color: #888;
        margin: 0 px(20) px(30);
        flex-shrink: 0;
        &--active {
            color: #262626;
            background-color: rgba(39, 163, 79, 0.2);
            border: 1px solid #27a34f;
            color: #27a34f;
        }
    }
}

.goods {
    position: relative;
    flex: 1;
    border-top: 1px solid #f6f6f6;

    &__scroll {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1;
        background-color: #fff;
    }

    &__list {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        padding: px(35) 0 0 0;
        background-color: #fff;
        border-top: 1px solid #f6f6f6;
    }

    &__item {
        width: 100%;
        margin-bottom: px(60);
        border-bottom: 1px solid #f1f1f1;
    }
    .main {
        width: 100%;
        overflow: hidden;
        // border-radius: px(20) px(20) 0 0;
        display: flex;
        justify-content: space-between;
        padding: 0 px(35);
    }

    &__img {
        flex-shrink: 0;
        margin-right: px(30);
        /deep/ img {
            width: px(230);
            height: px(230);
        }
    }

    &__info {
        display: flex;
        justify-content: space-between;
        flex-direction: column;
        align-items: flex-start;
        position: relative;
        width: 100%;
    }

    &__box {
        width: 100%;
    }
    &__title {
        width: 100%;
        @include omits(2);
        max-height: px(88);
        line-height: px(44);
        margin-top: px(30);
        font-size: px(40);
        color: #333;
    }

    &__price {
        // margin-top: px(30);
    }
    &__sale {
        font-size: px(36);
        line-height: px(40);
        color: #999;
        margin-top: px(20);
        span ~ span {
            margin-left: px(20);
        }
        &.flex {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        &.black {
            color: #666;
            margin-top: px(30);
        }
    }

    &__present {
        font-size: px(36);
        color: #000;
        font-weight: 600;
    }

    &__original {
        position: relative;
        font-size: px(30);
        color: #999;
        margin-top: px(20);
        text-decoration: line-through;
        &:after {
            content: "";
            position: absolute;
            top: 50%;
            left: 0;
            z-index: 1;
            width: 100%;
            height: px(1);
            transform: translateY(-100%);
            background-color: #999;
        }
    }
    .btns {
        overflow: visible;
        // position: absolute;
        // bottom: 0;
        // right: 0;
        // z-index: 1;
        width: 100%;
        display: flex;
        align-items: center;
        max-width: 100%;
        border-top: 1px solid #f1f1f1;
        margin-top: px(20);
    }
    .btn {
        width: 25%;
        height: px(100);
        line-height: px(100);
        // background-color: #27a34f;
        color: #333;
        font-size: px(40);
        padding: 0 px(20);
        text-align: center;
        & ~ .btn {
            border-left: 1px solid #f1f1f1;
        }
    }
    .cart-num {
        // padding: px(30) px(10) px(10) px(30);
        padding: 0 px(30);
        box-sizing: border-box;
        min-width: px(60);
    }
    .num-input {
        overflow: visible;
        position: absolute;
        bottom: 0;
        right: 0;
        z-index: 1;
    }

    &__remove {
        background-color: #fff;
        width: px(60);
        height: px(60);
        border-radius: 50%;
        border: 1px solid #999;
        position: relative;
        &::after {
            content: "";
            position: absolute;
            top: 50%;
            left: 50%;
            z-index: 1;
            width: 60%;
            height: px(6);
            border-radius: px(6);
            background: #999;
            transform: translate(-50%, -50%);
        }
    }
}

.menu {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: px(110);
    margin-bottom: px(5);
    padding: 0 px(85);
    background-color: #fff;
    // box-shadow: 0 px(-5) px(10) #333;

    &__item {
        display: flex;
        align-items: center;
    }

    &__text {
        font-size: px(36);
        color: #888;
    }

    &__item--active &__text {
        color: #27a34f;
    }

    &__sort {
        margin-left: px(15);

        &:before {
            display: block;
            content: "";
            width: 0;
            height: 0;
            margin-bottom: px(10);
            border-width: px(12);
            border-style: solid;
            border-color: transparent transparent #ccc transparent;
        }

        &:after {
            display: block;
            content: "";
            width: 0;
            height: 0;
            border-width: px(12);
            border-style: solid;
            border-color: #ccc transparent transparent transparent;
        }

        &--up:before {
            border-color: transparent transparent #27a34f transparent;
        }

        &--down:after {
            border-color: #27a34f transparent transparent transparent;
        }
    }
}

.more-text {
    display: flex;
    justify-content: center;
    padding-bottom: px(60);
    font-size: px(34);
    color: #999;
}

.null {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-top: px(115);

    &__img {
        width: px(576);
        height: px(281);
    }

    &__title {
        margin-top: px(60);
        font-size: px(40);
        color: #666;
    }
}

.page_red {
    /deep/ {
        .category {
            &__item {
                &--active {
                    &:before {
                        background-color: #fe6661;
                    }
                }
            }
        }
        .category-sub {
            &__item {
                &--active {
                    background-color: rgba(254, 102, 97, 0.2);
                    border: 1px solid #fe6661;
                    color: #fe6661;
                }
            }
        }
        .category-sub-all {
            &__item {
                &--active {
                    color: #262626;
                    background-color: rgba(254, 102, 97, 0.2);
                    border: 1px solid #fe6661;
                    color: #fe6661;
                }
            }
        }
        .goods {
            &__add-text {
                background-color: #fe6661;
            }
            &__add {
                background-color: #fe6661;
                box-shadow: px(1) px(2) px(6) #fe6661;
            }
        }

        .menu {
            .menu__item--active .menu__text {
                color: #fe6661;
            }

            &__sort {
                &--up:before {
                    border-color: transparent transparent #fe6661 transparent;
                }

                &--down:after {
                    border-color: #fe6661 transparent transparent transparent;
                }
            }
        }
    }
}
.item_one {
    border-bottom: 3px solid #c9c9c9;
    background-color: #efefef;
    padding: px(20) 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
.switch {
    transform: scale(0.7);
}
.canvas {
    position: fixed;
    top: -100%;
    left: -100%;
    z-index: -11;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    visibility: hidden;
    &__content {
        position: relative;
        width: 100%;
        height: 100%;
        padding: px(200) px(80) 0;
        z-index: 10;
    }

    &__close {
        position: absolute;
        top: px(50);
        right: px(120);
        z-index: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        width: px(100);
        height: px(100);
        border-radius: 50%;
        border: solid px(3) #fff;

        &:after {
            content: "";
            position: absolute;
            top: px(100);
            left: 50%;
            z-index: 1;
            display: block;
            width: px(3);
            height: px(50);
            transform: translateX(-50%);
            background-color: #fff;
        }
    }

    &__img {
        width: px(60);
        height: px(60);
    }

    &__box {
        width: px(920);
        height: px(1483);
        background-color: #fff;
    }
}
.remove-icon {
    padding-left: px(20);
}
.quick-add {
    font-size: px(44);
    color: rgb(0, 188, 38);
    flex-shrink: 0;
    margin-right: px(30);
}
.status-tab {
    display: flex;
    justify-content: center;
    font-size: px(44);
    text-align: center;
    margin: px(40) 0;
    span {
        border: 1px solid #f1f1f1;
        padding: px(20) 0;
        width: 40%;
        flex-shrink: 0;
        &.on {
            border-color: rgb(0, 188, 38);
            color: rgb(0, 188, 38);
        }
    }
}
</style>