<template>
    <div>
        <div>
            <el-form :model="query" inline label-position="left">
                <el-form-item>
                    <el-input placeholder="邀请人名称/被邀请人/昵称/手机号" v-model="query.search" style="width: 240px;" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.status" placeholder="状态" style="width: 140px;">
                        <el-option :label="item.label" :value="item.value" v-for="item in statusList"
                            :key="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <!-- <el-form-item>
                    <el-date-picker v-model="query.createdAt" type="daterange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"
                        value-format="yyyy-MM-dd HH:mm:ss">
                    </el-date-picker>
                </el-form-item> -->
                <el-form-item>
                    <el-button type="primary" @click="queryHandle">查询</el-button>
                    <el-button @click="resetHandle">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading">
                <el-table-column label="邀请人" prop="invitedName">
                    <template v-slot="scope">
                        <div class="goods_info">
                            <el-image :src="scope.row.invitedHeadImg" style="width:40px;height:40px;flex-shrink: 0;"
                                :preview-src-list="avatarUrlList1">
                                <div slot="error" class="image-slot">
                                    <i class="el-icon-folder-delete"></i>
                                </div>
                            </el-image>
                            <span>{{ scope.row.invitedName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="被邀请人" prop="beInvitedName">
                    <template v-slot="scope">
                        <div class="goods_info">
                            <el-image :src="scope.row.beInvitedHeadImg" style="width:40px;height:40px;flex-shrink: 0;"
                                :preview-src-list="avatarUrlList2">
                                <div slot="error" class="image-slot">
                                    <i class="el-icon-folder-delete"></i>
                                </div>
                            </el-image>
                            <span>{{ scope.row.beInvitedName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="邀请状态" prop="status">
                    <template v-slot="scope">
                        <template v-if="scope.row.method == 'get' && scope.row.status == 2">
                            <el-tag type="success" disable-transitions>已邀请成功</el-tag>
                        </template>
                        <template v-else-if="scope.row.method == 'use' && scope.row.status == 3">
                            <el-tag type="success" disable-transitions>已邀请成功</el-tag>
                        </template>
                        <template v-else>
                            <el-tag type="danger" disable-transitions>未邀请成功</el-tag>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="邀请时间" prop="createTime"></el-table-column>
                <el-table-column label="生效时间" prop="updateTime">
                    <template v-slot="scope">
                        <template v-if="scope.row.method == 'get'">{{ scope.row.updateTime }}</template>
                        <template v-if="scope.row.method == 'use'">{{ scope.row.rewardTime }}</template>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="head-container">
            <el-pagination :total="tableData.total" @size-change="handleSizeChange" :current-page="tableData.page"
                :page-size="tableData.size" @current-change="paginationChange"
                layout="total, sizes, prev, pager, next, jumper"></el-pagination>
        </div>
    </div>
</template>

<script>
import { byShare, byShopId } from '@/api/inviteFirend.js'
const statusList = [
    {
        value: 0,
        label: '非新用户'
    },
    {
        value: 1,
        label: '未领取'
    },
    {
        value: 2,
        label: '已领取'
    },
    {
        value: 3,
        label: '已使用'
    },
]
export default {
    data() {
        return {
            statusList,
            avatarUrlList1: [],
            avatarUrlList2: [],
            shareId: '',
            query: {
                search: '',
                status: ''
            },
            resetQuery: '',
            tableData: {
                list: [],
                page: 1,
                size: 30,
                loading: false,
                total: 0
            },
        }
    },
    filters: {
        statusFilter(t) {
            return statusList.find(item => item.value == t).label
        }
    },
    mounted() {
        this.resetQuery = { ...this.query }
        this.byShopId()
    },
    methods: {
        // 查询
        queryHandle() {
            this.getTableData()
        },
        // 重置查询
        resetHandle() {
            this.query = { ...this.resetQuery }
            this.page = 1
            this.size = 30
            this.getTableData()
        },
        // 分页大小改变
        handleSizeChange(val) {
            this.tableData.size = val
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e
            this.getTableData()
        },
        // 获取邀请设置
        async byShopId() {
            try {
                const res = await byShopId()
                this.shareId = res.id
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 获取表格数据
        async getTableData() {
            try {
                this.tableData.loading = true
                const res = await byShare({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    search: this.query.search,
                    status: this.query.status,
                    shareId: this.shareId
                })
                this.tableData.loading = false
                this.tableData.list = res.content
                this.tableData.total = res.totalElements
                // 头像预览图集合
                this.avatarUrlList1 = res.content.map(item => item.invitedHeadImg)
                this.avatarUrlList2 = res.content.map(item => item.beInvitedHeadImg)
            } catch (error) {
                console.log(error);
            }
        }
    }
}
</script>

<style scoped lang="scss">
.goods_info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.collect_wrap {
    display: flex;
    gap: 14px;

    .item {
        width: 200px;
        display: flex;
        align-items: center;
        background-color: #f5f5f5;
        padding: 20px;

        .icon_wrap {
            $size: 34px;
            $border: 6px;
            width: $size;
            height: $size;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: var(--bg-color);
            border-radius: 50%;
            position: relative;

            &::after {
                content: "";
                width: $size + $border;
                height: $size + $border;
                border-radius: 50%;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background-color: var(--bg-color);
                opacity: 0.3;
            }

            .icon {
                font-size: 16px;
                color: #fff;
            }

            .img {
                width: 20px;
                height: 20px;
            }
        }

        .info {
            flex: 1;
            display: flex;
            flex-direction: column;
            padding-left: 10px;

            .m {
                font-weight: bold;
            }

            .t {
                font-size: 12px;
                color: #999;
                padding-top: 4px;
            }
        }
    }
}
</style>