<template>
    <div class="app-container">
        <el-row>
            <el-col :span="22">
                <!-- 搜索表单 -->
                <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                    <el-form-item prop="customerMemberId">
                        <el-tooltip
                            content="请输入学生会员ID搜索"
                            placement="bottom"
                            effect="light"
                        >
                            <el-input
                                v-model="queryParams.customerMemberId"
                                placeholder="请输入学生会员ID搜索"
                                style="width: 260px"
                                clearable
                                @keyup.enter="handleQuery"
                            />
                        </el-tooltip>
                    </el-form-item>
                    <el-form-item prop="traderMemberId">
                        <el-tooltip
                            content="请输入交易员会员ID搜索"
                            placement="bottom"
                            effect="light"
                        >
                            <el-input
                                v-model="queryParams.traderMemberId"
                                placeholder="请输入交易员会员ID搜索"
                                style="width: 260px"
                                clearable
                                @keyup.enter="handleQuery"
                            />
                        </el-tooltip>
                    </el-form-item>
                    <el-form-item prop="positionId" v-show="searchBox.expand">
                        <el-tooltip
                            content="请输入用户手机号搜索"
                            placement="bottom"
                            effect="light"
                        >
                            <el-input
                                v-model="queryParams.positionId"
                                placeholder="仓位id"
                                style="width: 260px"
                                clearable
                                @keyup.enter="handleQuery"
                            />
                        </el-tooltip>
                    </el-form-item>
                    <el-form-item
                        prop="traderLevelId"
                        v-show="searchBox.expand"
                    >
                        <el-tooltip
                            content="请选择交易员等级"
                            placement="bottom"
                            effect="light"
                        >
                            <el-select
                                v-model="queryParams.closeType"
                                class="m-2"
                                placeholder="请选择交易员等级"
                                size="small"
                            >
                                <el-option
                                    v-for="(item, index) in state.closeType"
                                    :key="index"
                                    :label="item"
                                    :value="index"
                                />
                            </el-select>
                        </el-tooltip>
                    </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-col>
        </el-row>
        <el-table
            :data="listdata"
            v-loading="state.loading"
            highlight-current-row
            border
        >
            <!-- <el-table-column align="center" prop="customerId" label="用户ID" />
             -->
            <el-table-column type="index" width="50" align="center" />
            <el-table-column
                align="center"
                prop="customerMemberId"
                label="学生会员ID"
            />
            <el-table-column
                align="center"
                prop="customerName"
                label="学生昵称"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="traderMemberId"
                label="交易员会员ID"
                min-width="90"
            />
            <el-table-column
                align="center"
                prop="traderName"
                label="交易员昵称"
            />
            <el-table-column align="center" prop="positionId" label="仓位ID" />
            <el-table-column
                align="center"
                prop="closePositionNo"
                label="平仓委托单号"
                min-width="100"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="contractOrderEntrustId"
                label="合约委托单号"
                min-width="100"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="copyContractOrderEntrustId"
                label="跟随平仓委托单号"
                min-width="120"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="entrustPrice"
                label="委托价格"
            >
                <template #default="scope">
                    <span>{{ truncation(scope.row.entrustPrice) }}</span>
                </template>
            </el-table-column>

            <el-table-column
                align="center"
                prop="error"
                label="平仓委托失败原因"
                min-width="120"
                show-overflow-tooltip
            />

            <el-table-column align="center" prop="quantity" label="平仓张数">
                <template #default="scope">
                    <span>{{ truncation(scope.row.quantity) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="transactionAmount"
                label="成交金额"
            >
                <template #default="scope">
                    <span>{{ truncation(scope.row.transactionAmount) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="transactionFee"
                label="交易手续费"
            >
                <template #default="scope">
                    <span>{{ truncation(scope.row.transactionFee) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="transactionPrice"
                label="成交价格"
            >
                <template #default="scope">
                    <span>{{ truncation(scope.row.transactionPrice) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="closeType"
                label="平仓类型"
                min-width="110"
            >
                <template #default="scope">
                    <span>{{ state.closeType[scope.row.closeType] }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="status" label="状态">
                <template #default="scope">
                    <span>{{
                        scope.row.status == 0
                            ? "委托中"
                            : scope.row.status == 1
                            ? "已撤单"
                            : scope.row.status == 2
                            ? "委托失败"
                            : "已成交"
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="type" label="委托类型">
                <template #default="scope">
                    <span>{{ scope.row.type == 0 ? "市价" : "限价" }}</span>
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="updatedTime"
                label="操作时间"
                width="150"
            >
                <template #default="scope">
                    <span>{{ timestampToTime(scope.row.updatedTime) }}</span>
                </template>
            </el-table-column>

            <!-- <el-table-column
                align="center"
                prop="entrustTime"
                label="委托时间"
                width="150"
            >
                <template #default="scope">
                    <span>{{ timestampToTime(scope.row.entrustTime) }}</span>
                </template>
            </el-table-column> -->
            <!-- <el-table-column
                align="center"
                prop="transactionTime"
                label="成交时间"
                width="150"
            >
                <template #default="scope">
                    <span>{{
                        timestampToTime(scope.row.transactionTime)
                    }}</span>
                </template>
            </el-table-column> -->
        </el-table>
        <pagination
            v-if="state.total > 0"
            :total="state.total"
            v-model:page="state.queryParams.currentPage"
            v-model:limit="state.queryParams.pageSize"
            @pagination="handleQuery"
        />
    </div>
</template>

<script lang="ts" setup>
import { liquidationRecord } from "@/api/documentary/index";
import { recordItme } from "@/types/api/documentary/index";
import { timestampToTime, truncation } from "@/utils/filter";
import { reactive, onMounted, toRefs, ref } from "vue";
import { Search, Refresh, ArrowUp, ArrowDown } from "@element-plus/icons-vue";
import { ElForm } from "element-plus";

const queryFormRef = ref(ElForm);

const state = reactive({
    loading: true,
    total: 0,
    queryParams: {
        closeType: "",
        currentPage: 1,
        customerMemberId: "" as any,
        pageSize: 15,
        positionId: "" as any,
        traderMemberId: "" as any,
    },
    listdata: [] as recordItme[],
    searchBox: {
        title: "展开搜索",
        expand: false,
    },
    closeType: [
        "平仓",
        "市价全平",
        "一键平仓",
        "反向开仓",
        "止盈止损",
        "爆仓",
        "管理员平仓",
        "跟单账户止盈",
        "跟单账号止损",
        "跟单仓位止盈",
        "跟单仓位止损",
        "跟单用户平仓",
    ],
});
const { listdata, queryParams, searchBox } = toRefs(state);
const handleQuery = () => {
    if (state.queryParams.customerMemberId == "") {
        state.queryParams.customerMemberId == "";
    } else {
        state.queryParams.customerMemberId = Number(
            state.queryParams.customerMemberId
        );
    }
    if (state.queryParams.traderMemberId == "") {
        state.queryParams.traderMemberId == "";
    } else {
        state.queryParams.traderMemberId = Number(
            state.queryParams.traderMemberId
        );
    }
    liquidationRecord(state.queryParams).then((res: any) => {
        state.listdata = res.data.records;
        state.total = res.data.total;
        state.loading = false;
    });
};

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

/**
 * 查询重置
 */
function resetQuery() {
    queryFormRef.value.resetFields();
    state.queryParams.closeType = "";
    handleQuery();
}

onMounted(() => handleQuery());
</script>

<style lang="less" scoped></style>
