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, ProFormSelect } 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 [shopOption, setShopOption] = useState([]);
    const [searchParams, setSearchParams] = useState(null);




    const searchShop = async (value) => {
        const { success, data } = await usersPage({
            userId: 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: '用户信息',
            dataIndex: 'userId',
            valueType: 'text',
            render: (_: any, record: any) => {
                return <div>
                    <div>用户ID:{record?.userId}</div>
                    <div>用户名：{record?.userName}</div>
                    <div>手机号：{record?.phone}</div>
                    <div>等级：{record?.level}</div>
                </div>
            },
            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: 'pointType',
            valueType: 'text',
            search: false,
            render: (_: any, record: any) => {
                return _ == 1 ? '收益' : '余额'
            },
        },
        {
            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: 'remarks',
            valueType: 'text',
            search: false,
        },
        {
            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 = 2
                        setSearchParams(params)
                        let { data } = await variationPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
        </PageContainer>
    );
};

export default LogTableList;
