import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, message, Table, Divider, Tag, Pagination, Modal, Avatar } from 'antd';
import router from 'umi/router';
import moment from 'moment';
import apis from '../../../../services/apis';
import request from '../../../../utils/request';

function Ranking(props) {

    const { id } = props
    // const [taNoticeList, setTaNoticeList] = useState([])
    const [data, setData] = useState({})

    useEffect(() => {
        getList({ pageNum: 1, pageSize: 10 });
    }, [])
    // 查询列表

    const getList = params => {
        request({ ...apis.redPacket.getRedPacketRankingList, params: { ...params, activityId: id } }).then(data => {
            setData(data)
        })
    }

    function handleSubmit(e) {
        e.preventDefault();
        props.form.validateFields((err, values) => {
            if (!err) {
                // eslint-disable-next-line no-console
                console.log('提交数据: ', values)
                getList({ pageNum: 1, pageSize: 10, ...values })
            }
        });
    }

    const changePageNum = pageNumber => {
        props.form.validateFields((err, values) => {
            if (!err) {
                getList({ pageNum: pageNumber, pageSize: 10, ...values })
            }
        });
    }

    function handleReset() {
        props.form.resetFields();

        getList({ pageNum: 1, pageSize: 10 })
    }

    const columns = [
        {
            title: '头像',
            dataIndex: 'avatarurl',
            key: 'avatarurl',
            align: 'center',
            render: (x, row) => <img src={row.avatarurl} alt="" width="64px"></img >,
        },
        {
            title: '昵称',
            dataIndex: 'nickname',
            key: 'nickname',
            align: 'center',
        },
        {
            title: '姓名',
            dataIndex: 'name',
            key: 'name',
            align: 'center',
        },
        {
            title: '手机号',
            dataIndex: 'phone',
            key: 'phone',
            align: 'center',
        },
        {
            title: '发起时间',
            dataIndex: 'createDate',
            key: 'createDate',
            align: 'center',
            render: (x, row) => <><span>{`${moment(row.createDate).format('YYYY-MM-DD HH:mm:ss')}`}</span></>,
        },
        {
            title: '助力人数',
            dataIndex: 'votes',
            key: 'votes',
            align: 'center',
        },
    ]

    function download(data) {
        // if (!data) {
        //     console.log('234')
        //     return
        // }
        const url = window.URL.createObjectURL(new Blob([data]))
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '排行榜.xlsx')
        document.body.append(link)
        link.click()
    }

    const exportReport = () => {

        request({ ...apis.redPacket.exportRedPacketRankingList, responseType: 'blob', params: { ...props.form.getFieldsValue(), activityId: id } }).then(data => {
            download(data)
        }).catch()
    }
    const { getFieldDecorator } = props.form

    return (

        <>
            <Form layout="inline" onSubmit={e => handleSubmit(e, props)}>
                <Form.Item>
                    {getFieldDecorator('phone')(
                        <Input
                            placeholder="手机号"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    <Button type="primary" htmlType="submit"> 搜索</Button>
                    <Button style={{ marginLeft: 8 }} onClick={handleReset}>重置</Button>
                </Form.Item>
            </Form>
            <div style={{ marginTop: '20px' }}>
                <span style={{ lineHeight: '32px' }}>共筛选出{data.total}条数据</span>
                <Button type="primary" onClick={() => exportReport()} style={{ float: 'right', marginBottom: '20px', zIndex: 1 }} >导出</Button>
            </div>
            <Table dataSource={data.records} columns={columns} pagination={false} style={{ marginTop: '20px' }} />
            <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
                <Pagination
                    showQuickJumper
                    defaultCurrent={1}
                    total={data.total} onChange={e => changePageNum(e)} current={data.current} />
            </div>
        </>

    )
}


const WrappedRanking = Form.create({ name: 'Ranking' })(Ranking);

export default WrappedRanking
