import { useRef, useState, useEffect } from 'react';
import { connect, history } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProTable, TableDropdown, ProFormSelect
} from '@ant-design/pro-components';
import { Button, Image, ConfigProvider, Popconfirm, message, Tag } from 'antd';
import { GetTalentsinfoList, PostTalentsinfoDel } from '@/apis/api';
import { Imageprefix } from '@/constants';
const selectfieldNames = { label: 'name', value: 'id', children: 'childs' };

const PagesMainInformationArticleTable: 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'
                    bordered={true}
                    scroll={{ x: 1300 }}
                    actionRef={actionRef}
                    dataSource={list}
                    columns={[{
                        title: 'ID',
                        dataIndex: 'id',
                        width: 100,
                        search: false,
                    },
                    {
                        title: '户口编号',
                        dataIndex: 'title',
                        search: false,
                    },
                    {
                        title: '姓名',
                        dataIndex: 'name',
                    },
                    {
                        title: '性别',
                        dataIndex: 'cover_img',
                        search: false,

                    },
                    {
                        title: '是否随迁',
                        dataIndex: 'stick_top',
                        search: false,

                    },
                    {
                        title: '操作',
                        width: 300,
                        key: 'option',
                        valueType: 'option',
                        fixed: 'right',
                        render: (_, record, action) => [
                            <Button key='1' type='link' onClick={() => {
                                setId(record.id)
                                dispatch({ type: 'openModel/getOpenModal', payload: true })
                            }}>编辑</Button>,
                            <Button key='1' type='link' onClick={() => {

                            }}>迁入</Button>,
                            <Button key='1' type='link' onClick={() => {

                            }}>迁出</Button>,
                            <Popconfirm
                                title="是否删除"
                                onConfirm={(e) => {
                                    PostTalentsinfoDel({ id: record.id }).then(res => {
                                        message.success('删除成功')
                                        actionRef.current?.reload();
                                    })
                                }}
                                okText="删除"
                                cancelText="取消"
                            >
                                <Button danger type='link'>删除</Button>

                            </Popconfirm>
                        ],
                    },
                    ]}
                    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>) =>
                        GetTalentsinfoList({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.name,
                        }).then(res => {
                            setList(res.data.talentsInfos)
                            setTotal(res.data.total)
                        })
                    }
                    headerTitle="公共人口信息"
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            dispatch({ type: 'openModel/getOpenModal', payload: true })
                        }}>
                            公共人口信息
                        </Button>
                    ]}
                />
            </ConfigProvider>
        </>
    );
};


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