<!-- 查询管理-内部订单查询-交收成交查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #ishis="{ item }">
                    <el-form-item :label="item.label" prop="ishis">
                        <el-select v-model="item.value">
                            <el-option v-for="option in item.options?.()" :key="option.value" :label="option.label"
                                :value="option.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="日期" prop="date" v-if="item.value === 1">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            start-placeholder="开始时间" end-placeholder="结束时间" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_deliverytrade_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_deliverytrade_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, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { getBuyOrSellList, getBuyOrSellName } from '@/constants/order'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { querySettlementDeal } from '@/services/api/order'
import { i18n } from '@/stores'
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'

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

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'deliveryid', label: '交收单号' },
    { field: 'tradedate', label: '交易日' },
    { field: 'buyorsell', label: '方向', formatValue: (val) => getBuyOrSellName(val) },
    { field: 'accountid', label: '买方' },
    { field: 'matchaccountid', label: '卖方' },
    { field: 'deliverygoodsname', label: '现货商品' },
    { field: 'deliveryqty', label: '现货数量' },
    { field: 'deliveryprice', label: '交收单价' },
    { field: 'deliveryamount', label: '货款总金额' },
    { field: 'deliverypricemove', label: '升贴水总金额' },
    { field: 'deliverycharge', label: '交收服务费' },
    { field: 'deliveryorderid', label: '委托单号' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.SettlementDealReq>({
    filters: [
        {
            field: 'deliveryid',
            label: '交收单号',
        },
        {
            field: 'wrstandardid',
            label: '现货商品',
            placeholder: '请输入商品ID'
        },
        {
            field: 'xgoodsid',
            label: '主商品',
            placeholder: '请输入商品ID'
        },
        {
            field: 'deliveryorderid',
            label: '委托单号',
        },
        {
            field: 'accountid',
            label: '资金账户',
            placeholder: '请输入资金账户ID'
        },
        {
            field: 'tradedate',
            label: '交易日',
        },
        {
            field: 'buyorsell',
            label: '方向',
            options: () => getBuyOrSellList()
        },
        {
            field: 'ishis',
            label: '交收数据类型',
            value: 0,
            options: () => [
                { label: '当前', value: 0 },
                { label: '历史', value: 1 }
            ]
        },
    ],
    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>