<template>
    <div class="center">
        <el-card shadow="never" class="!border-none">
            <div class="flex pb-6 mt-8 ml-4">
                <div class="flex flex-1">
                    <div class="flex flex-col">
                        <div class="text">{{ '累计退款金额（元）' }}</div>
                        <div class="flex">
                            <div class="amount mt-[6px]">
                                {{ amountData.refundTotalAmount || '0.00' }}
                            </div>
                        </div>
                    </div>
                </div>

                <div class="flex flex-1">
                    <div class="flex flex-col">
                        <div class="text">{{ '退款中金额（元）' }}</div>
                        <div class="flex">
                            <div class="amount mt-[6px]">
                                {{ amountData.refundingAmount || '0.00' }}
                            </div>
                        </div>
                    </div>
                </div>

                <div class="flex flex-1">
                    <div class="flex flex-col">
                        <div class="text">{{ '退款成功金额（元）' }}</div>
                        <div class="flex">
                            <div class="amount mt-[6px]">
                                {{ amountData.refundAmount || '0.00' }}
                            </div>
                        </div>
                    </div>
                </div>

                <div class="flex flex-1">
                    <div class="flex flex-col">
                        <div class="text">{{ '退款失败金额（元）' }}</div>
                        <div class="flex">
                            <div class="amount mt-[6px]">
                                {{ amountData.failRefundAmount || '0.00' }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </el-card>

        <el-card shadow="never" class="!border-none my-4">
            <el-form class="ls-form" :model="formData" inline>
                <el-form-item label="退款编号">
                    <el-input class="ls-input" v-model="formData.refundSn" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="来源单号">
                    <el-input class="ls-input" v-model="formData.orderSn" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="买家信息">
                    <el-input class="ls-input" v-model="formData.userInfo" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="退款类型">
                    <el-select v-model="formData.type" class="ls-input" placeholder="">
                        <el-option label="全部" value></el-option>
                        <el-option label="系统取消订单" :value="1"></el-option>
                        <el-option label="后台取消订单" :value="2"></el-option>
                        <el-option label="用户取消订单" :value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="记录时间">
                    <data-picker
                        style="width: 280px"
                        v-model:startTime="formData.orderTimeStart"
                        v-model:endTime="formData.orderTimeEnd"
                    ></data-picker>
                </el-form-item>
                <el-form-item>
                    <div class="flex">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                        <!-- <el-button @click="exportFrom">导出</el-button> -->
                    </div>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="mt-4 !border-none" shadow="never">
            <el-tabs class="-mt-2" v-model="formData.refundStatus" @tab-change="resetPage">
                <el-tab-pane :label="`全部(${countData?.totalCount || '0'})`" name="" />
                <el-tab-pane :label="`退款中(${countData?.refundingCount || '0'})`" :name="0" />
                <el-tab-pane :label="`退款成功(${countData?.refundCount || '0'})`" :name="1" />
                <el-tab-pane :label="`退款失败(${countData?.failRefundCount || '0'})`" :name="2" />
            </el-tabs>
            <div class="mt-4">
                <el-table :data="pager.lists" size="large" v-loading="pager.loading">
                    <el-table-column label="ID" property="id" min-width="80" />

                    <el-table-column label="退款编号" property="refundSn" min-width="140" />

                    <el-table-column label="买家信息" min-width="200">
                        <template #default="scope">
                            <div class="flex items-center">
                                <el-image
                                    style="width: 60px; height: 60px"
                                    :src="scope.row.avatar"
                                    :preview-src-list="[scope.row.avatar]"
                                    :hide-on-click-modal="true"
                                    :preview-teleported="true"
                                    :fit="'cover'"
                                />
                                <div class="ml-2">{{ scope.row.nickname }}</div>
                            </div>
                        </template>
                    </el-table-column>

                    <el-table-column label="来源单号" prop="orderSn" min-width="200" />

                    <el-table-column label="退款金额" min-width="160">
                        <template #default="{ row }">
                            <span>{{ `￥${row.refundAmount}` }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="退款类型" prop="typeName" min-width="160" />

                    <el-table-column label="退款状态" prop="refundStatusName" min-width="100">
                        <template #default="{ row }">
                            <el-tag type="warning" v-if="row.refundStatus == 0"> 退款中</el-tag>
                            <el-tag type="success" v-if="row.refundStatus == 1">退款成功</el-tag>
                            <el-tag type="danger" v-if="row.refundStatus == 2">退款失败</el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column label="记录时间" prop="createTime" min-width="200">
                        <template #default="{ row }">
                            <span>
                                {{ row.createTime || '-' }}
                            </span>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" width="200" fixed="right">
                        <template #default="scope">
                            <div class="flex items-center">
                                <logForm
                                    v-perms="['refund:detail']"
                                    :id="scope.row.id"
                                    :title="'退款日志'"
                                    @refresh="getLists"
                                />
                                <div v-if="scope.row.refundStatus == 2">
                                    <popup
                                        class="inline -mt-4"
                                        @confirm="handleRefund(scope.row.id)"
                                        content="确定要重新退款吗？"
                                        title="重新退款"
                                    >
                                        <template #trigger>
                                            <el-link type="primary" :underline="false"
                                                >重新退款</el-link
                                            >
                                        </template>
                                    </popup>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { getRefundList, getreRefund, getAmount, getCount } from '@/api/finance/record'
import DataPicker from '@/components/daterange-picker/index.vue'
import Pagination from '@/components/pagination/index.vue'
import { usePaging } from '@/hooks/usePaging'
import logForm from './components/log-form.vue'

const formData = ref<any>({
    refundSn: '', // 退款编号
    orderSn: '', // 来源编号
    userInfo: '', // 买家信息
    type: '', // 退款类型:1-系统取消订单;2-后台取消订单;3-用户取消订单
    refundStatus: '', // 退款状态:0-全部;1-退款中;2-退款成功;3-退款失败
    orderTimeStart: '', // 开始时间
    orderTimeEnd: '' // 结束时间
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getRefundList,
    params: formData.value
})

// 重新退款
const handleRefund = async (id: number) => {
    console.log(id)
    await getreRefund({ id })
}

const amountData = ref<any>({})
const countData = ref<any>({})
const getRefundAmount = async () => {
    amountData.value = await getAmount('')
    countData.value = await getCount('')
}

getLists()
getRefundAmount()
</script>

<style lang="scss" scoped>
.ls-input {
    width: 280px;
}
.amount {
    font-family: 'PingFang HK';
    font-weight: 400;
    font-size: 32px;
    text-align: left;
    color: #333;
}
.text {
    font-family: 'PingFang HK';
    font-weight: 400;
    font-size: 14px;
    text-align: left;
    color: #666;
}
.el-tabs__nav-wrap::after {
    height: 1px !important;
}
:deep() .el-tabs__content {
    padding: 0;
}
</style>
