/** @format */

import * as React from 'react';
import {Table} from 'antd';
import moment from 'moment';
import {ColumnProps} from 'antd/es/table';
import BaseApiSelect from '../../../../components/select/baseApiSelect';
import {useDataOverview} from '../../../../service/dashbordMerchant';
import {formatNumer} from '../../../../utils/operation';
const nowTime = moment();
const nowDay = nowTime.format('MM-DD');
const yesterday = nowTime.subtract(1, 'd').format('MM-DD');
const weekStart = nowTime
    .startOf('week')
    .add(1, 'd')
    .format('MM-DD');
const weekEnd = nowTime
    .endOf('week')
    .add(1, 'd')
    .format('MM-DD');
const monthStart = nowTime.startOf('month').format('MM-DD');
const monthEnd = nowTime.endOf('month').format('MM-DD');

interface dataOverviewColumsProps {
    title: string;
    dataIndex: string;
    key: string;
}
const columns: ColumnProps<dataOverviewColumsProps>[] = [
    {
        title: '',
        dataIndex: 'name',
        key: 'name',
    },
    {
        title: `今日(${nowDay})`,
        dataIndex: 'today',
        key: 'today',
        align: 'right',
        render: text => {
            if (typeof text === 'number') {
                return formatNumer(text);
            }
            return text;
        },
    },
    {
        title: `昨日(${yesterday})`,
        dataIndex: 'yesterday',
        key: 'yesterday',
        align: 'right',
        render: text => {
            if (typeof text === 'number') {
                return formatNumer(text);
            }
            return text;
        },
    },
    {
        title: `本周(${weekStart}--${weekEnd})`,
        dataIndex: 'week',
        key: 'week',
        align: 'right',
        render: text => {
            if (typeof text === 'number') {
                return formatNumer(text);
            }
            return text;
        },
    },
    {
        title: `本月(${monthStart}--${monthEnd})`,
        dataIndex: 'month',
        key: 'month',
        align: 'right',
        render: text => {
            if (typeof text === 'number') {
                return formatNumer(text);
            }
            return text;
        },
    },
];
interface Iprops {
    today_end_timestamp: number;
}
interface Istate {
    api_id: '' | number;
}
/**
 * 数据概况
 */
const DataOverview: React.FC<Iprops> = props => {
    const {today_end_timestamp} = props;
    const [state, setState] = React.useState<Istate>({
        api_id: '',
    });
    const [dataOverviewState, requetDataOverview] = useDataOverview();
    React.useEffect(() => {
        requetDataOverview({
            today_end_timestamp: today_end_timestamp,
            api_id: state.api_id || '',
        });
    }, [today_end_timestamp, state.api_id]);
    const handleChangeMerchange = (api_id: any) => {
        setState(preState => ({...preState, api_id}));
    };
    return (
        <>
            <BaseApiSelect onChange={handleChangeMerchange} style={{width: 250}} />
            <Table
                rowKey='key'
                size='small'
                pagination={false}
                loading={dataOverviewState.loading}
                dataSource={dataOverviewState.dataSource}
                columns={columns}
            />
        </>
    );
};
export default DataOverview;
