<template>
    <!-- 余额明细 -->
    <div class="balance-statement">
        <el-card shadow="never" class="!border-none">
            <el-alert
                title="温馨提示： 用户账户余额变动记录"
                type="success"
                :closable="false"
                show-icon
            />

            <el-form :model="formData" label-width="90px" inline class="mt-4">
                <el-form-item label="用户信息">
                    <el-input class="w-[280px]" v-model="formData.userInfo" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="变动类型">
                    <el-select v-model="formData.changeType" class="w-[280px]" placeholder="请选择">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="item in typeLists"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="记录时间">
                    <daterange-picker
                        v-model:startTime="formData.startTime"
                        v-model:endTime="formData.endTime"
                    >
                    </daterange-picker>
                </el-form-item>
                <el-form-item>
                    <div class="m-l-20">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="never" class="mt-4 !border-none">
            <div class="m-t-20">
                <el-table
                    ref="tableData"
                    :data="pager.lists"
                    style="width: 100%"
                    v-loading="pager.loading"
                >
                    <el-table-column prop="userSn" label="用户编号" min-width="100" />
                    <el-table-column prop="nickname" label="用户昵称" min-width="220">
                        <template #default="scope">
                            <div class="flex items-center">
                                <div class="w-[60px] h-[60px]">
                                    <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="'contain'"
                                    />
                                </div>
                                <el-tooltip :content="scope.row.nickname" placement="top">
                                    <div class="ml-2 truncate">{{ scope.row.nickname }}</div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="changeAmount" label="变动金额" min-width="140">
                        <template #default="scope">
                            <span v-if="scope.row.action === 1">+{{ scope.row.changeAmount }}</span>
                            <span v-else class="red">-{{ scope.row.changeAmount }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column property="leftAmount" label="剩余金额" min-width="140" />
                    <el-table-column property="changeTypeDesc" label="变动类型" min-width="140" />
                    <el-table-column property="associationSn" label="来源单号" min-width="140" />
                    <el-table-column property="createTime" label="记录时间" min-width="140" />
                </el-table>
            </div>

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

<script lang="ts" setup>
import { apiAccountLogLists } from '@/api/finance/balance'
import { ref } from 'vue'
import Pagination from '@/components/pagination/index.vue'
import { usePaging } from '@/hooks/usePaging'

interface FormDataObj {
    userInfo: string // 昵称搜索
    changeType: number | string // 变动类型
    startTime: string // 记录开始时间
    endTime?: string // 记录结束时间
}

// 表单数据
const formData = ref<FormDataObj>({
    userInfo: '',
    changeType: '',
    startTime: '',
    endTime: ''
})

const typeLists: any = ref([
    { id: 1001, name: '管理员增加可用余额' },
    { id: 1002, name: '管理员扣减可用余额' },
    { id: 1003, name: '用户充值增加可用余额' },
    { id: 1004, name: '用户下单扣减可用余额' },
    { id: 1005, name: '售后退还余额' },
    { id: 1006, name: '佣金提现增加可用余额' }
])
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiAccountLogLists,
    params: formData.value
})

getLists()
</script>

<style lang="scss" scoped>
.ls-input {
    width: 170px;
}

.red {
    color: red;
}
.el-alert--success.is-light {
    background-color: #edefff;
    color: #4a5dff;
}
</style>
