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, Tag } from 'antd';
import { GetDictList } from '@/apis/api';
import { Imageprefix } from '@/constants';

const PagesSettingDictionaryTable: 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 setId = (id: number) => {
        getId(id)
    }

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

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                        colorLink: '#4FBE70',
                    }
                }}
            >
                <ProTable
                    size='small'
                    search={false}
                    scroll={{ y: 480, x: 'auto' }}
                    bordered={true}
                    actionRef={actionRef}
                    dataSource={list}
                    columns={[
                        {
                            title: 'ID',
                            dataIndex: 'id'
                        },
                        {
                            title: '字典名',
                            dataIndex: 'name'
                        },
                        {
                            title: '字典标识名',
                            dataIndex: 'code'
                        },
                        {
                            title: '字典代码值',
                            dataIndex: 'value'
                        },
                        {
                            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='1' type='link' onClick={() => {
                                    record.nextlevel = record.id // 添加下级字典，把id赋值给nextlevel
                                    setId(record)
                                    dispatch({ type: 'openModel/getOpenModal', payload: true })
                                }}>添加下级字典</Button>,
                                <Button key='2' type='link' onClick={() => {

                                }}>删除</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>) =>
                        GetDictList({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.name,
                            parent_id: -1,
                            show_childs: true
                        }).then(res => {
                            setList(res.data.dicts)
                            setTotal(res.data.total)
                        })
                    }
                    headerTitle="字典列表"
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            dispatch({ type: 'openModel/getOpenModal', payload: true })
                        }}>
                            添加字典
                        </Button>
                    ]}
                    expandable={{
                        childrenColumnName: 'children', // 指定子节点数据的字段名
                    }}
                />
            </ConfigProvider>
        </>
    );
};


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