<template>
  <FormPage
    ref="formPage"
    :action-column="actionColumn"
    :left-tools="leftTools"
    :paging="paging">
    <template #searchFormItem="{ searchForm }">
      <ElFormItem label="Id">
        <ElInput v-model="searchForm.id" placeholder="Id"/>
      </ElFormItem>
      <ElFormItem label="仓库 Id">
        <ElInput v-model="searchForm.warehouseId" placeholder="仓库 Id"/>
      </ElFormItem>
      <ElFormItem label="产品 Id">
        <ElInput v-model="searchForm.goodsId" placeholder="产品 Id"/>
      </ElFormItem>
      <ElFormItem label="存放位置">
        <ElInput v-model="searchForm.location" placeholder="存放位置"/>
      </ElFormItem>
      <ElFormItem label="当前库存数量">
        <ElInput v-model="searchForm.stockQuantity" placeholder="当前库存数量"/>
      </ElFormItem>
      <ElFormItem label="锁定数量">
        <ElInput v-model="searchForm.lockQuantity" placeholder="锁定数量"/>
      </ElFormItem>
      <ElFormItem label="总量">
        <ElInput v-model="searchForm.totalQuantity" placeholder="总量"/>
      </ElFormItem>
      <ElFormItem label="剩余量">
        <ElInput v-model="searchForm.remainingQuantity" placeholder="剩余量"/>
      </ElFormItem>
      <!-- <ElFormItem label="计量单位">
        <ElInput v-model="searchForm.unit" placeholder="计量单位；字典代码：unit" />
      </ElFormItem> -->
      <!-- <ElFormItem label="创建人 Id； sys_user.id">
        <ElInput v-model="searchForm.creatorId" placeholder="创建人 Id； sys_user.id" />
      </ElFormItem>
      <ElFormItem label="修改人 Id； sys_user.id">
        <ElInput v-model="searchForm.modifierId" placeholder="修改人 Id； sys_user.id" />
      </ElFormItem> -->
      <ElFormItem label="创建时间">
        <ElInput v-model="searchForm.createTime" placeholder="创建时间"/>
      </ElFormItem>
      <ElFormItem label="修改时间">
        <ElInput v-model="searchForm.modifyTime" placeholder="修改时间"/>
      </ElFormItem>
    </template>
    <template #simpleSearchFormItem="{ searchForm }">
      <ElFormItem label="存放位置">
        <ElInput v-model="searchForm.location" placeholder="存放位置"/>
      </ElFormItem>
      <ElFormItem label="当前库存数量">
        <ElInput v-model="searchForm.stockQuantity" placeholder="当前库存数量"/>
      </ElFormItem>
      <ElFormItem label="锁定数量">
        <ElInput v-model="searchForm.lockQuantity" placeholder="锁定数量"/>
      </ElFormItem>
      <ElFormItem label="总量">
        <ElInput v-model="searchForm.totalQuantity" placeholder="总量"/>
      </ElFormItem>
      <ElFormItem label="剩余量">
        <ElInput v-model="searchForm.remainingQuantity" placeholder="剩余量"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="仓库" prop="warehouseName"/>
      <ElTableColumn label="产品" prop="goodsName"/>
      <ElTableColumn label="存放位置" prop="location"/>
      <ElTableColumn label="当前库存数量" prop="stockQuantity" width="120"/>
      <ElTableColumn label="锁定数量" prop="lockQuantity"/>
      <ElTableColumn label="总量" prop="totalQuantity"/>
      <ElTableColumn label="剩余量" prop="remainingQuantity"/>
      <ElTableColumn label="计量单位" prop="unit"/>
      <ElTableColumn label="创建时间" prop="createTime" width="180"/>
      <ElTableColumn label="修改时间" prop="modifyTime" width="180"/>
    </template>
    <InventoryForm ref="inventoryForm" :research="research"/>
  </FormPage>
</template>

<script lang="ts" setup>
import InventoryApi from '@/pages/wh/inventory/inventory-api.ts'
import InventoryForm from '@/pages/wh/inventory/InventoryForm.vue'
import FormPage from '@/components/page/FormPage.vue'
import type {
  ActionColumnType,
  ToolType,
} from '@/components/page/a-page-type.ts'
import type { ComponentExposed } from 'vue-component-type-helpers'

const inventoryFormIns = useTemplateRef<InstanceType<typeof InventoryForm>>('inventoryForm')
const formPageIns = useTemplateRef<ComponentExposed<typeof FormPage>>('formPage')

const actionColumn = reactive<ActionColumnType<InventoryTypes.SearchInventoryResult>>({
  tableActions: [
    {
      tooltip: '编辑',
      icon: 'Edit',
      type: 'success',
      action({row}) {
        inventoryFormIns.value?.open(row)
      },
    },
    {
      icon: 'Delete',
      loading: false,
      type: 'danger',
      tooltip: '删除',
      confirm: {
        title: '是否删除当前数据',
      },
      action({row}) {
        return InventoryApi.del([ row.id! ])
          .then(() => {
            ElMessage.success('删除成功')
            return true
          })
      },
    },
  ],
})
const leftTools: ToolType[] = [
  {
    icon: 'Plus',
    label: '新建',
    action() {
      inventoryFormIns.value?.open()
    },
  },
]

function research() {
  formPageIns.value?.doSearch()
}

function paging(param: InventoryTypes.SearchInventoryParam) {
  return InventoryApi.paging(param)
}
</script>
