<template>
  <FormPage
    ref="formPage"
    :action-column="actionColumn"
    :left-tools="leftTools"
    :paging="paging">
    <template #searchFormItem="{ searchForm }">
      <ElFormItem label="编号">
        <ElInput v-model="searchForm.sn" placeholder="编号"/>
      </ElFormItem>

      <!-- ，JinXingZhong-进行中、YiDaoHuo-已到货、YiRuKu-已入库、YiQuXiao-已取消 -->
      <ElFormItem label="状态">
        <ElInput v-model="searchForm.purchaseOrderStatus" placeholder="状态"/>
      </ElFormItem>
      <ElFormItem label="供货客户 Id">
        <ElInput v-model="searchForm.customerId" placeholder="供货客户 Id"/>
      </ElFormItem>
      <ElFormItem label="采购日期">
        <ElInput v-model="searchForm.purchaseDate" placeholder="采购日期"/>
      </ElFormItem>
      <!-- <ElFormItem label="到货日期">
        <ElInput v-model="searchForm.arrivalDate" placeholder="到货日期" />
      </ElFormItem>
      <ElFormItem label="产品分类 Id">
        <ElInput v-model="searchForm.goodsCategoryId" placeholder="产品分类 Id" />
      </ElFormItem>-->
      <ElFormItem label="分类名称">
        <ElInput v-model="searchForm.goodsCategoryName" placeholder="分类名称"/>
      </ElFormItem>
      <!-- <ElFormItem label="产品 Id">
        <ElInput v-model="searchForm.goodsId" placeholder="产品 Id" />
      </ElFormItem> -->
      <ElFormItem label="产品名称">
        <ElInput v-model="searchForm.goodsName" placeholder="产品名称"/>
      </ElFormItem>
      <ElFormItem label="产品编码">
        <ElInput v-model="searchForm.goodSn" placeholder="产品编码"/>
      </ElFormItem>
      <ElFormItem label="数量">
        <ElInput v-model="searchForm.quantity" placeholder="数量"/>
      </ElFormItem>
    </template>
    <template #simpleSearchFormItem="{ searchForm }">
      <ElFormItem label="编号">
        <ElInput v-model="searchForm.sn" placeholder="编号"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="编号" prop="sn"/>
      <ElTableColumn label="状态" prop="purchaseOrderStatus"/>
      <ElTableColumn label="采购日期" prop="purchaseDate"/>
      <ElTableColumn label="到货日期" prop="arrivalDate"/>
      <ElTableColumn label="分类名称" prop="goodsCategoryName"/>
      <ElTableColumn label="产品名称" prop="goodsName"/>
      <ElTableColumn label="产品编码" prop="goodSn"/>
      <ElTableColumn label="数量" prop="quantity"/>
      <ElTableColumn label="计量单位" prop="unit"/>
      <ElTableColumn label="总金额（元）" prop="totalMoney"/>
      <ElTableColumn label="备注" prop="memo"/>
      <ElTableColumn label="创建时间" prop="createTime"/>
      <ElTableColumn label="修改时间" prop="modifyTime"/>
    </template>
    <PurchaseOrderForm ref="purchaseOrderForm" :research="research"/>
  </FormPage>
</template>

<script lang="ts" setup>
import PurchaseOrderApi from '@/pages/wh/purchase-order/purchase-order-api.ts'
import PurchaseOrderForm from '@/pages/wh/purchase-order/PurchaseOrderForm.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 purchaseOrderFormIns = useTemplateRef<InstanceType<typeof PurchaseOrderForm>>('purchaseOrderForm')
const formPageIns = useTemplateRef<ComponentExposed<typeof FormPage>>('formPage')

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

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

function paging(param: PurchaseOrderTypes.SearchPurchaseOrderParam) {
  return PurchaseOrderApi.paging(param)
}
</script>
