<!-- 查询管理-其它查询-出入金查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #accountcode="{ item }">
                    <el-form-item :label="item.label" prop="accountcode">
                        <app-select-account v-model="item.value" @change="onAccountChange" />
                    </el-form-item>
                </template>
                <template #after>
                    <el-form-item :label="t('query.other.bank_inout.date')" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" />
                    </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 #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_other_bankinout_details'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <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, flag: 2 }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryPutOrIn } from '@/services/api/account'
import { getAllBankCus } from '@/services/api/bank'
import AppSelectAccount from '@pc/components/modules/select-account/index.vue'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'

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

const executetypeEnum = useEnum('executetype') // 类型
const applystatusEnum = useEnum('applystatus') // 状态
const usertype2Enum = useEnum('usertype2') // 账户类型

const dateValue = shallowRef<string[] | null>([])

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

const { dataList: bankList } = useRequest(getAllBankCus)

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


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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountcode', label: 'query.other.bank_inout.accountcode' },
    { field: 'accountname', label: 'query.other.bank_inout.accountname' },
    { field: 'usertype', label: 'query.other.bank_inout.usertype', formatValue: (val) => usertype2Enum.getEnumTypeName(val) },
    { field: 'parentaccountname', label: 'query.other.bank_inout.parentaccountname' },
    { field: 'cusbankname', label: 'query.other.bank_inout.cusbankname' },
    { field: 'tradedate', label: 'query.other.bank_inout.tradedate' },
    { field: 'amount', label: 'query.other.bank_inout.amount' },
    { field: 'charge', label: 'query.other.bank_inout.charge' },
    { field: 'currency', label: 'query.other.bank_inout.currency' },
    { field: 'executetype', label: 'query.other.bank_inout.executetype', formatValue: (val) => executetypeEnum.getEnumTypeName(val) },
    { field: 'applystatus', label: 'query.other.bank_inout.applystatus', formatValue: (val) => applystatusEnum.getEnumTypeName(val) },
    { field: 'updatetime', label: 'query.other.bank_inout.updatetime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Model.PutOrInReq>({
    filters: [
        {
            field: 'executetype',
            label: t('query.other.bank_inout.executetype'),
            options: () => executetypeEnum.getEnumOptions()
        },
        {
            field: 'applystatus',
            label: t('query.other.bank_inout.applystatus'),
            options: () => applystatusEnum.getEnumOptions()
        },
        {
            field: 'cusbankid',
            label: t('query.other.bank_inout.cusbankname'),
            options: () => bankList.value.map((e) => ({
                label: e.cusbankname,
                value: e.cusbankid
            }))
        },
        {
            field: 'usertype',
            label: t('query.other.bank_inout.usertype'),
            options: () => usertype2Enum.getEnumOptions()
        },
        {
            label: t('query.other.bank_inout.loginid'),
            field: 'loginid'
        },
        {
            label: t('query.other.bank_inout.accountcode'),
            field: 'accountcode'
        },
        {
            label: t('query.other.bank_inout.accountname1'),
            field: 'accountname',
            placeholder: t('query.other.bank_inout.tips1'),
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

const onSearch = () => {
    const qs = getQueryParams()
    const [startDate, endDate] = dateValue.value || []
    qs.tradedatestart = startDate
    qs.tradedateend = endDate
    run(qs)
}

const onReset = () => {
    dateValue.value = []
    resetFilters()
}
</script>