import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType } from '@ant-design/pro-components';
import {
    ProTable
} from '@ant-design/pro-components';
import { Button, ConfigProvider, Select } from 'antd';
import { GetCustomerList, GetCustomerDetail, PostCustomerUpdate, PostCustomerDel } from '@/apis/api';
import { Imageprefix } from '@/constants';

const selectfieldNames = { label: 'name', value: 'id' };


const PagesMainJobseekerAccountTable: React.FC = ({ dispatch, getId, dictModel, 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
                    scroll={{ x: 1300 }}
                    actionRef={actionRef}
                    dataSource={list}
                    columns={[
                        {
                            title: '用户',
                            dataIndex: 'username',
                            width: 200
                        }, {
                            title: '邮箱',
                            dataIndex: 'email',
                            search: false
                        },
                        {
                            title: '联系方式',
                            dataIndex: 'mobile',
                            search: false
                        },
                        {
                            title: '登录次数',
                            dataIndex: 'login_count',
                            search: false
                        },
                        {
                            title: '账号状态',
                            dataIndex: 'status',
                            render: (_, record) => (<>
                                {
                                    <>
                                        <Select style={{ width: '100%' }}
                                            value={record.status === 1 ? '使用' : '禁用'}
                                            options={[{ name: '正常', id: 1 }, { name: '禁用', id: 2 }]}
                                            fieldNames={selectfieldNames}
                                            onChange={(value) => {
                                                PostCustomerUpdate({
                                                    id: record.id,
                                                    status: value
                                                }).then(res => {
                                                    actionRef.current.reload()
                                                })
                                            }}>
                                        </Select>
                                    </>
                                }

                            </>),
                            valueType: 'select',
                            valueEnum: {
                                0: { text: '全部' },
                                1: {
                                    text: '使用中',
                                },
                                2: {
                                    text: '已禁用',
                                }
                            },
                        },
                        {
                            title: '注册日期',
                            dataIndex: 'created_at',
                            search: false
                        },
                        {
                            title: '操作',
                            width: 100,
                            key: 'option',
                            valueType: 'option',
                            fixed: 'right',
                            render: (_, record, action) => [
                                <Button key='1' type='link' onClick={() => {
                                    setId(record)

                                    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>) =>
                        GetCustomerList({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: params.username,
                            status: params.status,
                            role: 1
                        }).then(res => {
                            setList(res.data.customers)
                            setTotal(res.data.total)
                        })
                    }
                    headerTitle="简历列表"
                />
            </ConfigProvider>

        </>



    );
};


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