<template>
  <FormPage
    ref="formPage"
    :action-column="actionColumn"
    :left-tools="leftTools"
    :paging="paging">
    <template #searchFormItem="{ searchForm }">
      <ElFormItem label="编号">
        <ElInput v-model="searchForm.sn" placeholder="编号"/>
      </ElFormItem>
      <ElFormItem label="仓库">
        <ElInput v-model="searchForm.warehouseId" placeholder="仓库"/>
      </ElFormItem>
      <ElFormItem label="存放位置">
        <ElInput v-model="searchForm.location" placeholder="存放位置"/>
      </ElFormItem>
      <ElFormItem label="入库时间">
        <ElInput v-model="searchForm.inTime" placeholder="入库时间"/>
      </ElFormItem>
      <ElFormItem label="采购单">
        <ElInput v-model="searchForm.orderId" placeholder="采购单"/>
      </ElFormItem>
      <ElFormItem label="采购单单号">
        <ElInput v-model="searchForm.orderSn" placeholder="采购单单号"/>
      </ElFormItem>
      <ElFormItem label="产品分类 Id">
        <ElInput v-model="searchForm.goodsCategoryId" placeholder="产品分类 Id"/>
      </ElFormItem>
      <ElFormItem label="分类名称">
        <ElInput v-model="searchForm.goodsCategoryName" placeholder="分类名称"/>
      </ElFormItem>
      <ElFormItem label="产品名称">
        <ElInput v-model="searchForm.goodsName" placeholder="产品名称"/>
      </ElFormItem>
    </template>
    <template #simpleSearchFormItem="{ searchForm }">
      <ElFormItem>
        <ElInput v-model="searchForm.sn" placeholder="编号"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput v-model="searchForm.warehouseId" placeholder="仓库"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput v-model="searchForm.location" placeholder="存放位置"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput v-model="searchForm.inTime" placeholder="入库时间"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput v-model="searchForm.goodsName" placeholder="产品名称"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="编号" prop="sn"/>
      <ElTableColumn label="仓库" prop="warehouseName"/>
      <ElTableColumn label="存放位置" prop="location"/>
      <ElTableColumn label="入库时间" prop="inTime" width="170px"/>
      <ElTableColumn label="采购单单号" prop="orderSn" width="170px"/>
      <ElTableColumn label="分类名称" prop="goodsCategoryName"/>
      <ElTableColumn label="产品名称" prop="goodsName"/>
      <ElTableColumn label="产品编码" prop="goodSn" width="100px"/>
      <ElTableColumn label="数量" prop="quantity"/>
      <ElTableColumn label="计量单位" prop="unit"/>
      <ElTableColumn label="备注" prop="memo"/>
      <ElTableColumn label="创建时间" prop="createTime" width="170px"/>
      <ElTableColumn label="修改时间" prop="modifyTime" width="170px"/>
    </template>
    <InOrderForm ref="inOrderForm" :research="research"/>
  </FormPage>
</template>

<script lang="ts" setup>
import InOrderApi from '@/pages/wh/in-order/in-order-api.ts'
import InOrderForm from '@/pages/wh/in-order/InOrderForm.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 inOrderFormIns = useTemplateRef<InstanceType<typeof InOrderForm>>('inOrderForm')
const formPageIns = useTemplateRef<ComponentExposed<typeof FormPage>>('formPage')
const actionColumn = reactive<ActionColumnType<InOrderTypes.SearchInOrderResult>>({
  tableActions: [
    {
      tooltip: '编辑',
      icon: 'Edit',
      type: 'success',
      action({row}) {
        inOrderFormIns.value?.open(row)
      },
    },
    {
      icon: 'Delete',
      loading: false,
      type: 'danger',
      tooltip: '删除',
      confirm: {
        title: '是否删除当前数据',
      },
      action({row}) {
        return InOrderApi.del([ row.id! ])
          .then(() => {
            ElMessage.success('删除成功')
            return true
          })
      },
    },
  ],
})
const leftTools: ToolType[] = [
  {
    icon: 'Plus',
    label: '新建',
    action() {
      inOrderFormIns.value?.open()
    },
  },
]

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

function paging(param: InOrderTypes.SearchInOrderParam) {
  return InOrderApi.paging(param)
}
</script>
