<template>
    <view class="container clearfix">
        <tui-skeleton v-if="skeletonShow" backgroundColor="#fafafa" borderRadius="10rpx" :isLoading="true"
            :loadingType="5" />
        <template v-else>
            <view class="container-list">
                <view class="empty-container" v-if="showEmpty">
                    <image class="empty-container-image"
                        src="https://static.caimei365.com/app/img//icon/icon-remarks-empty@2x.png"></image>
                    <text class="error-text">暂无任务~</text>
                </view>
                <template v-else>
                    <view v-for="(task, index) in taskList" :key="index" :id="task.id" class="task-list"
                        @click.stop="handleTaskDetails(task)">
                        <view class="list-cell-le">
                            <image class="le-image" :src="task.topPic" mode=""></image>
                        </view>
                        <view class="list-cell-ri">
                            <view class="ri-icon" :class="{
                            			reviewed: task.auditStatus == 1,
                            			approved: task.auditStatus == 2,
                            			failed: task.auditStatus == 3
                            		}">
                            </view>
                            <view class="list-cell-top">
                                <text>{{ task.title }}</text>
                            </view>
                            <view class="list-cell-time">{{ task.startTime }} - {{ task.endTime }}</view>
                            <view class="list-cell-btn">
                                <view class="le-tags">
                                    <text>奖金:{{ task.reward }}元</text>
                                </view>
                                <button class="button add" @click.stop="handleAdd(task)"
                                    v-if="task.auditStatus === 0 || task.auditStatus === 3">
                                    {{ task.auditStatus === 3 ? '重新上传' : '上传截图'  }}
                                </button>
                                <button open-type="share" @tap.native.stop="handleShare($event)" class="button share"
                                    :data-task="task" v-if="task.receiveFlag === 1">
                                    分享
                                </button>
                            </view>
                        </view>
                    </view>
                    <!--加载loadding-->
                    <tui-loadmore :visible="loadding" :index="3" type="black" />
                    <tui-nomore :visible="!pullUpOn" :backgroundColor="'#F7F7F7'" :text="nomoreText" />
                    <!--加载loadding-->
                </template>
            </view>
        </template>
        <!-- 弹窗提示 -->
        <tui-modal :show="modal" @click="handleClick" @cancel="hideMobel" :content="contentModalText"
            :button="modalButton" color="#333" :size="32" shape="circle" :maskClosable="false">
        </tui-modal>
    </view>
</template>

<script>
    import { mapState, mapMutations } from 'vuex'
    import authorize from '@/common/config/authorize.js'
    export default {
        data() {
            return {
                listQuery: {
                    taskType: 1,
                    serviceProviderId: 0,
                    pageNum: 1,
                    pageSize: 10
                },
                taskList: [],
                skeletonShow: true,
                showEmpty: false,
                nomoreText: '上拉显示更多',
                hasNextPage: false,
                loadding: false,
                pullUpOn: true,
                pullFlag: true,
                isReceiveLoading: false, //领券操作状态
                contentModalText: '', //操作文字提示语句
                modal: false,
                modalButton: [{
                        text: '取消',
                        type: 'gray',
                        plain: true //是否空心
                    },
                    {
                        text: '去升级',
                        customStyle: {
                            color: '#fff',
                            bgColor: 'linear-gradient(90deg, #F28F31 0%, #FF5B00 100%)'
                        },
                        plain: false
                    }
                ],
            }
        },
        onLoad() {},
        computed: {
            ...mapState(['hasLogin', 'userInfo', 'identity', 'isActivity'])
        },
        methods: {
            async initGetStotage() {
                // 初始化
                const userInfo = await this.$api.getStorage()
                this.listQuery.serviceProviderId = userInfo.serviceProviderId ? userInfo.serviceProviderId : 0
                this.getTaskList()
            },
            async getTaskList() {
                // 初始化查询任务列表
                try {
                    this.listQuery.pageNum = 1
                    const res = await this.SellerService.getTaskList(this.listQuery)
                    const data = res.data
                    if (data.list && data.list.length > 0) {
                        this.showEmpty = false
                        this.hasNextPage = data.hasNextPage
                        this.taskList = []
                        this.taskList = data.list
                        this.pullFlag = false
                        setTimeout(() => {
                            this.pullFlag = true
                        }, 500)
                        if (this.hasNextPage) {
                            this.pullUpOn = false
                            this.nomoreText = '上拉显示更多'
                        } else {
                            if (this.taskList.length < 8) {
                                this.pullUpOn = true
                            } else {
                                this.pullUpOn = false
                                this.loadding = false
                                this.nomoreText = '已至底部'
                            }
                        }
                    } else {
                        this.showEmpty = true
                    }
                    setTimeout(() => {
                        this.skeletonShow = false
                    }, 500)
                } catch (e) {
                    console.log('获取任务列表失败~')
                }
            },
            async getOnReachBottomData() {
                // 上滑加载分页
                try {
                    this.listQuery.pageNum += 1
                    const res = await this.SellerService.getTaskList(this.listQuery)
                    const data = res.data
                    if (data.list && data.list.length > 0) {
                        this.hasNextPage = data.hasNextPage
                        this.taskList = this.taskList.concat(data.list)
                        this.pullFlag = false // 防上拉暴滑
                        setTimeout(() => {
                            this.pullFlag = true
                        }, 500)
                        if (this.hasNextPage) {
                            this.pullUpOn = false
                            this.nomoreText = '上拉显示更多'
                        } else {
                            this.pullUpOn = false
                            this.loadding = false
                            this.nomoreText = '已至底部'
                        }
                    }
                } catch (error) {
                    //TODO handle the exception
                    this.$util.msg(error.msg, 2000)
                }
            },
            // 跳转上传推广截图
            handleAdd(task) {
                let type = 'add'
                if (task.auditStatus === 3) {
                    type = 'edit'
                }
                this.$api.navigateTo(
                    `/pages/seller/task/task-add?type=${type}&taskId=${task.taskId}&serviceProviderId=${this.listQuery.serviceProviderId}`
                )
            },
            // 跳转推广任务详情
            handleTaskDetails(task) {
                const path =
                    `/pages/seller/task/task-details?taskId=${task.taskId}&shareSpid=${this.listQuery.serviceProviderId}`
                this.$api.navigateTo(path)
            },
            hideMobel() {
                this.modal = false
            },
            handleClick(e) {
                //个人机构跳转升级页面
                if (e.index == 1) {
                    this.$api.navigateTo('/pages/login/apply')
                }
                this.modal = false
            },
            handleShare(res) {
                //分享
                console.log('handleShare')
            }
        },
        onShareAppMessage(res) {
            //分享
            if (res.from === 'button') {
                const task = res.target.dataset.task
                const path =
                    `/pages/seller/task/task-details?taskId=${task.taskId}&shareSpid=${this.listQuery.serviceProviderId}`
                return {
                    title: task.title,
                    path: path,
                    imageUrl: task.topPic ? `${task.topPic}` : ''
                }
            }
        },
        onPullDownRefresh() {
            setTimeout(() => {
                this.getTaskList()
                uni.stopPullDownRefresh()
            }, 200)
        },
        onReachBottom() {
            if (this.hasNextPage) {
                this.loadding = true
                this.pullUpOn = true
                this.getOnReachBottomData()
            }
        },
        onShow() {
            // 隐藏右上角分享菜单
            uni.hideShareMenu()
            this.initGetStotage()
        }
    }
