import React from 'react';
import { useSetState, useRequest } from 'ahooks';
import { Space } from 'antd';
import moment from 'moment';
import { VMDatePicker } from '../vm-filter';
import StatCollection from '../vm-stat-collection';
import VMTable, { ExportBtn, renderColumnParam } from '../vm-table';
import useSettings from '../vm-hooks/useSettings';
import { ThrowData } from './api';
import projectDarkImg from '../../image/project-dark.png';
import projectImg from '../../image/project.png';
import realDarkImg from '../../image/real-dark.png';
import realImg from '../../image/real.png';
import PropTypes from 'prop-types';
import './style';
const ThrowContent = props => {
    const { id } = props;
    const [state, setState] = useSetState({
        start: moment().subtract(1, 'y'),
        end: moment(),
        ids: [],
    });
    const { tableProps } = useRequest(
        ({ current, pageSize }) =>
            ThrowData.getCollectList(
                current - 1,
                pageSize,
                id,
                state.start.format('YYYY-MM-DD hh:mm:ss'),
                state.end.format('YYYY-MM-DD hh:mm:ss'),
            ),
        {
            defaultPageSize: 10,
            paginated: true,
        },
    );
    const { isDark } = useSettings();
    const statInfo = ThrowData.useThrowStat(
        id,
        state.start.format('YYYY-MM-DD'),
        state.end.format('YYYY-MM-DD'),
    );
    const statData = [
        {
            icon: isDark ? projectDarkImg : projectImg,
            label: '收运次数',
            value: statInfo?.totalThrowCount,
            unit: '次',
        },
        {
            icon: isDark ? realDarkImg : realImg,
            label: '收运重量',
            value: statInfo?.participateInHousehold,
            unit: 'kg',
        },
    ];
    const handleDateChange = (start, end) => {
        setState({ start, end });
    };
    const handleExport = type => {
        let downloadAll = false;
        let ids = '';
        switch (type) {
            case 'all':
                downloadAll = true;
                break;
            case 'select-rows':
                ids = state.ids.join(',');
                break;
            case 'current-page':
                ids = tableProps?.dataSource?.map(i => i.id).join(',');
                break;
            default:
                break;
        }
        ThrowData.exportData({
            subjectId: id,
            startTime: state.start.format('YYYY-MM-DD hh:mm:ss'),
            endTime: state.end.format('YYYY-MM-DD hh:mm:ss'),
            downloadAll,
            ids,
            columnJson: renderColumnParam(columns),
        });
    };
    return (
        <div className="community-throw-content">
            <div className="top-bar">
                <StatCollection items={statData} />
                <Space className="right">
                    <VMDatePicker
                        defaultValue={[state.start, state.end]}
                        onChange={dates => dates && handleDateChange(dates[0], dates[1])}
                    />
                    <ExportBtn onClick={handleExport} />
                </Space>
            </div>
            <div className="table-container">
                <VMTable
                    columns={columns}
                    rowSelection={{
                        onChange: rowKeys => setState({ ids: rowKeys }),
                    }}
                    {...tableProps}
                />
            </div>
        </div>
    );
};
ThrowContent.propTypes = {
    id: PropTypes.string,
};

const columns = [
    {
        title: '收运单位',
        dataIndex: 'collectUnitName',
        key: 'collectUnitName',
    },
    {
        title: '收运车辆',
        dataIndex: 'carCode',
        key: 'carCode',
    },
    {
        title: '收运人员',
        dataIndex: 'houseNumber',
        key: 'houseNumber',
    },
    {
        title: '垃圾类型',
        dataIndex: 'productTypeName',
        key: 'productTypeName',
    },
    {
        title: '收运重量（kg）',
        dataIndex: 'weight',
        key: 'weight',
    },
    {
        title: '收运时间',
        dataIndex: 'actualCollectTime',
        key: 'actualCollectTime',
    },
];

export default ThrowContent;
