<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    title="功能名称数据"
    @ok="handleSubmit"
    :width="700"
  >
    <BasicTable @register="registerTable" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, BasicColumn, FormSchema } from '/@/components/Table';
  import { getTableRelationList } from '/@/api/system/functionAuthority';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '关键字',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入关键字',
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      title: '功能名称',
      dataIndex: 'menuName',
      align: 'center',
      resizable: true,
    },
    {
      title: '表名',
      dataIndex: 'tableName',
      align: 'center',
      resizable: true,
    },
  ];

  const { t } = useI18n();
  const { notification } = useMessage();
  const emits = defineEmits(['success']);

  const [registerModal, { closeModal }] = useModalInner(async (data) => {
    setSelectedRowKeys(data.selectedName ? [data.selectedName] : []);
  });
  const [registerTable, { getSelectRows, setSelectedRowKeys }] = useTable({
    api: getTableRelationList,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    showTableSetting: true,
    tableSetting: {
      size: false,
    },
    useSearchForm: true,
    rowKey: 'tableName',
    columns,
    striped: false,
    rowSelection: {
      type: 'radio',
    },
  });

  const handleSubmit = () => {
    if (!getSelectRows().length) {
      notification.error({
        message: t('请选择功能名称'),
      });
      return;
    }
    emits('success', getSelectRows()[0]);
    closeModal();
  };
</script>
