import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProTable
} from '@ant-design/pro-components';
import { Button, Image, ConfigProvider, Input, Descriptions, Space, Modal, Radio, Row, Col } from 'antd';
import { PostCompanyViplist, PostCompanyFeechange } from '@/apis/api';
import { Imageprefix } from '@/constants';




const CompanyListPage: 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 [fullname, setFullname] = useState<any>(sessionStorage.getItem('vip_company_info') ? JSON.parse(sessionStorage.getItem('vip_company_info')).full_name : '')

    const setId = (id: number) => {
        getId(id)
    }

    useEffect(() => {
        if (!openModel.openModal) {
            actionRef.current?.reload();

        }

        return () => {
            sessionStorage.removeItem('company_info')
            sessionStorage.removeItem('post_company_info')
            sessionStorage.removeItem('vip_company_info')

        };
    }, [openModel.openModal])

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

    const expandedRowRender = (record) => {
        return (
            <>
                <Space direction='vertical' style={{ width: '100%' }}>
                    <Descriptions title="基本信息" bordered column={3} contentStyle={{ fontWeight: 'bold' }} labelStyle={{ width: 180 }}>
                        <Descriptions.Item label="见习基地">{record.probation_text}</Descriptions.Item>
                        <Descriptions.Item label="知名企业">{record.famous_text}</Descriptions.Item>
                        <Descriptions.Item label="业务员">{record.sales}</Descriptions.Item>

                    </Descriptions>
                </Space >

            </>
        );
    };

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

                    }
                }}
            >
                <ProTable
                    scroll={{ y: 480, x: 'auto' }}
                    bordered={true}
                    actionRef={actionRef}
                    dataSource={list}
                    search={{
                        span: 8, labelWidth: 'auto'
                    }}
                    rowSelection={{
                        type: 'checkbox'
                    }}
                    expandable={{
                        expandedRowRender
                    }}
                    columns={[
                        {
                            title: '筛选日期',
                            dataIndex: 'date_range',
                            hidden: true,
                            valueType: 'dateRange',
                        },
                        {
                            title: 'ID',
                            dataIndex: 'id',
                            width: 200,
                        },
                        {
                            title: '企业名称',
                            dataIndex: 'full_name',
                            width: 200,
                            valueType: 'input',
                            renderFormItem: () => {
                                return (
                                    <><Input placeholder="请输入企业名称" value={fullname} onChange={(e) => {
                                        setFullname(e.target.value)
                                    }} /></>
                                )
                            }
                        },
                        {
                            title: '会员类型',
                            dataIndex: 'member_type_text',
                            width: 200,
                            search: false,
                        },
                        {
                            title: '会员状态',
                            dataIndex: 'member_status',
                            width: 200,
                            search: false,
                            valueEnum: {
                                0: { text: '全部' },
                                1: {
                                    text: '待审',
                                    status: 'Error'
                                },
                                2: {
                                    text: '正常',
                                    status: 'Processing'
                                },
                                3: {
                                    text: '到期',
                                    status: 'Error'
                                },
                                4: {
                                    text: '推荐',
                                    status: 'Error'
                                },
                            },
                        },
                        {
                            title: '开始时间',
                            dataIndex: 'start_date',
                            width: 200,
                            search: false,
                        },
                        {
                            title: '到期时间',
                            dataIndex: 'end_date',
                            width: 200,
                            search: false,

                        }, {
                            title: '剩余点数',
                            dataIndex: 'balance_fee',
                            width: 200,
                            search: false,
                        },
                        {
                            title: '操作',
                            key: 'option',
                            valueType: 'option',
                            render: (_, record, action) => [

                                <Button key='1' type='link' onClick={() => {
                                    setId(record.id)
                                    dispatch({ type: 'openModel/getOpenModal', payload: true })
                                }}>编辑</Button>,
                                <Button key='2' type='link' onClick={() => {
                                    setId(record.id)
                                    dispatch({ type: 'openModel/getOpenFeeModal', 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>) =>
                        PostCompanyViplist({
                            page: page,
                            pagesize: pageSize,
                            sort: 'id',
                            sortby: 'desc',
                            keyword: fullname,
                            start_date: params.date_range ? params.date_range[0] : '',
                            end_date: params.date_range ? params.date_range[1] : '',
                        }).then(res => {
                            setList(res.data.list)
                            setTotal(res.data.total)
                        })
                    }
                    onReset={() => {
                        if (sessionStorage.getItem('vip_company_info')) {
                            sessionStorage.removeItem('vip_company_info')
                        }
                        setFullname('')
                        actionRef.current?.reload();
                    }}
                    headerTitle="VIP企业列表"
                    toolBarRender={() => [
                        <Button type="primary" onClick={() => {
                            Modal.confirm({
                                title: '请选择对批量数据开通哪种会员类型',
                                content: (<>
                                    <div>参数： id, member_type,start_date,end_date </div>
                                    <h4 style={{ fontWeight: 'bold' }}>类型：</h4>
                                    <Radio.Group size='small' onChange={async (e) => {

                                        actionRef.current?.reload();
                                    }}>
                                        <Row gutter={[20, 20]} style={{ padding: 20 }}>
                                            <Col span={12}>
                                                <Radio value={1}>试用申请中</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>试用正式</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>普通申请中</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>普通正式</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>VIP申请中</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>VIP正式</Radio>
                                            </Col>
                                        </Row>
                                    </Radio.Group>
                                    <h4 style={{ fontWeight: 'bold' }}>时长：</h4>
                                    <Radio.Group size='small' onChange={async (e) => {
                                        actionRef.current?.reload();
                                    }}>
                                        <Row gutter={[20, 20]} style={{ padding: 20 }}>
                                            <Col span={12}>
                                                <Radio value={1}>1天</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>3天</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>7天</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>15天</Radio>
                                            </Col>
                                            <Col span={12}>
                                                <Radio value={1}>30天</Radio>
                                            </Col>
                                        </Row>
                                    </Radio.Group>
                                </>),
                                onOk() {

                                },
                                onCancel() {
                                    console.log('Cancel');
                                },
                                okText: '保存',
                                cancelText: '取消',
                                centered: true
                            });
                        }}>
                            批量开通会员
                        </Button>,
                    ]}
                />
            </ConfigProvider>
        </>
    );
};



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