<!-- 收益查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <!-- 成交时间 -->
            <template #tradetimedisplay="{ value }">
                {{ formatDate(value) }}
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { getTHJMarketList } from '@/constants/market'
import { getTHJProfitRoleTypeName } from '@/constants/account'
import { useDataTable, useDataFilter } from '@/hooks/datatable'
import { queryTHJProfits } from '@/services/api/common'
import { loginStore } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'

const { userId } = loginStore.$mapGetters()
const { dataList, total, pageIndex, pageSize } = useDataTable<Model.THJProfitsRsp>()
const { filterOptons, getQueryParams } = useDataFilter<Model.THJProfitsReq>()
const loading = shallowRef(false)

const tableColumns = shallowRef<Model.TableColumn[]>([
    { prop: 'marketname', label: '市场' },
    { prop: 'firend', label: '下单好友' },
    { prop: 'buyorselldisplay', label: '方向' },
    { prop: 'wrstandardname', label: '商品', width: 140 },
    { prop: 'tradeqty', label: '成交数量' },
    { prop: 'chargeamount', label: '手续费总额' },
    { prop: 'profitamount', label: '分润金额' },
    { prop: 'profitroletypedisplay', label: '分润角色' },
    { prop: 'brokerrate', label: '会员比例' },
    { prop: 'brokerprofitrate', label: '会员释出比例' },
    { prop: 'levelonevalue', label: '一级比例' },
    { prop: 'leveltwovalue', label: '二级比例' },
    { prop: 'tradeid', label: '成交单号', width: 200 },
    { prop: 'tradetimedisplay', label: '成交时间', width: 180 },
])

filterOptons.selectList = [
    {
        label: '市场',
        key: 'marketid',
        options: getTHJMarketList(),
    },
    {
        label: '角色',
        key: 'profitroletype',
        options: [
            { label: getTHJProfitRoleTypeName(3), value: 3 },
            { label: getTHJProfitRoleTypeName(4), value: 4 }
        ],
    },
]

filterOptons.inputList = [
    { label: '好友', keys: ['accountname'] },
    { label: '商品', keys: ['goods'] },
]

filterOptons.buttonList = [
    { lable: '重置', onClick: () => onSearch(true) },
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
]

const getTHJProfits = (params: Partial<Model.THJProfitsReq> = {}) => {
    loading.value = true
    return queryTHJProfits({
        data: {
            userid: userId.value,
            page: pageIndex.value,
            pagesize: pageSize.value,
            ...params,
        },
        success: (res) => {
            total.value = res.total
            dataList.value = res.data
        },
        complete: () => {
            loading.value = false
        }
    })
}

const onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        getTHJProfits(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => {
        getTHJProfits(qs).catch((err) => ElMessage.error(err))
    })
}

onRefresh()
</script>