<script lang="ts">
export default {
    name: "charge",
};
</script>

<script setup lang="ts">
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElForm } from "element-plus";
import { Search, Refresh } from "@element-plus/icons-vue";
import { memberDeposit } from "@/api/finance";
import { getCoinName } from "@/api/member/detail";
import { ChargeCoinParam, ChargeCoinItem } from "@/types/api/finance";
import { Symbol } from "@/types/api/member/detail";

const queryFormRef = ref(ElForm);

const state = reactive({
    loading: true,
    queryParams: {
        pageNo: 1,
        pageSize: 15,
        address: "",
        unit: "",
        property: "",
        direction: "",
        account: "",
    } as ChargeCoinParam,
    list: [] as ChargeCoinItem[],
    total: 0,
    allSymbolMap: [] as Symbol[],
});

const { loading, queryParams, list, total, allSymbolMap } = toRefs(state);

function handleQuery() {
    state.loading = true;
    memberDeposit(state.queryParams).then(({ data }) => {
        state.list = data.content;
        state.total = data.totalElements;
        state.loading = false;
    });
}

/**
 * 排序
 */
function handleSortChange(column: any) {
    var sortingType = column.order;
    state.queryParams.property = column.prop;
    if (sortingType === "ascending") {
        state.queryParams.direction = "1";
    } else if (sortingType === "descending") {
        state.queryParams.direction = "0";
    } else {
        state.queryParams.property = "id";
        state.queryParams.direction = "1";
    }
    handleQuery();
}

/**
 * 查询重置
 */
function resetQuery() {
    queryFormRef.value.resetFields();
    handleQuery();
}

const getSymbolCoinName = () => {
    getCoinName()
        .then(({ data }) => {
            state.allSymbolMap = data;
        })
        .catch((err) => {
            console.log(err);
        });
};

onMounted(() => {
    getSymbolCoinName();
    handleQuery();
});
</script>

<template>
    <div class="app-container">
        <!-- 搜索表单 -->
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item prop="account">
                <el-tooltip
                    content="请输入账户查询"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.account"
                        placeholder="请输入账户查询"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </el-form-item>
            <el-form-item prop="address">
                <el-tooltip
                    content="请输入充币地址查询"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.address"
                        placeholder="请输入充币地址查询"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </el-form-item>
            <el-form-item prop="unit">
                <span>币种：</span>
                <el-select
                    v-model="queryParams.unit"
                    class="filter-item"
                    placeholder="请选择"
                    style="width: 100px"
                >
                    <el-option
                        v-for="item in allSymbolMap"
                        :key="item.unit"
                        :label="item.unit"
                        :value="item.unit"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :icon="Search" @click="handleQuery"
                    >搜索</el-button
                >
                <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 数据表格 -->
        <el-table
            ref="dataTableRef"
            v-loading="loading"
            :data="list"
            @sort-change="handleSortChange"
            highlight-current-row
            border
        >
            <el-table-column
                label="编号"
                prop="id"
                min-width="80"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="会员昵称"
                prop="username"
                min-width="200"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="充币币种"
                prop="unit"
                min-width="110"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="充币地址"
                prop="address"
                min-width="200"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="充币数量"
                prop="amount"
                min-width="110"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="钱包余额"
                prop="walletBalance"
                min-width="110"
                align="center"
            />
            <el-table-column
                label="交易时间"
                prop="createTime"
                min-width="160"
                align="center"
                sortable="custom"
            />
        </el-table>
        <!-- 分页工具条 -->
        <pagination
            v-if="total > 0"
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
        />
    </div>
</template>
