<!-- 查询管理-内部订单查询-账户成交汇总 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <!-- 商品 -->
                <template #goodsid="{ item }">
                    <el-form-item :label="item.label" prop="goodsid">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId: queryParams.marketid ?? -1, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <!-- 日期 -->
                <template #startdate="{ item }">
                    <el-form-item :label="item.label" prop="startdate">
                        <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>
                <!-- 账户 -->
                <template #accountid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-account v-model="item.value" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_accountsumm_export'])"
                    @click="openComponentOnClick" />
            </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, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

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

const { getMarketOptions } = useMarket()

const { global: { t } } = i18n
const goodsRef = ref() // 商品组件实例
const dateValue = ref<string[] | null>([])

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradeDate', label: 'query.internal.accountsumm.tradeDate' },
    { field: 'userfullname', label: 'query.internal.accountsumm.userfullname' },
    { field: 'goodscode', label: 'query.internal.accountsumm.goodsfullname' },
    { field: 'membername', label: 'query.internal.accountsumm.membername' },
    { field: 'parentname', label: 'query.internal.accountsumm.parentname' },
    { field: 'tradeqtysum', label: 'query.internal.accountsumm.tradeqtysum' },
    { field: 'tradeamountsum', label: 'query.internal.accountsumm.tradeamountsum' },
    { field: 'closeplsum', label: 'query.internal.accountsumm.closeplsum' },
    { field: 'chargesum', label: 'query.internal.accountsumm.chargesum' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.AccountTransactionsReq>({
    filters: [
        {
            field: 'marketid',
            label: t('query.internal.accountsumm.marketid'),
            required: true,
            options: () => getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsid',
            label: t('query.internal.accountsumm.goodsid'),
            required: true
        },
        {
            field: 'summode',
            label: t('query.internal.accountsumm.summode'),
            value: 1,
            options: () => [
                { label: t('query.internal.accountsumm.summode1'), value: 1 },
                { label: t('query.internal.accountsumm.summode2'), value: 2 },
            ]
        },
        {
            field: 'startdate',
            label: t('query.internal.accountsumm.date'),
            required: true
        },
        {
            field: 'accountid',
            label: t('query.internal.accountsumm.accountid')
        },
        {
            field: 'excludeaccountidsstr',
            label: t('query.internal.accountsumm.excludeaccountidsstr'),
            placeholder: t('query.internal.accountsumm.tips3'),
            width: 260
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

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

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    const [startDate, endDate] = dateValue.value || []
    qs.startdate = startDate
    qs.enddate = endDate
    run(qs)
}

const onReset = () => {
    resetFilters()
    dateValue.value = []
    goodsRef.value.onReset()
}

const openComponentOnClick = (code: string) => {
    //openComponent(code)
}
</script>