
import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProFormText,
    ProFormSegmented,
    ProFormSelect,
    ProForm,
    FormControlRender,
    ProFormRadio,
    ProFormSwitch
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, Modal, message, Image, Row, Col, Switch, Form, Typography } from 'antd';
import { addPermission, updatePermission, getPermissionListWithchilds, GetDetailPermission } from '@/apis/api';


const selectfieldNames = { label: 'name', value: 'id' };


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

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


    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }, components: {
                        Segmented: {
                            itemSelectedBg: '#19be6e',
                            itemSelectedColor: '#ffffff'
                        },
                    },
                }}
            >
                <Modal
                    open={openPreview}
                    title={id ? '编辑权限/菜单' : '添加权限/菜单'}
                    centered
                    maskClosable={false}
                    footer={null}
                    destroyOnClose
                    width={600}
                    onCancel={() => {
                        dispatch({ type: 'openModel/getOpenModal', payload: false })
                    }}
                >
                    <ProForm<PermissionType.list>
                        formRef={formRef}
                        request={async () => {
                            if (id) {
                                if (id.nextlevel) {
                                    setDetail(null)
                                    return {
                                        name: '',
                                        path: '',
                                        action: '',
                                        module: '',
                                        p_type: 2,
                                        parent_id: id.nextlevel,
                                        description: ''
                                    };
                                } else {
                                    let res = await GetDetailPermission({ id: id.id })
                                    setDetail(res.data)
                                    return res.data;
                                }
                            } else {
                                setDetail(null)
                                return {
                                    name: '',
                                    path: '',
                                    action: '',
                                    module: '',
                                    p_type: 2,
                                    parent_id: 0,
                                    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) => {
                            if (id.nextlevel) {
                                values.parent_id = id.nextlevel;
                            } else if (detail && detail.id) {
                                values.parent_id = detail.parent_id;
                            } else {
                                values.parent_id = -1;
                            }
                            console.log(values)
                            if (detail) {
                                values.id = id.id;
                                let res = await updatePermission(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            } else {
                                let res = await addPermission(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            }
                        }}
                    >
                        <Row gutter={16}>
                            <Col span={24}>
                                <ProFormText
                                    label="权限/菜单名称"
                                    name="name"
                                    placeholder="请输入权限/菜单名称"
                                    rules={[{ required: true, message: '请输入权限/菜单名称' }]}
                                />
                            </Col>
                            <Col span={24}>

                                <ProFormText
                                    label="路径"
                                    name="path"
                                    placeholder="请输入路径"
                                    rules={[{ required: true, message: '请输入路径' }]}
                                />
                            </Col>
                            <Col span={24}>
                                <ProFormText
                                    label="标识名称"
                                    name="action"
                                    placeholder="请输入标识名称"
                                    rules={[{ required: true, message: '请输入标识名称' }]}
                                />
                            </Col>
                            <Col span={24}>
                                <ProFormText
                                    label="指向页面"
                                    name="module"
                                    placeholder="请输入指向页面"
                                    rules={[{ required: true, message: '请输入指向页面' }]}
                                />
                            </Col>
                            <Col span={24}>
                                <ProFormRadio.Group
                                    name="p_type"
                                    label="权限类型"
                                    request={async () => [
                                        { label: '权限', value: 1 },
                                        { label: '菜单', value: 2 }
                                    ]}
                                    rules={[{ required: true, message: '请选择权限类型' }]}
                                />
                            </Col>

                            <Col span={24}>
                                <ProFormText
                                    name="description"
                                    label="描述"
                                    placeholder="请输入描述"
                                />
                            </Col>
                        </Row>

                    </ProForm>
                </Modal>
            </ConfigProvider>
        </>
    );
};



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