<template>
  <ATablePage
    ref="tablePage"
    v-bind="tablePageProps">
    <template #simpleFormItem="formData">
      <ElFormItem>
        <ElInput
          v-model="formData.endpointPath"
          placeholder="端点地址"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="请求方式" prop="requestMethod"/>
      <ElTableColumn label="路由前缀" prop="routingPath"/>
      <ElTableColumn label="端点地址" prop="endpointPath"/>
      <ElTableColumn label="访问模式" prop="accessModelTxt"/>
      <ElTableColumn label="备注" prop="memo"/>
    </template>
    <EndpointForm ref="endpointForm" :research="research"/>
  </ATablePage>
</template>

<script lang="ts" setup>
import EndpointApi from '@/pages/sys/endpoint/endpoint-api.ts'
import EndpointForm from '@/pages/sys/endpoint/EndpointForm.vue'
import ATablePage, {
  type ATablePageInstance,
  buildTablePageProps,
} from '@/components/a-page/a-table-page/ATablePage.tsx'

const tablePageIns = useTemplateRef<ATablePageInstance>('tablePage')
const endpointFormIns = useTemplateRef<InstanceType<typeof EndpointForm>>('endpointForm')

const tablePageProps = buildTablePageProps<EndpointTypes.SearchEndpointParam, EndpointTypes.SearchEndpointResult>({
  pageLayout: {
    enableHighForm: false,
  },
  searchForm: {
    paging: EndpointApi.paging,
  },
  toolBar: {
    leftTools: [
      {
        icon: 'Plus',
        label: '新建',
        action() {
          endpointFormIns.value?.open()
        },
      },
    ],
  },
  table: {
    actionColumn: {
      tableActions: [
        {
          tooltip: '编辑',
          icon: 'Edit',
          type: 'success',
          action({row}) {
            endpointFormIns.value?.open(row)
          },
        },
        {
          icon: 'Delete',
          loading: false,
          type: 'danger',
          tooltip: '删除',
          confirm: {
            title: '是否删除当前数据',
          },
          action({row}) {
            return EndpointApi.del([ row.id! ])
              .then(() => {
                ElMessage.success('删除成功')
                return true
              })
          },
        },
      ],
    },
  },
})

function research() {
  tablePageIns.value?.doSearch()
}
</script>

<style lang="stylus" scoped>
</style>
