import { useRef, useState, useEffect } from 'react';
import { connect, history } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProTable
} from '@ant-design/pro-components';
import { Button, ConfigProvider, Tag } from 'antd';
import { listRecruitment, GetRecruitmentDetail } from '@/apis/api';

const MainFairListPage: React.FC = ({ dispatch, getId, openModel }: 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: 80,
                    },
                    {
                        title: '招聘会名称',
                        dataIndex: 'title',
                        width: 400,
                    }, {
                        title: '地点',
                        dataIndex: 'address',
                        width: 200,
                        search: false
                    }, {
                        title: '开始时间',
                        dataIndex: 'open_date',
                        width: 200,
                        search: false
                    }, {
                        title: '开通状态',
                        dataIndex: 'status',
                        width: 100,
                        render: (_, record) => (<>
                            {
                                <>
                                    {record.status === 1 ? <Tag color="green">进行中</Tag> : <Tag color="red">已结束</Tag>}
                                </>
                            }

                        </>),
                        valueType: 'select',
                        valueEnum: {
                            0: { text: '全部' },
                            1: {
                                text: '进行中',
                            },
                            2: {
                                text: '已结束',
                            }
                        },
                    }, {
                        title: '参与的企业总数',
                        dataIndex: 'join_count',
                        width: 120,
                        search: false
                    }, {
                        title: '已审核企业总数',
                        dataIndex: 'audit_count',
                        width: 120,
                        search: false
                    }, {
                        title: '参与职位数',
                        dataIndex: 'job_count',
                        width: 100,
                        search: false
                    }, {
                        title: '需求人数',
                        dataIndex: 'recruiting_num',
                        width: 100,
                        search: false
                    },
                    {
                        title: '操作',
                        width: 120,
                        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={() => {
                                setId(record)
                                dispatch({ type: 'openModel/getOpenFairDetailModal', 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>) =>
                        listRecruitment({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.title,
                            status: params.status ? params.status : 0
                        }).then(res => {
                            setList(res.data.recruitments)
                            setTotal(res.data.total)
                        })
                    }
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            dispatch({ type: 'openModel/getOpenModal', payload: true })
                        }}>
                            添加招聘会
                        </Button>
                    ]}
                    headerTitle="招聘会列表"
                />
            </ConfigProvider>
        </>
    );
};




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