<script setup lang="ts">
import { useTable, useTableOperate } from '@/hooks/common/table';
import { useAppStore } from '@/store/modules/app';
import { fetchGetInventory } from '@/service/api/taskmg';
import LybSearch from '@/views/inventory/takestock/modules/inventory-search.vue'; // 表格数据初始化
import TableHeaderOperationinplan from '@/components/advanced/table-header-operationinplan.vue';
const appStore = useAppStore();

const {
  columns,
  data,
  columnChecks,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetInventory,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    OrderLineNumber: null, // 订单+行号，作为主键
    PurchaseOrderNumber: null, // 采购订单号
    PurchaseOrderLineItem: null, // 采购订单行项目号
    PurchaseRequestNumber: null, // 采购申请号（可选）
    StockUnit: null, // 库存单位
    MaterialType: null, // 物资类型
    ProjectName: null, // 项目名称
    MaterialCode: null, // 物料编码
    MaterialName: null, // 物资名称
    OrderQuantity: null, // 订单数量
    MaterialUnit: null, // 物料单位
    SupplierName: null, // 供应商名称
    PlannedDeliveryDate: null, // 采购订单计划交货日期
    ERPSystemFactory: null, // ERP系统工厂
    ProjectCode: null, // 项目编码
    ContractUnitPrice: null, // 签约单价(不含税)
    ContractTotalPrice: null, // 签约总价(不含税)
    ProjectType: null, // 项目类型
    ProcurementMethod: null, // 采购方式
    MajorCategory: null, // 大类
    MiddleCategory: null, // 中类
    SubCategory: null, // 小类
    ExtraField1: null, // 扩展字段1，格式 "yyyy-MM-dd HH:mm:ss"
    ExtraField2: null, // 扩展字段2
    CreateTimeEnd: null, // 扩展字段2
    CreateTimeStart: null, // 扩展字段2
    RFID: null, // 扩展字段2
    PalletNo: null, // 扩展字段2
    SearchType: 0, // 扩展字段2
    CreateUser: null, // 扩展字段2
    CellNo: null // 扩展字段2
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'OrderQuantity',
      title: '库存数量',
      align: 'center',
      width: 65
    },
    {
      key: 'OrderLineNumber',
      title: '订单+行号',
      align: 'center',
      width: 100
    },
    {
      key: 'PurchaseOrderNumber',
      title: '采购订单号',
      align: 'center',
      width: 100
    },
    {
      key: 'PurchaseOrderLineItem',
      title: '采购订单行项目号',
      align: 'center',
      width: 100
    },
    {
      key: 'PurchaseRequestNumber',
      title: '采购申请号',
      align: 'center',
      width: 100
    },
    {
      key: 'StockUnit',
      title: '库存单位',
      align: 'center',
      width: 100
    },
    {
      key: 'MaterialType',
      title: '物资类型',
      align: 'center',
      width: 100
    },
    {
      key: 'ProjectName',
      title: '项目名称',
      align: 'center',
      width: 230
    },
    {
      key: 'MaterialCode',
      title: '物料编码',
      align: 'center',
      width: 100
    },
    {
      key: 'MaterialName',
      title: '物资名称',
      align: 'center',
      width: 200
    },
    {
      key: 'MaterialUnit',
      title: '物料单位',
      align: 'center',
      width: 100
    },
    {
      key: 'SupplierName',
      title: '供应商名称',
      align: 'center',
      width: 150
    },
    {
      key: 'PlannedDeliveryDate',
      title: '计划交货日期',
      align: 'center',
      width: 100
    },
    {
      key: 'ERPSystemFactory',
      title: 'ERP系统工厂',
      align: 'center',
      width: 100
    },
    {
      key: 'ProjectCode',
      title: '项目编码',
      align: 'center',
      width: 100
    },
    {
      key: 'ContractUnitPrice',
      title: '签约单价(不含税)',
      align: 'center',
      width: 100
    },
    {
      key: 'ContractTotalPrice',
      title: '签约总价(不含税)',
      align: 'center',
      width: 100
    },
    {
      key: 'ProjectType',
      title: '项目类型',
      align: 'center',
      width: 100
    },
    {
      key: 'ProcurementMethod',
      title: '采购方式',
      align: 'center',
      width: 100
    },
    {
      key: 'MajorCategory',
      title: '大类',
      align: 'center',
      width: 100
    },
    {
      key: 'MiddleCategory',
      title: '中类',
      align: 'center',
      width: 100
    },
    {
      key: 'SubCategory',
      title: '小类',
      align: 'center',
      width: 100
    },
    {
      key: 'RFID',
      title: 'RFID编码',
      align: 'center',
      width: 100
    },
    {
      key: 'PalletNo',
      title: '托盘编码',
      align: 'center',
      width: 100
    },
    {
      key: 'CellNo',
      title: '仓位号',
      align: 'center',
      width: 100
    },
    {
      key: 'CreateTime',
      title: '入库时间',
      align: 'center',
      width: 150
    },
    {
      key: 'CreateUser',
      title: '创建人',
      align: 'center',
      width: 100
    }
  ]
});
const { checkedRowKeys } = useTableOperate(data, getData);
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <LybSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <NCard title="库存信息" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperationinplan
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          @refresh="getData"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="3500"
        :loading="loading"
        remote
        :row-key="row => row.OrderLineNumber"
        :pagination="mobilePagination"
        class="amin sm:h-full"
      />
    </NCard>
  </div>
</template>

<style scoped></style>
