<!-- 查询管理-其它查询-机构资金查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <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>
        </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 { useEnum } from '@/hooks/enum'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
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 { parsePercent } from '@/filters'

const { global: { t } } = i18n
const dateValue = shallowRef<string[] | null>([])

const { getEnumOptions } = useEnum('accountstatus') // 业务类型

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.institution_bank.accountid' },
    { field: 'accountcodename', label: 'query.other.institution_bank.accountcodename' },
    { field: 'balance', label: 'query.other.institution_bank.balance' },
    { field: 'currentbalance', label: 'query.other.institution_bank.currentbalance' },
    { field: 'inamount', label: 'query.other.institution_bank.inamount' },
    { field: 'outamount', label: 'query.other.institution_bank.outamount' },
    { field: 'closepl', label: 'query.other.institution_bank.closepl' },
    { field: 'floatpl', label: 'query.other.institution_bank.floatpl' },
    { field: 'freezecharge', label: 'query.other.institution_bank.freezecharge' },
    { field: 'paycharge', label: 'query.other.institution_bank.paycharge' },
    { field: 'otherpay', label: 'query.other.institution_bank.otherpay' },
    { field: 'otherincome', label: 'query.other.institution_bank.otherincome' },
    { field: 'freezemargin', label: 'query.other.institution_bank.freezemargin' },
    { field: 'usedmargin', label: 'query.other.institution_bank.usedmargin' },
    { field: 'otherfreezemargin', label: 'query.other.institution_bank.otherfreezemargin' },
    { field: 'outamountfreeze', label: 'query.other.institution_bank.outamountfreeze' },
    { field: 'avaiablemoney', label: 'query.other.institution_bank.avaiablemoney' },
    { field: 'avaiableoutmoney', label: 'query.other.institution_bank.avaiableoutmoney' },
    { field: 'floatnetvalue', label: 'query.other.institution_bank.floatnetvalue' },
    { field: 'cur_risk_rate', label: 'query.other.institution_bank.cur_risk_rate', formatValue: (val) => parsePercent(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

// 表单验证规则
const filterRules: FormRules = {
    date: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.InvestorReq>({
    filters: [
        {
            field: 'memberid',
            label: t('query.other.institution_bank.memberid'),
            placeholder: t('common.pleaseenter'),
        },
        {
            field: 'accountstatus',
            label: t('query.other.institution_bank.accountstatus'),
            required: true,
            placeholder: t('common.pleasechoice'),
            options: () => getEnumOptions()
        },
        {
            field: 'sortfield',
            label: t('query.other.institution_bank.sortfield'),
            required: true,
            value: 0,
            options: () => [
                { label: t('query.other.institution_bank.nolimit'), value: 0 },
                { label: t('query.other.institution_bank.currentbalance'), value: 1 },
                { label: t('query.other.institution_bank.balance'), value: 2 },
                { label: t('query.other.institution_bank.inamount'), value: 3 },
                { label: t('query.other.institution_bank.outamount'), value: 4 },
                { label: t('query.other.institution_bank.closepl'), value: 5 },
                { label: t('query.other.institution_bank.paycharge'), value: 6 }
            ]
        },
        {
            field: 'sorttype',
            value: 0,
            options: () => [
                { label: t('query.other.institution_bank.sorttype1'), value: 1 },
                { label: t('query.other.institution_bank.sorttype2'), value: 0 }
            ]
        },
    ],
    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 = () => {
    dateValue.value = []
    resetFilters()
}
</script>