<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicTable @register="registerTable" :searchInfo="searchInfo"/>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, reactive, onMounted } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './role.data';

  import { loadByRole } from '@/api/demo/system';
  import { BasicTable, useTable, TableAction } from '@/components/Table';

  defineOptions({ name: 'DeptModal' });

  const emit = defineEmits(['success', 'register']);

  const rowId = ref('');
  const searchInfo = reactive < Recordable > ({});

  const [registerModal, { setModalProps, closeModal, }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false });
    rowId.value = data.record.id;
    searchInfo.roleId = rowId.value

  });
  const [registerTable, { reload, getPaginationRef, }] = useTable({
    api: loadByRole,
    columns: [
      {
        title: '用户名',
        dataIndex: 'account',
        width: 120,
      },
      {
        title: '昵称',
        dataIndex: 'name',
        width: 120,
      },
    ],
    useSearchForm: false,
    showTableSetting: false,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    isCanResizeParent: true,
    rowKey: 'id',
    handleSearchInfoFn(info) {
      return info;
    },
  });
 
  onMounted(() => {
  });
  async function handleSubmit() {
    try {
      const values = await validate();
      let query = values
      // 调用接口
      if (!unref(isUpdate)) {
        const data = await addRole(query);
      } else {
        const data = await updateRole(query);
      }


      setModalProps({ confirmLoading: true });
      // TODO custom api
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>