<template>
  <ATablePage
    ref="tablePage"
    v-bind="tablePageProps">
    <template #simpleFormItem="formData">
      <ElFormItem label="仓库名称">
        <ElInput
          v-model="formData.warehouseName"
          placeholder="仓库名称"/>
      </ElFormItem>
      <ElFormItem label="位置">
        <ElInput
          v-model="formData.location"
          placeholder="位置"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="Id" prop="id"/>
      <ElTableColumn label="仓库名称" prop="warehouseName"/>
      <ElTableColumn label="位置" prop="location"/>
      <!-- <ElTableColumn label="创建人 Id； sys_user.id" prop="creatorId"/>
      <ElTableColumn label="修改人 Id；sys_user.id" prop="modifierId"/> -->
      <ElTableColumn label="创建时间" prop="createTime"/>
      <ElTableColumn label="修改时间" prop="modifyTime"/>
      <!-- <ElTableColumn label="是否删除； 0-->未删除、1-->已删除" prop="deleted"/> -->
    </template>
    <WarehouseForm ref="warehouseForm" :research="research"/>
  </ATablePage>
</template>

<script lang="ts" setup>
import WarehouseApi from '@/pages/wh/warehouse/warehouse-api.ts'
import WarehouseForm from '@/pages/wh/warehouse/WarehouseForm.vue'
import ATablePage, {
  type ATablePageInstance,
  buildTablePageProps,
} from '@/components/a-page/a-table-page/ATablePage.tsx'

const warehouseFormIns = useTemplateRef<InstanceType<typeof WarehouseForm>>('warehouseForm')
const tablePageIns = useTemplateRef<ATablePageInstance>('tablePage')
const tablePageProps = buildTablePageProps<WarehouseTypes.SearchWarehouseParam, WarehouseTypes.SearchWarehouseResult>({
  pageLayout: {
    enableHighForm: false,
  },
  searchForm: {
    paging: WarehouseApi.paging,
  },
  toolBar: {
    leftTools: [
      {
        icon: 'Plus',
        label: '新建',
        action() {
          warehouseFormIns.value?.open()
        },
      },
    ],
  },
  table: {
    actionColumn: {
      tableActions: [
        {
          tooltip: '编辑',
          icon: 'Edit',
          type: 'success',
          action({row}) {
            warehouseFormIns.value?.open(row)
          },
        },
        {
          icon: 'Delete',
          loading: false,
          type: 'danger',
          tooltip: '删除',
          confirm: {
            title: '是否删除当前数据',
          },
          action({row}) {
            return WarehouseApi.del([ row.id! ])
              .then(() => {
                ElMessage.success('删除成功')
                return true
              })
          },
        },
      ],
    },
  },
})

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

</script>
