<!-- 兑换记录 -->
<template>
    <div>
        <div>
            <el-form :model="query" inline label-position="left">
                <el-form-item>
                    <el-input placeholder="订单编号/核销码" v-model="query.keywords" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.pickupMethod" placeholder="领取方式" style="width: 140px;">
                        <el-option label="自取" value="self"></el-option>
                        <el-option label="邮寄" value="post"></el-option>
                    </el-select>
                </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">
            <div class="collect_wrap">
                <div class="item">
                    <div class="icon_wrap" style="--bg-color:#ADDBBC">
                        <i class="icon el-icon-s-goods"></i>
                    </div>
                    <div class="info">
                        <div class="m">{{ countData.count }}</div>
                        <div class="t">总订单数</div>
                    </div>
                </div>
                <div class="item">
                    <div class="icon_wrap" style="--bg-color:#8CC4FC">
                        <i class="icon el-icon-s-goods"></i>
                    </div>
                    <div class="info">
                        <div class="m">￥{{ countData.totalAmount }}</div>
                        <div class="t">已支付金额</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading">
                <el-table-column label="订单编号" prop="orderNo" width="200"></el-table-column>
                <el-table-column label="用户名" prop="memberName" width="200">
                    <template v-slot="scope">
                        <div class="goods_info">
                            <el-image :src="scope.row.avatarUrl" style="width:40px;height:40px;flex-shrink: 0;"
                                :preview-src-list="avatarUrlList">
                                <div slot="error" class="image-slot">
                                    <i class="el-icon-folder-delete"></i>
                                </div>
                            </el-image>
                            <span>{{ scope.row.memberName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="商品" prop="pointsGoodsName" width="200">
                    <template v-slot="scope">
                        <div class="goods_info">
                            <el-image :src="scope.row.goodsImageUrl" style="width:40px;height:40px;flex-shrink: 0;"
                                :preview-src-list="srcList">
                                <div slot="error" class="image-slot">
                                    <i class="el-icon-folder-delete"></i>
                                </div>
                            </el-image>
                            <span>{{ scope.row.pointsGoodsName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="使用积分" prop="spendPoints"></el-table-column>
                <el-table-column label="支付金额" prop="extraPaymentAmount"></el-table-column>
                <el-table-column label="支付方式" prop="payMethod"></el-table-column>
                <el-table-column label="实际支付时间" prop="payTime" width="200"></el-table-column>
                <el-table-column label="下单时间" prop="createTime" width="200"></el-table-column>
                <el-table-column label="领取方式" prop="pickupMethod">
                    <template v-slot="scope">
                        <el-tag type="success" effect="plain" disable-transitions
                            v-if="scope.row.pickupMethod == 'self'">自取</el-tag>
                        <el-tag type="info" effect="plain" disable-transitions
                            v-if="scope.row.pickupMethod == 'post'">邮寄
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="status">
                    <template v-slot="scope">
                        <el-tag type="warning" disable-transitions v-if="scope.row.status == 'waiting'">待自取</el-tag>
                        <el-tag type="success" disable-transitions v-if="scope.row.status == 'done'">已完成</el-tag>
                        <el-tag type="warning" disable-transitions v-if="scope.row.status == 'unpaid'">待支付</el-tag>
                        <el-tag type="info" disable-transitions v-if="scope.row.status == 'cancel'">已取消</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="取消/退款" prop="cancelOrRefundTime" width="200">
                    <template v-slot="scope">
                        <template v-if="scope.row.status == 'cancel'">
                            <div>{{ scope.row.cancelOrRefundTime }}</div>
                            <div>{{ scope.row.cancelOrRefundReason }}</div>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template v-slot="scope">
                        <template v-if="scope.row.status == 'waiting'">
                            <el-popconfirm title="确定核销吗？" @confirm="confirmOrder(scope.row.couponCode)">
                                <el-button type="text" icon="el-icon-finished" slot="reference">待核销</el-button>
                            </el-popconfirm>
                        </template>
                        <el-button type="text" icon="el-icon-finished" disabled v-else>{{
                            scope.row.status | statusFilter }}</el-button>
                    </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 { exchangeRecordPage, exchangeRecordTotal, recordCheckout } from '@/api/points.js'
const statusList = [
    {
        value: 'unpaid',
        label: '待支付'
    },
    {
        value: 'waiting',
        label: '待自取'
    },
    {
        value: 'done',
        label: '已完成'
    },
    {
        value: 'cancel',
        label: '已取消'
    },
]
export default {
    data() {
        return {
            statusList,
            srcList: [],
            avatarUrlList: [],
            countData: {
                count: 0,
                totalAmount: 0
            },
            query: {
                keywords: '',
                beginDate: '',
                endDate: '',
                status: '',
                pickupMethod: '',
                createdAt: []
            },
            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.getTableData()
        this.exchangeRecordTotal()
    },
    methods: {
        // 核销
        async confirmOrder(couponCode) {
            try {
                this.tableData.loading = true
                await recordCheckout({ couponCode: couponCode })
                this.$notify({
                    title: '成功',
                    message: '核销成功',
                    type: 'success'
                })
                this.getTableData()
            } catch (error) {
                this.tableData.loading = false
                console.log(error);
            }
        },
        // 统计
        async exchangeRecordTotal() {
            try {
                const { count, totalAmount } = await exchangeRecordTotal({
                    keywords: this.query.keywords,
                    beginDate: this.query.createdAt[0] || '',
                    endDate: this.query.createdAt[1] || '',
                    status: this.query.status,
                    pickupMethod: this.query.pickupMethod
                })
                this.countData.count = count
                this.countData.totalAmount = totalAmount
            } catch (error) {
                console.log(error);
            }
        },
        // 查询
        queryHandle() {
            this.getTableData()
            this.exchangeRecordTotal()
        },
        // 重置查询
        resetHandle() {
            this.query = { ...this.resetQuery }
            this.page = 1
            this.size = 30
            this.getTableData()
            this.exchangeRecordTotal()
        },
        // 分页大小改变
        handleSizeChange(val) {
            this.tableData.size = val
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e
            this.getTableData()
        },
        // 获取表格数据
        async getTableData() {
            try {
                this.tableData.loading = true
                const res = await exchangeRecordPage({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    keywords: this.query.keywords,
                    beginDate: this.query.createdAt[0] || '',
                    endDate: this.query.createdAt[1] || '',
                    status: this.query.status,
                    pickupMethod: this.query.pickupMethod
                })
                this.tableData.loading = false
                this.tableData.list = res.content
                this.tableData.total = res.totalElements
                // 预览图集合
                this.srcList = res.content.map(item => item.goodsImageUrl)
                // 头像预览图集合
                this.avatarUrlList = res.content.map(item => item.avatarUrl)
            } 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>