import { usersPage, usersCardPage, addCardBalance } from '@/services/user/index';


import React, { useRef, useEffect, useState } from 'react';
import { useIntl, useAccess } from '@umijs/max';
import { message, Tag, Select, Tooltip, Button } from 'antd';
import { ActionType, PageContainer, ProTable } from '@ant-design/pro-components';
import UserAddCard from './userAddCard';
import CardTable from './cardTable';




const LogTableList: React.FC = () => {
    const actionRef = useRef<ActionType>();
    const [modalVisible, setModalVisible] = useState<boolean>(false);
    const [modalVisit, setModalVisit] = useState<any>(false)
    const [row1, setRow1] = useState<any>()



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

    const columns: any = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
        },
        {
            title: '用户信息',
            dataIndex: 'userId',
            valueType: 'text',
            search: true,
            render: (_: any, record: any) => {
                return <div>
                    <div>用户ID：{record?.userId}</div>
                    <div>用户昵称：{record?.nickName}</div>
                    <div>手机号：{record?.phone}</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: "nickName",
                        value: "id"
                    }}
                    options={shopOption}
                />
            },
        },
        {
            title: '卡号',
            dataIndex: 'cardNo',
            valueType: 'text',
            search: true,
        },
        {
            title: '用户余额',
            dataIndex: 'balance',
            valueType: 'text',
            search: false,
        },
        {
            title: '状态',
            dataIndex: 'status',
            valueType: 'text',
            search: true,
            render: (_: any, record: any) => {
                return record.statusName
            },
            renderFormItem: (
                _,
                { type, defaultRender, formItemProps, fieldProps, ...rest },
                form,
            ) => {
                return <Select
                    {...fieldProps}
                    allowClear
                    placeholder="请选择状态"
                    style={{ width: "100%" }}
                    filterOption={false}
                    fieldNames={{
                        label: "name",
                        value: "id"
                    }}
                    options={[
                        {
                            name: '正常',
                            id: 1
                        },
                        {
                            name: '禁用',
                            id: 2
                        }
                    ]}
                />
            },
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'text',
            search: false,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 170,
            valueType: 'option',
            fixed: 'right',
            render: (_, record: any) => {
                return <div className='flex flex-wrap'>
                    <Button
                        size="small"
                        type="primary"
                        style={{ marginRight: 10, marginBottom: 10 }}
                        onClick={() => {
                            setRow1(record)
                            setModalVisit(true)
                        }}
                    >
                        电卡记录
                    </Button>
                </div>
            },
        },
    ];

    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 () => {
                            setModalVisible(true)
                        }}>赠送电卡</Button>,
                    ]}
                    request={async (params, sorter, filter) => {

                        let { data } = await usersCardPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
                <UserAddCard
                    onSubmit={async (values) => {
                        let success = false;
                        success = await addCardBalance({ ...values });
                        if (success) {
                            setModalVisible(false);
                            if (actionRef.current) {
                                actionRef.current.reload();
                            }
                        }
                    }}
                    onCancel={() => {
                        setModalVisible(false);
                    }}
                    open={modalVisible}
                />
                <CardTable modalVisit={modalVisit} values={row1} setModalVisit={setModalVisit} />
            </div>
        </PageContainer>
    );
};

export default LogTableList;
