<!-- 查询管理-其它查询-交易商资金查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #accountid="{ item }">
                    <el-form-item :label="item.label" prop="accountid">
                        <app-select-account v-model="item.value" @change="onAccountChange" />
                    </el-form-item>
                </template>
                <template #memberid="{ item }">
                    <el-form-item :label="item.label" prop="memberid">
                        <app-select-member v-model="item.value" :params="{ usertype: '2' }"
                            :placeholder="t('investor.custom.group.edit.tips1')" />
                    </el-form-item>
                </template>
            </app-filter>
            <div class="report-account__info" v-if="selectedAccount">
                <span>{{ t('report.account.accountid1') }}{{ selectedAccount.relatedname }}</span>
                <span>{{ t('report.account.accountid') }}{{ selectedAccount.accountid }}</span>
                <span>{{ t('report.account.invloginids') }}{{ selectedAccount.invloginids }}</span>
            </div>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref, shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryinvestor } from '@/services/api/order'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppSelectAccount from '@pc/components/modules/select-account/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { parsePercent } from '@/filters'

const { global: { t } } = i18n
const selectedAccount = shallowRef<Model.TAAccountChildrenSelectRsp>()

const { componentMap, componentId, record, closeComponent } = useOperation<Model.InvestorReq>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryinvestor, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'accountid', label: 'query.other.investor_bank.accountid' },
    { field: 'accountcodename', label: 'query.other.investor_bank.accountcodename' },
    { field: 'balance', label: 'query.other.investor_bank.balance' },
    { field: 'currentbalance', label: 'query.other.investor_bank.currentbalance' },
    { field: 'inamount', label: 'query.other.investor_bank.inamount' },
    { field: 'outamount', label: 'query.other.investor_bank.outamount' },
    { field: 'closepl', label: 'query.other.investor_bank.closepl' },
    { field: 'floatpl', label: 'query.other.investor_bank.floatpl' },
    { field: 'freezecharge', label: 'query.other.investor_bank.freezecharge' },
    { field: 'paycharge', label: 'query.other.investor_bank.paycharge' },
    { field: 'otherpay', label: 'query.other.investor_bank.otherpay' },
    { field: 'otherincome', label: 'query.other.investor_bank.otherincome' },
    { field: 'freezemargin', label: 'query.other.investor_bank.freezemargin' },
    { field: 'usedmargin', label: 'query.other.investor_bank.usedmargin' },
    { field: 'otherfreezemargin', label: 'query.other.investor_bank.otherfreezemargin' },
    { field: 'outamountfreeze', label: 'query.other.investor_bank.outamountfreeze' },
    { field: 'avaiablemoney', label: 'query.other.investor_bank.avaiablemoney' },
    { field: 'avaiableoutmoney', label: 'query.other.investor_bank.avaiableoutmoney' },
    { field: 'floatnetvalue', label: 'query.other.investor_bank.floatnetvalue' },
    { field: 'cur_risk_rate', label: 'query.other.investor_bank.cur_risk_rate', formatValue: (val) => parsePercent(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const onAccountChange = (item?: Model.TAAccountChildrenSelectRsp) => {
    selectedAccount.value = item
}

// 表单验证规则
const filterRules: FormRules = {

}

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.InvestorReq>({
    filters: [
        {
            field: 'accountcode',
            label: t('query.other.investor_bank.accountcode'),
            placeholder: t('common.pleaseenter'),
        },
        {
            field: 'memberid',
            label: t('query.other.investor_bank.memberid'),
            placeholder: t('common.pleaseenter'),
        },
        {
            field: 'sortfield',
            label: t('query.other.investor_bank.sortfield'),
            required: true,
            value: 0,
            options: () => [
                { label: t('query.other.investor_bank.nolimit'), value: 0 },
                { label: t('query.other.investor_bank.balance'), value: 1 },
                { label: t('query.other.investor_bank.balance'), value: 2 },
                { label: t('query.other.investor_bank.inamount'), value: 3 },
                { label: t('query.other.investor_bank.outamount'), value: 4 },
                { label: t('query.other.investor_bank.closepl'), value: 5 },
                { label: t('query.other.investor_bank.paycharge'), value: 6 }
            ]
        },
        {
            field: 'sorttype',
            value: 0,
            options: () => [
                { label: t('query.other.investor_bank.sorttype1'), value: 1 },
                { label: t('query.other.investor_bank.sorttype2'), value: 0 }
            ]
        },
        {
            field: 'accountid',
            label: t('query.other.investor_bank.accountid'),
            placeholder: t('common.pleaseenter'),
        },
        {
            field: 'loginid',
            label: t('query.other.investor_bank.loginid'),
            placeholder: t('common.pleaseenter'),
        },
        {
            field: 'parentarea',
            label: t('query.other.investor_bank.parentarea'),
            placeholder: t('common.pleaseenter'),
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}


const onReset = () => {
    resetFilters()
}
</script>