<!--我的关注-->
<template>
    <main>
        <div class="search">
            <van-search shape="round" placeholder="输入搜索内容" v-model="name" @input="search" />
        </div>
        <div class="itemCtn">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <!--<transition-group enter-active-class="zoomInLeft" leave-active-class="zoomOutRight">-->

                <router-link
                    :to="'/doctorDetail?id='+item.ID"
                    v-for="(item,index) in list"
                    :key="index"
                    class="item"
                >
                    <div class="imgCtn">
                        <van-image
                            round
                            width="1rem"
                            height="1rem"
                            :src="item.HeadImg"
                            class="img"
                            cover
                        />
                        <div class="info">
                            <h3>
                                <b class="ellips">{{item.DoctorName}}</b>
                                <span>{{item.PositionalTitles | filterDoctorTitles}}</span>
                            </h3>
                            <p class="ellips">{{item.HospitalName}}</p>
                            <p class="ellips">{{item.HospitalDepartments}}</p>
                        </div>
                    </div>
                    <transition
                        mode="out-in"
                        enter-active-class="animated zoomIn"
                        leave-active-class="animated zoomOut"
                    >
                        <div v-if="item.isFollow" key="1">
                            <van-button
                                class="follow"
                                round
                                size="small"
                                color="#999"
                                @click.stop.prevent="CancelFollow(item.ID,index)"
                            >
                                {{targetIndex ===index?'':'取消关注'}}
                                <van-loading
                                    v-if="targetIndex === index"
                                    type="spinner"
                                    color="#fff"
                                    size=".28rem"
                                />
                            </van-button>
                        </div>
                        <div v-else key="2">
                            <van-button
                                class="follow"
                                round
                                size="small"
                                color="red"
                                @click.stop.prevent="AddFollow(item.ID,index)"
                            >
                                {{targetIndex ===index?'':'关注'}}
                                <van-loading
                                    v-if="targetIndex === index"
                                    type="spinner"
                                    size=".28rem"
                                />
                            </van-button>
                        </div>
                    </transition>
                </router-link>
                <!--</transition-group>-->
            </van-list>
        </div>
    </main>
</template>

<script>
    export default {
        name: "MyFollow",
        data() {
            return {
                name: "",
                list: [],
                loading: false,
                finished: false,
                pageIndex: 1,
                pageSize: 15,
                targetIndex: -1
            };
        },
        created() {},
        methods: {
            onLoad() {
                var obj = {
                    name: this.name,
                    pageIndex: this.pageIndex,
                    pageSize: this.pageSize
                };
                this.$http.VideoGetFollowList(obj).then(({ data }) => {
                    console.log(data);
                    this.loading = false;
                    if (data.IsSuccess) {
                        data.ResultObject.forEach(a => (a.isFollow = true));

                        data.ResultObject.length < this.pageSize
                            ? (this.finished = true)
                            : "";
                        this.list = this.list.concat(data.ResultObject);
                        this.pageIndex++;
                    } else {
                        this.$toast(data.ResultMsg);
                    }
                });
            },

            ResetLoad() {
                this.list = [];
                this.loading = true;
                this.finished = false;
                this.pageIndex = 1;
                this.onLoad();
            },

            search() {
                this.ResetLoad();
            },

            // 取消关注
            CancelFollow(id, index) {
                if (this.targetIndex != -1) {
                    return;
                }
                this.targetIndex = index;
                this.$http.VideoCancelFollow(id).then(({ data }) => {
                    if (data.IsSuccess) {
                        this.targetIndex = -1;
                        this.list[index].isFollow = false;
                        this.$toast("已取消关注");
                    } else {
                        this.$toast(data.ResultMsg);
                    }
                });
            },

            // 添加关注
            AddFollow(id, index) {
                if (this.targetIndex != -1) {
                    return;
                }
                this.targetIndex = index;
                this.$http.VideoAddFollow(id).then(({ data }) => {
                    if (data.IsSuccess) {
                        this.targetIndex = -1;
                        this.list[index].isFollow = true;
                        this.$toast("已关注");
                    } else {
                        this.$toast(data.ResultMsg);
                    }
                });
            }
        }
    };
</script>

<style scoped lang="less">
    main {
        padding-top: 1.15rem;
    }
    .search {
        position: fixed;
        width: 100%;
        top: 0;
        left: 0;
        z-index: 5;
    }
    .itemCtn {
        width: 7.1rem;
        margin: 0 auto;
        box-sizing: border-box;
    }
    .item {
        height: 1.8rem;
        display: flex;
        margin-bottom: 0.2rem;
        justify-content: space-between;
        align-items: center;
        box-shadow: 0 2px 6px #e8e0e0;
        box-sizing: border-box;
        padding: 0 0.2rem;
        border-radius: 0.2rem;
        .imgCtn {
            display: flex;
            justify-content: space-between;
        }
        .img {
            margin-right: 0.2rem;
        }
        .info {
            color: #333333;
            text-align: left;
            max-width: 4rem;
            > h3 {
                font-size: 0.34rem;
                margin-bottom: 0.1rem;
                > b {
                    font-size: 0.34rem;
                    max-width: 2.4rem;
                    display: inline-block;
                }
                > span {
                    font-size: 0.26rem;
                    margin-left: 0.2rem;
                }
            }
            > p {
                font-size: 0.26rem;
                margin-bottom: 0.1rem;
            }
        }
        .follow {
        }
    }
</style>
