import { useEffect, useRef, useState } from 'react';
import { connect } from '@umijs/max';
import type { ActionType } from '@ant-design/pro-components';
import {
    ProTable, ProFormSelect
} from '@ant-design/pro-components';
import { Button, ConfigProvider, } from 'antd';
import { GetCompanyJobList, getCompanyList, GetCompanyInfo, PostJobExport } from '@/apis/api';
import { Imageprefix } from '@/constants';

const selectfieldNames = { label: 'full_name', value: 'id' };
const PagesMainCompanyPostTable: 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 [companyId, setCompanyId] = useState<number>(0);
    const setId = (id: number) => {
        getId(id)
    }

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

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                        colorLink: '#4FBE70',

                    }
                }}
            >

                <ProTable
                    scroll={{ x: 1300 }}
                    actionRef={actionRef}
                    dataSource={list}
                    columns={[
                        {
                            title: 'ID',
                            dataIndex: 'id',
                            width: 100,
                            fixed: 'left',
                        },
                        {
                            title: '职位名称',
                            dataIndex: 'name',
                            width: 200,
                            fixed: 'left'
                        },
                        {
                            title: '所属企业',
                            dataIndex: 'full_name',
                            width: 300,
                            valueType: 'select',
                            renderFormItem: () => {
                                return (
                                    <ProFormSelect
                                        fieldProps={{
                                            fieldNames: selectfieldNames,
                                            defaultValue: sessionStorage.getItem('post_company_info') ? JSON.parse(sessionStorage.getItem('post_company_info')).full_name : null,
                                        }}
                                        request={async (keyword) =>
                                            getCompanyList({ page: 1, pagesize: 100, keyword: keyword.keyWords }).then(res => {
                                                return res.data.list;
                                            })
                                        }
                                        placeholder="请选择所属企业"
                                        onChange={(value) => {
                                            setCompanyId(value)
                                            GetCompanyInfo({ id: value }).then(res => {
                                                sessionStorage.setItem('post_company_info', JSON.stringify(res.data))
                                            })
                                        }}
                                    />
                                )
                            }
                        },
                        {
                            title: '点击次数',
                            dataIndex: 'click_count',
                            search: false,
                            width: 100
                        },
                        {
                            title: '职位状态',
                            dataIndex: 'status',
                            width: 100,
                            hidden: true,
                            valueType: 'select',
                            valueEnum: {
                                0: { text: '全部' },
                                1: {
                                    text: '发布中',
                                },
                                2: {
                                    text: '未发布',
                                },
                                3: {
                                    text: '暂停',
                                },
                                4: {
                                    text: '已锁定',
                                },
                                5: {
                                    text: '回收站',
                                },
                                6: {
                                    text: '已过期',
                                }
                            },
                        },
                        {
                            title: '状态',
                            dataIndex: 'status_text',
                            search: false,
                            width: 100
                        },
                        {
                            title: '有效期（天）',
                            dataIndex: 'useful_life',
                            search: false,
                            width: 150
                        },
                        {
                            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>
                            ],
                        },
                    ]}
                    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>) =>
                        GetCompanyJobList({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.name,
                            expired: params.expired,
                            status: params.status,
                            company_id: sessionStorage.getItem('post_company_info') ? JSON.parse(sessionStorage.getItem('post_company_info')).id : companyId
                        }).then(res => {
                            setList(res.data.rows)
                            setTotal(res.data.total)
                        })
                    }
                    onReset={() => {
                        if (sessionStorage.getItem('post_company_info')) {
                            sessionStorage.removeItem('post_company_info')
                        }
                        setCompanyId(0)
                        actionRef.current.reload();
                    }}
                    headerTitle="部门列表"
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            message.info('数据导出需要时间，请耐心等待')
                            PostJobExport().then(res => {
                                let url = Imageprefix + res.data.excel_url;
                                window.open(url);
                                message.info('数据导出完毕')
                            })
                        }}>
                            导出数据
                        </Button>,
                        <Button type="primary" onClick={() => {
                            dispatch({ type: 'openModel/getOpenModal', payload: true })
                        }}>
                            添加职位
                        </Button>
                    ]}
                />


            </ConfigProvider>

        </>



    );
};


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