import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProFormText,
    ProForm,
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, Modal, message } from 'antd';
import { PostRoleAdd, PostRoleEdit, GetRoleDetail, PostRoleAddpermission } from '@/apis/api';


const PagesPermissionRoleCreate: React.FC = ({ dispatch, dictModel, openModel, id }: any) => {
    const formRef = useRef<ProFormInstance>();
    const [openPreview, setOpenPreview] = useState(false);
    const [detail, setDetail] = useState<PermissionType.role | null>(null)

    useEffect(() => {
        setOpenPreview(openModel.openModal)
    }, [openModel.openModal])


    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }
                }}
            >
                <Modal
                    open={openPreview}
                    title={id ? '编辑角色' : '添加角色'}
                    centered
                    maskClosable={false}
                    footer={null}
                    destroyOnClose
                    width={600}
                    onCancel={() => {
                        dispatch({ type: 'openModel/getOpenModal', payload: false })
                    }}
                >
                    <ProForm<PermissionType.role>
                        formRef={formRef}
                        request={async () => {
                            if (id) {
                                let res = await GetRoleDetail({ id: id })
                                setDetail(res.data)
                                return res.data;
                            } else {
                                return {
                                    name: '',
                                    code: '',
                                    description: ''
                                }
                            }
                        }}
                        submitter={{
                            searchConfig: {
                                resetText: '取消',
                                submitText: '保存',
                            },
                            render: (_, dom) => { return <Flex justify='flex-end'> <Space>{dom}</Space></Flex> },
                            onReset: () => {
                                setDetail(null)
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            }
                        }}
                        onFinish={async (values) => {
                            console.log(values)
                            if (id) {
                                values.id = id;
                                let res = await PostRoleEdit(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            } else {
                                let res = await PostRoleAdd(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                              
                            }
                        }}
                    >
                        <ProFormText
                            label="角色名"
                            name="name"
                            placeholder="请输入角色名"
                            rules={[{ required: true, message: '请输入角色名' }]}
                        />
                        <ProFormText
                            label="角色别名"
                            name="code"
                            placeholder="请输入路径"
                            rules={[{ required: true, message: '请输入路径' }]}
                        />
                        <ProFormText
                            label="描述"
                            name="description"
                            placeholder="请输入描述"
                            rules={[{ required: true, message: '请输入描述' }]}
                        />
                    </ProForm>
                </Modal>
            </ConfigProvider>
        </>
    );
};



export default connect(({ dictModel, openModel }: any) => ({
    dictModel, openModel
}))(PagesPermissionRoleCreate);