<template>
  <ATablePage
    ref="tablePage"
    v-bind="tablePageProps">
    <template #simpleFormItem="formData">
      <ElFormItem>
        <ElInput v-model="formData.keywords" placeholder="分类名称/编码"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="图片" prop="picture">
        <template #default="{ row }">
          <el-image :preview-src-list="[AppApi.fileUrl(row.picture)]" :src="AppApi.fileUrl(row.picture)" preview-teleported show-progress style="width: 32px; height: 32px"/>
        </template>
      </ElTableColumn>
      <ElTableColumn label="业务类型" prop="bizTypeTxt"/>
      <ElTableColumn label="编码" prop="sn"/>
      <ElTableColumn label="分类名称" prop="categoryName"/>
      <ElTableColumn label="备注" prop="memo" show-overflow-tooltip width="180"/>
      <ElTableColumn label="创建时间" prop="createTime" width="180"/>
    </template>
    <GoodsCategoryForm ref="goodsCategoryForm" :default-biz-type="defaultBizType" :research="research"/>
  </ATablePage>
</template>

<script lang="ts" setup>
import GoodsCategoryApi from '@/pages/gds/goods-category/goods-category-api.ts'
import GoodsCategoryForm from '@/pages/gds/goods-category/GoodsCategoryForm.vue'
import AppApi from '@/common/app/app-api.ts'
import { bizType } from '@/pages/gds/goods-category/constants.ts'
import ATablePage, {
  type ATablePageInstance,
  buildTablePageProps,
} from '@/components/a-page/a-table-page/ATablePage.tsx'

const props = defineProps<{
  defaultBizType: typeof bizType[number]['val']
}>()

const goodsCategoryFormIns = useTemplateRef<InstanceType<typeof GoodsCategoryForm>>('goodsCategoryForm')
const tablePageIns = useTemplateRef<ATablePageInstance>('tablePage')

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

const tablePageProps = buildTablePageProps<GoodsCategoryTypes.SearchGoodsCategoryParam, GoodsCategoryTypes.SearchGoodsCategoryResult>({
  pageLayout: {
    enableHighForm: false,
  },
  searchForm: {
    defaultData: {bizType: props.defaultBizType},
    highForm: {
      contentWidth: 320,
    },
    paging(param) {
      return GoodsCategoryApi.paging({
        bizType: param.bizType,
        keywords: param.keywords,
      })
    },
  },
  toolBar: {
    leftTools: [
      {
        icon: 'Plus',
        label: '新建',
        action() {
          goodsCategoryFormIns.value?.open()
        },
      },
    ],
  },
  table: {
    actionColumn: {
      tableActions: [
        {
          tooltip: '编辑',
          icon: 'Edit',
          action({row}) {
            goodsCategoryFormIns.value?.open(row)
          },
        },
        {
          icon: 'Delete',
          loading: false,
          type: 'danger',
          tooltip: '删除',
          confirm: {
            title: '是否删除当前数据',
          },
          action({row}) {
            return GoodsCategoryApi.del([ row.id! ])
              .then(() => {
                ElMessage.success('删除成功')
                return true
              })
          },
        },
      ],
    },
  },
})
</script>
