import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  MyTreeCheckable,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { message } from 'antd';

export default function EditPermissions(props: MyBetaModalFormProps) {
  return (
    <BetaSchemaForm<ApiTypes.SysRoles.SetPermissions>
      {...MyModalFormProps.props}
      title={props.title}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Edit title={props.title} />}
      request={() =>
        Apis.SysRoles.GetPermissions({ id: props?.item?.id ?? 0 }).then(
          (res) => {
            return {
              id: props?.item?.id,
              permissions_ids: res.data?.permissions_ids,
            };
          },
        )
      }
      onFinish={async (values) => {
        return Apis.SysRoles.SetPermissions({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          key: 'permissions_ids',
          title: '选择权限',
          ...rulesHelper.array,
          renderFormItem: () => (
            <MyTreeCheckable api={Apis.SysPermissions.Tree} />
          ),
        },
      ]}
    />
  );
}
