import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { OrganizationsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import NextOrganizationChange from '../../organizations/modals/NextOrganizationChange';
import OrganizationChange from '../../organizations/modals/OrganizationChange';
import OrganizationCreate from '../../organizations/modals/OrganizationCreate';
import OrganizationUpdate from '../../organizations/modals/OrganizationUpdate';

export default function Organizations(props: MyBetaModalFormProps) {
  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, companies_id: props?.item?.id },
            sort,
            Apis.Company.Organizations.TreeList,
          )
        }
        toolBarRender={(action) => [
          <OrganizationCreate
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="组织"
          />,
        ]}
        search={false}
        // options={false}
        columns={[
          {
            title: '组织名称',
            dataIndex: 'name',
          },
          {
            title: '组织ID',
            dataIndex: 'id',
            search: false,
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: OrganizationsTypeEnum,
            search: false,
          }),

          {
            title: '上级组织',
            dataIndex: ['organization_parent', 'name'],
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <OrganizationUpdate
                  item={item}
                  reload={action?.reload}
                  title="组织"
                />
                {item?.type !== OrganizationsTypeEnum.Group.value && (
                  <OrganizationChange
                    item={{ ...item, companies_id: props?.item?.companies_id }}
                    reload={action?.reload}
                    title="组织"
                  />
                )}
                <NextOrganizationChange
                  item={{ ...item, companies_id: item?.companies_id }}
                  reload={action?.reload}
                  title="下级组织"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.Organizations.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
