<!-- 查询管理-内部订单查询-交收委托查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #ishis="{ item }">
                    <el-form-item :label="item.label" prop="ishis">
                        <el-switch v-model="item.value" :active-value="1" :inactive-value="0" />
                    </el-form-item>
                    <el-form-item :label="t('query.internal.deliveryorder.date')" prop="date" v-if="item.value === 1">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('query.internal.deliveryorder.startdate')" :end-placeholder="t('query.internal.deliveryorder.enddate')" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_deliveryorder_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_deliveryorder_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 { formatDate } from '@/filters'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryDeliveryOrder } 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 deliveryorderstatus1Enum = useEnum('deliveryorderstatus1') // 状态
const dateValue = shallowRef<string[] | null>([])
const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'deliveryticket', label: 'query.internal.deliveryorder.deliveryticket' },
    { field: 'deliveryorderid', label: 'query.internal.deliveryorder.deliveryorderid' },
    { field: 'accountidname', label: 'query.internal.deliveryorder.accountidname' },
    { field: 'xgoodsname', label: 'query.internal.deliveryorder.xgoodsname' },
    { field: 'xdeliveryqty', label: 'query.internal.deliveryorder.xdeliveryqty' },
    { field: 'deliverygoodsname', label: 'query.internal.deliveryorder.deliverygoodsname' },
    { field: 'deliveryqty', label: 'query.internal.deliveryorder.deliveryqty' },
    { field: 'deliveryorderstatus', label: 'query.internal.deliveryorder.deliveryorderstatus', formatValue: (val) => deliveryorderstatus1Enum.getEnumTypeName(val) },
    { field: 'ordertime', label: 'query.internal.deliveryorder.ordertime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.DeliveryOrderReq>({
    filters: [
        {
            field: 'deliveryorderid',
            label: t('query.internal.deliveryorder.deliveryorderid'),
        },
        {
            field: 'accountname',
            label: t('query.internal.deliveryorder.accountidname'),
        },
        {
            field: 'xgoodsid',
            label: t('query.internal.deliveryorder.xgoodsname'),
        },
        {
            field: 'wrstandardid',
            label: t('query.internal.deliveryorder.wrstandardid'),
        },
        {
            field: 'deliveryorderstatus',
            label: t('query.internal.deliveryorder.deliveryorderstatus'),
            options: () => deliveryorderstatus1Enum.getEnumOptions()
        },
        {
            field: 'ishis',
            label: t('query.internal.deliveryorder.ishis'),
            value: 0,
            options: () => [
                { label: t('common.current'), value: 0 },
                { label: t('common.history'), 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>