<script setup lang="ts">
import { reactive } from "vue";
import { ElButton } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import DataForm from "@/components/data-form/DataForm.vue";
import type { FormField } from "@/components/data-form/DataFormTypes.ts";
import { AgGridVue } from "ag-grid-vue3";
import { type GridOptions, type IServerSideGetRowsParams } from "ag-grid-enterprise";
import type { GridApi, GridReadyEvent } from "ag-grid-community";
import { localeText as localeTextCn } from "@/components/yvTable/yv-aggrid-cn.locale";
import { Request } from "@ease-forge/shared";

// 定义 Props 类型
interface ComponentProps {
}

// 读取组件 props 属性
const props = withDefaults(defineProps<ComponentProps>(), {});

// 定义 State 类型
interface ComponentState {
    loading: boolean;
    queryData: any;
    grid1Data: Array<any>;
}

// state 属性
const state = reactive<ComponentState>({
    loading: false,
    queryData: {},
    grid1Data: [],
});

// 定义 Data 类型
interface ComponentData {
    formFields?: Array<FormField>;
    gridSetting: Partial<GridOptions>;
    api?: GridApi;
}

// 内部数据
const data: ComponentData = {
    formFields: [
        {
            dataPath: 'lpn', label: '托盘条码', input: 'Input',
            inputProps: {
                placeholder: '托盘条码',
                clearable: true,
            }
        },
    ],
    gridSetting: {
        localeText: localeTextCn,
        // suppressNoRowsOverlay: true,
        // suppressLoadingOverlay: true,
        // 选择行配置
        rowSelection: "single",
        rowModelType: "serverSide",
        columnDefs: [
            { headerName: "环境ID", field: "envId" },
            { headerName: "业务单据ID", field: "bizTaskId" },
            { headerName: "账页号", field: "ledgerId" },
            { headerName: "账页类型", field: "ledgerType" },
            { headerName: "账页原因", field: "ledgerRemark" },
            { headerName: "托盘条码", field: "lpn" },
            { headerName: "库存改变数量", field: "qtyChange" },
            { headerName: "入库占用改变数量", field: "qtyInChange" },
            { headerName: "出库占用改变数量", field: "qtyOutChange" },
            { headerName: "改变后堆叠层号", field: "layerIndex" },
            { headerName: "改变前库存位置", field: "locCode" },
            { headerName: "改变后库存数量", field: "qty" },
            { headerName: "改变后入库占用数量", field: "qtyIn" },
            { headerName: "改变后出库占用数量", field: "qtyOut" },
            { headerName: "创建时间", field: "createAt" },
            { headerName: "创建人", field: "createBy" },
            { headerName: "更新时间", field: "updateAt" },
            { headerName: "更新人", field: "updateBy" },
        ],
        pagination: true,
        paginationPageSize: 50,
        // cacheBlockSize: 50,
        // suppressPaginationPanel
        onGridReady(event: GridReadyEvent) {
            data.api = event.api;
            data.api.setServerSideDatasource({ getRows: serverSideDatasource });
        },
    },
};

function reload() {
    data.api.paginationGoToPage(0);
    data.api.setServerSideDatasource({ getRows: serverSideDatasource });
}

function serverSideDatasource(params: IServerSideGetRowsParams) {
    const { startRow, endRow } = params.request;
    console.log("startRow, endRow ", params.request);
    const pageSize = endRow - startRow;
    const pageNo = Math.floor(startRow / pageSize) + 1;
    state.loading = true;
    Request.request.get<PageData>(
        "/api/workbench/DeviceManager@queryInvLedger",
        {
            params: {
                ...state.queryData,
                pageSize,
                pageNo,
            },
        },
    ).then(page => {
        params.success({ rowData: page.records, rowCount: page.total })
    }).finally(() => state.loading = false);
}
</script>

<template>
    <div class="dashboard flex-column-container">
        <div class="tools flex-item-fixed">
            <ElButton :icon="Search" :loading="state.loading" @click="reload">查询</ElButton>
        </div>
        <DataForm
            class="query-form flex-item-fixed"
            style="width: 500px;"
            :data="state.queryData"
            :formFields="data.formFields"
            :columnCount="2"
            layout="bothFixed"
            labelWidth="85px"
            inputWidth="200px"
        />
        <AgGridVue
            ref="gridRef"
            :class="['ag-theme-alpine', 'yv-table', 'hi-light-selected-row','allow-vertical-line', 'flex-item-fill']"
            v-bind="{...data.gridSetting}"
        >
        </AgGridVue>
    </div>
</template>

<style scoped>
.dashboard {
    height: 100%;
}

.tools {
    margin-bottom: 8px;
}

.query-form {
    height: unset;
}
</style>

