import { useRef, useState, useEffect } from 'react';
import { connect, history } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProTable, TableDropdown
} from '@ant-design/pro-components';
import { Button, Image, ConfigProvider, Popconfirm, Badge, Space, Modal } from 'antd';
import { getPermissionListWithchilds } from '@/apis/api';
import { Imageprefix } from '@/constants';

const PagesPermissionListTable: React.FC = ({ dispatch, openModel, getId }: any) => {
    const actionRef = useRef<ActionType>();
    const [list, setList] = useState<object[]>([])
    const [total, setTotal] = useState<number>(0)
    const [page, setPage] = useState<number>(1)
    const [pageSize, setPageSize] = useState<number>(10)
    const [ids, setIds] = useState<number[]>([])

    const setId = (id: number) => {
        getId(id)
    }


    useEffect(() => {
        if (!openModel.openParentModal) {
            actionRef.current?.reload();
        }
    }, [openModel.openParentModal])

    useEffect(() => {
        if (!openModel.openModal) {
            actionRef.current?.reload();
        }
    }, [openModel.openModal])

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                        colorLink: '#4FBE70',
                    }
                }}
            >
                <ProTable
                    scroll={{ y: 480, x: 'auto' }}
                    bordered={true}
                    actionRef={actionRef}
                    dataSource={list}
                    search={false}
                  
                    columns={[
                        {
                            title: 'ID',
                            dataIndex: 'id',
                            width: 200,
                            search: false,
                        },

                        {
                            title: '权限/菜单',
                            dataIndex: 'name',
                            width: 200,
                            search: false,
                        }, {
                            title: '类型',
                            dataIndex: 'p_type',
                            search: false,
                            width: 200,
                            valueEnum: {
                                1: {
                                    text: '权限',
                                    status: 'Error'
                                },
                                2: {
                                    text: '菜单',
                                    status: 'Processing'
                                },
                            },
                        }, {
                            title: '路径',
                            dataIndex: 'path',
                            search: false,
                            width: 200,

                        }, {
                            title: '标识名称',
                            dataIndex: 'action',
                            search: false,
                            width: 200,
                        }, {
                            title: '指向页面',
                            dataIndex: 'module',
                            search: false,
                            width: 200,
                        },
                        {
                            title: '上级菜单id',
                            dataIndex: 'parent_id',
                            search: false,
                            width: 200,
                            render: (_, record) => (<>
                                {record.parent_id == -1 ? <Badge status="processing" text='顶级菜单' /> : <Badge status="error" text={record.parent_id} />}
                            </>
                            )
                        }, {
                            title: '描述',
                            dataIndex: 'description',
                            search: false,
                            width: 200,
                        },
                        {
                            title: '操作',
                            key: 'option',
                            valueType: 'option',
                            render: (_, record, action) => [
                                <Button key='1' type='link' onClick={() => {
                                    setId(record)
                                    dispatch({ type: 'openModel/getOpenModal', payload: true })
                                }}>编辑</Button>,
                                <Button key='2' type='link' onClick={() => {
                                    record.nextlevel = record.id // 添加下级栏目，把id赋值给nextlevel
                                    setId(record)
                                    dispatch({ type: 'openModel/getOpenModal', payload: true })
                                }}>添加下级栏目</Button>,
                                <Button key='3' type='link' onClick={() => {
                                    setId(record)
                                    dispatch({ type: 'openModel/getOpenParentModal', payload: true })
                                }}>更换父级</Button>
                            ],
                        },
                    ]}
                    rowKey="id"
                    pagination={{
                        current: page,
                        pageSize: pageSize,
                        showSizeChanger: true,
                        total: total,
                        pageSizeOptions: [9, 18, 27, 99],
                        onChange(page, pageSize) {
                            setPage(page)
                            setPageSize(pageSize)
                        },
                        onShowSizeChange(current, size) {
                            setPage(current)
                            setPageSize(size)
                        }
                    }}
                    request={async (params = {} as Record<string, any>) =>
                        getPermissionListWithchilds({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.name,

                        }).then(res => {
                            setList(res.data.permissions)
                            setTotal(res.data.total)
                        })
                    }
                    headerTitle="权限列表"
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            dispatch({ type: 'openModel/getOpenModal', payload: true })
                        }}>
                            添加权限
                        </Button>
                    ]}
                    expandable={{
                        childrenColumnName: 'childs', // 指定子节点数据的字段名
                    }}
                />
            </ConfigProvider>
        </>
    );
};


export default connect(({ openModel }: any) => ({
    openModel
}))(PagesPermissionListTable);
