import { findList, configUpdate, configAdd, configDeleteBatchByIds } from '@/services/facility/index';


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



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


    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
        },
        {
            title: '设备类型',
            dataIndex: 'deviceType',
            valueType: 'text',
            render: (_, record) => {
                return record?.deviceType + 'W'
            },
            // renderFormItem: (
            //     _,
            //     { type, defaultRender, formItemProps, fieldProps, ...rest },
            //     form,
            // ) => {
            //     const statusMap = [
            //         { label: '7W', value: 7 },
            //         { label: '20W', value: 20 },
            //         { label: '30W', value: 30 },
            //     ]
            //     return <Select
            //         {...fieldProps}
            //         allowClear
            //         style={{ width: "100%" }}
            //         filterOption={false}
            //         fieldNames={{
            //             label: "label",
            //             value: "value"
            //         }}
            //         options={statusMap}
            //     />
            // },
        },
        {
            title: '补贴金额',
            dataIndex: 'amount',
            valueType: 'text',
            search: false,
        },
        {
            title: '上下浮动比例',
            dataIndex: 'earingsFloat',
            valueType: 'text',
            search: true,
        },
        {
            title: '基础收入服务费',
            dataIndex: 'baseAmount',
            valueType: 'text',
            search: false,
        },
        {
            title: '交付时间',
            dataIndex: 'deliverDate',
            valueType: 'text',
            search: false,
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'text',
            search: false,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: '220px',
            valueType: 'option',
            render: (_, record) => [
                <Button
                    type="link"
                    size="small"
                    hidden={!access.hasPerms('/device/subsidy/config/update')}
                    onClick={() => {
                        setModalVisible(true);
                        setCurrentRow(record);
                    }}
                >
                    编辑
                </Button>,
                <Button
                    type="link"
                    size="small"
                    danger
                    hidden={!access.hasPerms('/device/subsidy/config/deleteBatchByIds')}
                    onClick={async () => {
                        Modal.confirm({
                            title: '删除',
                            content: '确定删除该项吗？',
                            okText: '确认',
                            cancelText: '取消',
                            onOk: async () => {
                                const success = await configDeleteBatchByIds([record.id]);
                                if (success) {
                                    if (actionRef.current) {
                                        actionRef.current.reload();
                                    }
                                }
                            },
                        });
                    }}
                >
                    删除
                </Button>,
            ],
        },
    ];

    return (
        <PageContainer>
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    actionRef={actionRef}
                    rowKey="id"
                    key="logList"
                    search={{
                        labelWidth: 120,
                    }}
                    toolBarRender={() => [
                        <Button
                            type="primary"
                            key="add"
                            hidden={!access.hasPerms('/device/subsidy/config/add')}
                            onClick={async () => {
                                setCurrentRow(undefined);
                                setModalVisible(true);
                            }}
                        >
                            新建
                        </Button>
                    ]}
                    request={async (params, sorter, filter) => {
                        let { data } = await findList(params)
                        return {
                            data: data || [],
                            total: data.length,
                        };
                    }}
                    columns={columns}
                />
            </div>
            <UpdateForm
                onSubmit={async (values) => {
                    let success = false;
                    if (values.id) {
                        success = await configUpdate({ ...values });
                    } else {
                        success = await configAdd({ ...values });
                    }
                    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;
