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

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

const queryFormRef = ref(ElForm);

const state = reactive({
    loading: true,
    searchBox: {
        title: "展开搜索",
        expand: false,
    },
    queryParams: {
        pageNo: 1,
        pageSize: 15,
        account: "",
        mobilePhone: "",
        unit: "",
        property: "",
        direction: "",
    } as WithDrawalsQueryParam,
    list: [] as WithDrawalsItem[],
    total: 0,
    allSymbolMap: [] as Symbol[],
});

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

function handleQuery() {
    state.loading = true;
    withdrawManage(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 = null;
        state.queryParams.direction = null;
    }
    handleQuery();
}

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

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

/**
 * 展开/收起搜索
 */
const closeSearch = () => {
    state.searchBox.expand = !state.searchBox.expand;
    state.searchBox.title = !state.searchBox.expand ? "展开搜索" : "收起搜索";
};

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="mobilePhone" v-show="searchBox.expand">
                <el-tooltip
                    content="请输入手机号查询"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.mobilePhone"
                        placeholder="请输入手机号查询"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </el-form-item>
            <el-form-item prop="unit" v-show="searchBox.expand">
                <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-button
                    link
                    @click="closeSearch"
                    style="border: 0"
                    size="small"
                >
                    {{ searchBox.title }}
                    <template v-if="searchBox.expand"
                        ><el-icon class="el-icon--right"><ArrowUp /></el-icon
                    ></template>
                    <template v-else
                        ><el-icon class="el-icon--right"><ArrowDown /></el-icon
                    ></template>
                </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="memberUsername"
                min-width="180"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="币种名称"
                prop="unit"
                min-width="110"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="提币个数"
                prop="totalAmount"
                min-width="120"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="实际到账数"
                prop="arrivedAmount"
                min-width="120"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="提币手续费"
                prop="fee"
                min-width="120"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="钱包地址"
                prop="address"
                min-width="200"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="哈希值"
                prop="transactionNumber"
                min-width="280"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="交易时间"
                prop="createTime"
                min-width="160"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="审核时间"
                prop="dealTime"
                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>
