import TreeNodeSelect from '@/pages/Project/Management/List/components/TreeNodeSelect'
import { LinkRoleAccountItem } from '@/services/permission'
import { PlusOutlined } from '@ant-design/icons'
import ProForm, { ModalForm } from '@ant-design/pro-form'
import { Button, message } from 'antd'
import React from 'react'
import { useRequest } from 'umi'

const ConnectModal = ({ dataId, onReload }) => {
  const { run: tryConnectRoleAccount } = useRequest(
    (params: API.LinkAccountItem) => LinkRoleAccountItem(params),
    {
      manual: true,
      onSuccess: async () => {
        await onReload()
      }
    }
  )
  return (
    <ModalForm
      title="添加用户"
      width="500px"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 16 }}
      layout="horizontal"
      trigger={
        <Button size="small" type="primary" ghost>
          <PlusOutlined />
          添加用户
        </Button>
      }
      onFinish={async values => {
        await tryConnectRoleAccount({ ...values, ID: dataId })
        message.success('添加成功')
        return true
      }}>
      <ProForm.Item
        label="选择用户"
        name="accountID"
        rules={[{ required: true, message: '请选择创建人' }]}>
        <TreeNodeSelect />
      </ProForm.Item>
    </ModalForm>
  )
}

export default ConnectModal
