import { operatorPage, deleteBatchByIds, operatorAdd, operatorUpdate } from '@/services/operator/config';
import { exportData } from '@/utils/func';

import React, { useRef, useEffect, useState } from 'react';
import { useIntl, useAccess } from '@umijs/max';
import { message, Tag, Select, Tooltip, Button, Modal, Image, Popover } from 'antd';
import { ActionType, PageContainer, ProTable } from '@ant-design/pro-components';
import UpdateForm from './edit';
import { PlusOutlined, DownloadOutlined } from '@ant-design/icons';


const LogTableList: React.FC = () => {
    const actionRef = useRef<ActionType>();
    const [modalVisible, setModalVisible] = useState<boolean>(false);
    const [currentRow, setCurrentRow] = useState();



    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
            width: 30
        },
        {
            title: '比例配置名称',
            dataIndex: 'name',
            valueType: 'text',
        },
        {
            title: '电费分成比例',
            dataIndex: 'electricityRate',
            valueType: 'text',
            search: false,
        },
        {
            title: '服务费分成比例',
            dataIndex: 'serviceRate',
            valueType: 'text',
            search: false,
        },
        {
            title: '配置类型',
            dataIndex: 'configType',
            valueType: 'text',
            search: false,
        },
        
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'dateRange',
            search: {
                transform: (value) => {
                    return {
                        startTime: value[0],
                        endTime: value[1],
                    };
                },
            },
            render: (_: any, record: any) => {
                return record.createTime
            },
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 120,
            valueType: 'option',
            render: (_, record: any) => [
                <Button
                    type="link"
                    size="small"
                    // hidden={!access.hasPerms('/api/sys/user/update')}
                    onClick={async () => {
                        setModalVisible(true);
                        setCurrentRow(record);
                    }}
                >
                    编辑
                </Button>,
                <Button
                    type="link"
                    size="small"
                    danger
                    // hidden={!access.hasPerms('/api/sys/user/deleteUserBatchByIds')}
                    onClick={async () => {
                        Modal.confirm({
                            title: '删除',
                            content: '确定删除该项吗？',
                            okText: '确认',
                            cancelText: '取消',
                            onOk: async () => {
                                const success = await deleteBatchByIds([record.id]);
                                if (success) {
                                    if (actionRef.current) {
                                        message.success('删除成功');
                                        actionRef.current.reload();
                                    }
                                }
                            },
                        });
                    }}
                >
                    删除
                </Button>,
            ],
        },
    ];

    return (
        <PageContainer>
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    actionRef={actionRef}
                    rowKey="id"
                    key="logList"
                    search={{
                        labelWidth: 120,
                        defaultCollapsed: false,
                    }}
                    toolBarRender={() => [
                        <Button
                            type="primary"
                            key="add"
                            //   hidden={!access.hasPerms('/api/sys/user/add')}
                            onClick={async () => {
                                setCurrentRow(undefined);
                                setModalVisible(true);
                            }}
                        >
                            <PlusOutlined /> 新增
                        </Button>,
                    ]}
                    request={async (params, sorter, filter) => {
                        let { data } = await operatorPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
            <UpdateForm
                onSubmit={async (values) => {
                    let success = false;
                    if (values.id) {
                        success = await operatorUpdate({ ...values });
                        message.success('修改成功');
                    } else {
                        success = await operatorAdd({ ...values });
                        message.success('添加成功');
                    }
                    if (success) {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                        if (actionRef.current) {
                            actionRef.current.reload();
                        }
                    }
                }}
                onCancel={() => {
                    setModalVisible(false);
                    setCurrentRow(undefined);
                }}
                open={modalVisible}
                values={currentRow || {}}
            />
        </PageContainer>
    );
};

export default LogTableList;
