<!-- 查询管理-内部订单查询-账户成交汇总 -->
<template>
    <app-view>
        <template #header>
            <el-form ref="formRef" class="el-form--filter" :model="queryParams" :rules="formRules"
                :show-message="false">
                <el-form-item :label="t('query.internal.accountsumm.marketid')" prop="marketid">
                    <el-select v-model="queryParams.marketid">
                        <template v-for="item in getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true)"
                            :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.accountsumm.summode')" prop="summode">
                    <el-select v-model="queryParams.summode">
                        <el-option :label="t('query.internal.accountsumm.summode1')" :value="1" />
                        <el-option :label="t('query.internal.accountsumm.summode2')" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.accountsumm.date')" prop="date">
                    <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD" :start-placeholder="t('common.start')"
                        :end-placeholder="t('common.end')" @change="onDateChange" />
                </el-form-item>
                <el-form-item :label="t('query.internal.accountsumm.goodsid')" prop="goodsid">
                    <el-input type="number" v-model="queryParams.goodsid" :placeholder="t('query.internal.accountsumm.tips1')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.accountsumm.accountid')" prop="accountid">
                    <el-input type="number" v-model="queryParams.accountid" :placeholder="t('query.internal.accountsumm.tips2')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.accountsumm.excludeaccountidsstr')" prop="excludeaccountidsstr">
                    <el-input v-model="queryParams.excludeaccountidsstr" :placeholder="t('query.internal.accountsumm.tips3')"
                        style="width: 260px;" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch(false)">{{ t('operation.search') }}</el-button>
                    <el-button type="primary" @click="onSearch(true)">{{ t('operation.reset') }}</el-button>
                </el-form-item>
            </el-form>
        </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 { FormInstance, FormRules } from 'element-plus'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
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 { i18n } from '@/stores'

const { getMarketOptions } = useMarket()

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

const queryParams = ref<Partial<Model.AccountTransactionsReq>>({
    summode: 1
})

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,
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradeDate', label: 'query.internal.accountsumm.tradeDate' },
    { field: 'userfullname', label: 'query.internal.accountsumm.userfullname' },
    { field: 'goodsfullname', 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 formRules: FormRules = {
    marketid: [{ required: true }],
    //goodsid: [{ required: true }],
    date: [{
        required: true,
        validator: (rule, value, callback) => {
            if (dateValue.value && dateValue.value.length === 2) {
                callback()
            } else {
                callback(new Error(t('common.pleasechoicedate')))
            }
        },
    }],
}

// 选择日期
const onDateChange = () => {
    const [startDate, endDate] = dateValue.value || []
    queryParams.value.startdate = startDate
    queryParams.value.enddate = endDate
}

const onSearch = (clear = false) => {
    if (clear) {
        queryParams.value = {
            summode: 1,
        }
        dateValue.value = []
    } else {
        formRef.value?.validate((valid) => {
            if (valid) {
                run(queryParams.value)
            }
        })
    }
}

const openComponentOnClick = (code: string) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            openComponent(code)
        }
    })
}
</script>