import { variationPage, recordExtract } from '@/services/finance/index';
import { exportData } from '@/utils/func';

import React, { useRef, useEffect, useState } from 'react';
import { useIntl, useAccess } from '@umijs/max';
import { message, Tag, Select, Button } from 'antd';
import { ActionType, PageContainer, ProTable } from '@ant-design/pro-components';
import moneyType from '@/assets/json/moneyType.json'
import { usersPage } from '@/services/user/user';


const LogTableList: React.FC = () => {
    const actionRef = useRef<ActionType>();
    const [searchParams, setSearchParams] = useState(null);
    const [shopOption, setShopOption] = useState([]);

    const searchShop = async (value) => {
        const { success, data } = await usersPage({
            userName: value
        });
        data.records.map((i) => {
            i.userName = i.userName + ' ' + i.phone
        })
        setShopOption(data.records)
    }

    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
        },
        {
            title: '用户ID',
            dataIndex: 'userId',
            valueType: 'text',
            renderFormItem: (
                _,
                { type, defaultRender, formItemProps, fieldProps, ...rest },
                form,
            ) => {
                return <Select
                    {...fieldProps}
                    allowClear
                    showSearch
                    placeholder="请输入用户名"
                    style={{ width: "100%" }}
                    filterOption={false}
                    onSearch={
                        (e) => {
                            if (!e) {
                                return;
                            }
                            searchShop(e);
                        }
                    }
                    fieldNames={{
                        label: "userName",
                        value: "id"
                    }}
                    options={shopOption}
                />
            },
        },
        {
            title: '记录名称',
            dataIndex: 'recordName',
            valueType: 'text',
            search: false,
        },
        {
            title: '当前积分余额',
            dataIndex: 'curPoints',
            valueType: 'text',
            search: false,
        },
        {
            title: '积分变动',
            dataIndex: 'points',
            valueType: 'text',
            search: false,
        },
        {
            title: '来源',
            dataIndex: 'sourceType',
            valueType: 'text',
            search: true,
            render: (_: any, record: any) => {
                return record?.sourceType == 1 ? '订单' : record?.sourceType == 2 ? '充电桩服务费' : record?.sourceType == 3 ? '充值' : record?.sourceType == 4 ? '提现' : '-'
            },
            renderFormItem: (
                _,
                { type, defaultRender, formItemProps, fieldProps, ...rest },
                form,
            ) => {
                return <Select
                    {...fieldProps}
                    allowClear
                    style={{ width: "100%" }}
                    filterOption={false}
                    fieldNames={{
                        label: "name",
                        value: "id"
                    }}
                    options={[
                        {
                            name: '订单',
                            id: 1
                        }, {
                            name: '充电桩服务费',
                            id: 2
                        }, {
                            name: '充值',
                            id: 3
                        }, {
                            name: '提现',
                            id: 4
                        }
                    ]}
                />
            },
        },
        {
            title: '记录类型',
            dataIndex: 'type',
            valueType: 'text',
            search: true,
            render: (_: any, record: any) => {
                let list = moneyType
                return list.find((val) => val.id == record.type)?.name
            },
            renderFormItem: (
                _,
                { type, defaultRender, formItemProps, fieldProps, ...rest },
                form,
            ) => {
                return <Select
                    {...fieldProps}
                    allowClear
                    style={{ width: "100%" }}
                    filterOption={false}
                    fieldNames={{
                        label: "name",
                        value: "id"
                    }}
                    options={moneyType}
                />
            },
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'dateRange',
            search: {
                transform: (value) => {
                    return {
                        createTimeBegin: value[0],
                        createTimeEnd: value[1],
                    };
                },
            },
            render: (_, record) => {
                return record?.createTime
            },
        },
    ];

    return (
        <PageContainer>
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    actionRef={actionRef}
                    rowKey="id"
                    key="logList"
                    search={{
                        labelWidth: 120,
                        defaultCollapsed: false,
                    }}
                    toolBarRender={() => [
                        <Button type="primary" onClick={async () => {
                            let _res = await recordExtract(searchParams)
                            exportData(_res, '积分变动记录')
                        }}>导出</Button>,
                    ]}
                    request={async (params, sorter, filter) => {
                        params.pointType = 1
                        setSearchParams(params)
                        let { data } = await variationPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
        </PageContainer>
    );
};

export default LogTableList;
