<template>
    <div>
        <van-swipe :autoplay="3000" style="margin: 10px 10px;">
            <van-swipe-item v-for="(image, index) in images" :key="index">
                <img v-lazy="image" style="max-width: 100%"/>
            </van-swipe-item>
        </van-swipe>

        <van-row type="flex" justify="space-between" align="center" style="margin: 0 10px 0 15px">
            <van-col>
                <p><strong>我的礼物</strong></p>
            </van-col>
            <van-col>
                <van-button round type="primary" size="small" @click="toShare">邀请好友赢大奖</van-button>
            </van-col>
        </van-row>

        <ul style="background-color: #fff6e3">
            <li
                    is="index-present"
                    v-for="(present, index) in presents"
                    :key="present.id"
                    :present-name="present.name"
                    :present-desc="present.desc"
                    :publish-time="present.time"
                    :status="present.status"
                    @to-detail="toDetail(index)"
            ></li>
        </ul>
        <div style="margin: 50px 10px;">
            <p style="text-align: left; margin-left: 5px"><strong>简单四步，轻松互赠圣诞礼物</strong></p>
            <img src="../assets/declare_icons/declare.jpg" style="margin-bottom: 40px"/>
        </div>
        <add-present-button :can_add="button_active"/>
    </div>
</template>

<script>
    import IndexPresent from "../components/IndexPresent";
    import {
        Button, Row, Col,
        Swipe, SwipeItem
    } from "vant";
    import AddPresentButton from "../components/addPresentButton";
    import {BasicFunction} from '../connector/basic-service';

    export default {
        name: "index",
        components: {
            AddPresentButton,
            IndexPresent,
            [Button.name]: Button,
            [Row.name]: Row, [Col.name]: Col,
            [Swipe.name]: Swipe, [SwipeItem.name]: SwipeItem,
        },
        data() {
            return {
                images: [
                    require('../assets/banners/banner1.jpg'),
                    require('../assets/banners/banner2.jpg'),
                    require('../assets/banners/banner3.jpg'),
                ],
                active: 0,
                button_active: true,
                presents: {}
            }
        },
        methods: {
            toShare() {
                this.$router.push('share')
            },
            toDetail(i) {
                let present = this.presents[i];
                this.$router.push({
                    path: 'giftDetail', query: {
                        presentId: present.id,
                        name: present.name,
                        desc: present.desc_long,
                        status: present.status,
                        gender_tags: present.gender_tags
                    }
                });
            }
        },
        created: function () {
            // return get_present_data()

            if (!localStorage.getItem('frontend-userid')) {
                this.$router.push('login');
                return;
            }
            let userId = localStorage.getItem("frontend-userid");
            // 判断userId 是不是空的

            let presentList = [];
            BasicFunction.get_data("smartGoods/list?isDel=0&ownerId=" + userId, function (response) {
                console.log("------ Data Rcvd in Index --------");
                console.log(response);
                if (response.ret === "10000") {
                    let list = response.model.list;
                    if (list !== undefined || list.length > 0) console.warn(list);
                    list.forEach(gift => {
                        let temp = {};
                        temp.id = gift.goodid;
                        temp.name = gift.name;
                        if (gift.description.length > 45) {
                            temp.desc = gift.description.substring(0, 30);
                            temp.desc += "..."
                        }
                        else temp.desc = gift.description;
                        temp.desc_long = gift.description;
                        temp.time = gift.createTime.substring(0, 9);
                        temp.status = gift.sort;
                        temp.gender_tags = gift.goodFirstKind.toString(2);
                        presentList.push(temp);
                    });
                } else {
                    // 失败的处理
                    console.warn("获取资源失败");
                }
            }, {});

            console.log("presents:");
            console.log(presentList);
            this.presents = presentList;
        }
    }

</script>

<style scoped>

</style>