<!-- 查询管理-订单管理-订单日报表 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #startDate="{ item }">
                    <el-form-item :label="item.label" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('query.order.report.startDate')"
                            :end-placeholder="t('query.order.report.enddate')" />
                    </el-form-item>
                </template>
                <template #goodsid="{ item }">
                    <el-form-item :label="item.label" prop="goodsid">
                        <app-select-goods v-model="item.value"
                            :params="{ trademodels: '52', goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <template #areauserid="{ item }">
                    <el-form-item :label="item.label" prop="areauserid">
                        <app-select-member v-model="item.value" :params="{ usertype: '2' }" />
                    </el-form-item>
                </template>
                <template #currentUserId="{ item }">
                    <el-form-item :label="item.label" prop="currentUserId">
                        <app-select-investor 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_order_report_export'])"
                    @click="(code: string) => openComponent(code)" />
            </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 { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryOrderDate } 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 AppSelectInvestor from '@pc/components/modules/select-investor/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { i18n } from '@/stores'

const buyOrSellEnum = useEnum('buyOrSell') // 方向

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

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'reckondate', label: 'query.order.report.reckondate' },
    { field: 'tradeid', label: 'query.order.report.tradeid' },
    { field: 'userfullname', label: 'query.order.report.userfullname' },
    { field: 'areafullname', label: 'query.order.report.areafullname' },
    { field: 'goodscode', label: 'query.order.report.goodsfullname' },
    { field: 'buyorsell', label: 'query.order.report.buyorselldisplay', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'holderprice', label: 'query.order.report.holderprice' },
    { field: 'opennum', label: 'query.order.report.opennum' },
    { field: 'tradeamount', label: 'query.order.report.tradeamount' },
    { field: 'holdernum', label: 'query.order.report.holdernum' },
    { field: 'holderamount', label: 'query.order.report.holderamount' },
    { field: 'payeddeposit', label: 'query.order.report.payeddeposit' },
    { field: 'freezenum', label: 'query.order.report.freezenum' },
    { field: 'deliverynum', label: 'query.order.report.deliverynum' },
    { field: 'closepl', label: 'query.order.report.closepl' },
    { field: 'floatpl', label: 'query.order.report.floatpl' },
    { field: 'depositratedisplay', label: 'query.order.report.depositratedisplay' },
    { field: 'operatorcode', label: 'query.order.report.operatorcode' }
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.OrderDateReq>({
    filters: [
        {
            field: 'startDate',
            label: t('query.order.report.date'),
            required: true
        },
        {
            field: 'goodsid',
            label: t('query.order.report.goodsid'),
        },
        {
            field: 'tradeid',
            label: t('query.order.report.tradeid'),
        },
        {
            field: 'currentUserId',
            label: t('query.order.report.currentUserId'),
        },
        {
            field: 'areauserid',
            label: t('query.order.report.areauserid'),
        },
    ],
    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()
    const [startDate, endDate] = dateValue.value || []
    qs.startDate = startDate
    qs.endDate = endDate
    return qs
}

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

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