<!-- 收益查询 -->
<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 { useRequest } from '@/hooks/request'
import { getTHJMarketList } from '@/constants/market'
import { getTHJProfitRoleTypeName } from '@/constants/account'
import { useDataFilter } from '@/hooks/datatable'
import { queryTHJProfits } from '@/services/api/common'
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 { filterOptons, getQueryParams } = useDataFilter<Model.THJProfitsReq>()
const { loading, dataList, total, pageIndex, pageSize, run } = useRequest(queryTHJProfits, {
    params: {
        pagesize: 20,
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

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 onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        run(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => run(qs))
}
</script>