</script>

<style lang="scss">
    page {
        background-color: #f7f7f7;
    }

    .container {
        width: 100%;
        height: auto;
    }

    .container-list {
        box-sizing: border-box;
        padding: 24rpx;

        .empty-container-image {
            width: 260rpx;
            height: 260rpx;
            margin-top: -300rpx;
        }

        .toIndexPage {
            bottom: 390rpx;
        }

        .task-list {
            width: 100%;
            height: 270rpx;
            margin-bottom: 24rpx;
            box-sizing: border-box;
            padding: 15rpx 10rpx;
            background: #FFFFFF;
            border-radius: 16rpx;
            display: flex;
            flex-direction: row;
            overflow: hidden;

            .list-cell-le {
                width: 240rpx;
                height: 240rpx;
                position: relative;
                box-sizing: border-box;
                border: 1px solid #F7F7F7;
                border-radius: 16rpx;
                overflow: hidden;

                .le-image {
                    width: 238rpx;
                    height: 238rpx;
                    display: block;
                }
            }

            .list-cell-ri {
                width: 442rpx;
                height: 100%;
                box-sizing: border-box;
                padding: 0 0 0 20rpx;
                position: relative;
                display: flex;
                flex-direction: column;
                position: relative;

                .ri-icon {
                    width: 128rpx;
                    height: 128rpx;
                    position: absolute;
                    bottom: 5rpx;
                    left: 0;

                    &.reviewed {
                        background: url(https://static.caimei365.com/app/img/icon/icon-verify1@2x.png);
                        background-size: cover;
                    }

                    &.failed {
                        background: url(https://static.caimei365.com/app/img/icon/icon-verify2@2x.png);
                        background-size: cover;
                    }

                    &.approved {
                        background: url(https://static.caimei365.com/app/img/icon/icon-verify3@2x.png);
                        background-size: cover;
                    }
                }

                .list-cell-top {
                    width: 100%;
                    height: 80rpx;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    line-clamp: 2;
                    -webkit-box-orient: vertical;
                    line-height: 40rpx;
                    color: #333333;
                    font-size: 24rpx;
                }

                .list-cell-btn {
                    width: 100%;
                    height: 54rpx;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-top: 35rpx;

                    .le-tags {
                        height: 54rpx;
                        color: #FF5B00;
                        text-align: center;
                        font-size: 22rpx;
                        line-height: 54rpx;
                        font-weight: bold;
                    }

                    .button {
                        height: 54rpx;
                        padding: 0 25rpx;
                        box-sizing: border-box;
                        line-height: 54rpx;
                        font-size: $font-size-24;
                        background: $btn-confirm;
                        color: #FFFFFF;
                        text-align: center;
                        float: right;
                        position: relative;
                        border-radius: 30rpx;
                        margin: 0 0 0 15rpx;

                        &.share {
                            background: #07c160;
                        }

                        &.add {
                            background: #999999;
                        }
                    }
                }

                .list-cell-share {
                    width: 60rpx;
                    height: 60rpx;
                    line-height: 60rpx;
                    text-align: center;
                    position: absolute;
                    right: 0;
                    bottom: 0;
                    box-sizing: border-box;
                    display: block;
                    background: transparent;
                    border-radius: 0;
                    border: 0;
                    margin: 0;

                    .iconfont {
                        font-size: 40rpx;
                        color: #07c160;
                    }
                }

                .list-cell-time {
                    width: 100%;
                    height: 58rpx;
                    line-height: 58rpx;
                    text-align: left;
                    font-size: $font-size-20;
                    color: #999999;
                }
            }
        }
    }
</style>