import { pricePage, priceUpdate, priceAdd, deleteBatchByIds, getInfo } from '@/services/power/price';


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



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

    const [shopOption, setShopOption] = useState([]);
    const searchShop = async (value) => {
        const { success, data } = await merchantPage({
            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,
            width: 30
        },
        {
            title: '商户信息',
            dataIndex: 'merchantId',
            valueType: 'text',
            render: (_: any, record: any) => {
                return <div>
                    <div>商户ID：{record?.merchantId}</div>
                    <div>商户名称：{record?.merchantName}</div>
                    <div>商户电话：{record?.merchantPhone}</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: 'templateName',
            valueType: 'text',
            width: 150
        },
        {
            title: '尖',
            dataIndex: '_',
            valueType: 'text',
            search: false,
            width: 120,
            render: (data, _) => {
                return <div>
                    <div>
                        电费:{_.tipElectricityRate}
                    </div>
                    <div>
                        服务费:{_.tipServiceFeeRate}
                    </div>
                </div>
            },
        },
        {
            title: '峰',
            dataIndex: '_',
            valueType: 'text',
            search: false,
            width: 120,
            render: (data, _) => {
                return <div>
                    <div>
                        电费:{_.peakElectricityRate}
                    </div>
                    <div>
                        服务费:{_.peakServiceFeeRate}
                    </div>
                </div>
            },
        },
        {
            title: '平',
            dataIndex: '_',
            valueType: 'text',
            search: false,
            width: 120,
            render: (data, _) => {
                return <div>
                    <div>
                        电费:{_.flatElectricityRate}
                    </div>
                    <div>
                        服务费:{_.flatServiceFeeRate}
                    </div>
                </div>
            },
        },
        {
            title: '谷',
            dataIndex: '_',
            valueType: 'text',
            search: false,
            width: 120,
            render: (data, _) => {
                return <div>
                    <div>
                        电费:{_.valleyElectricityRate}
                    </div>
                    <div>
                        服务费:{_.valleyServiceFeeRate}
                    </div>
                </div>
            },
        },
        {
            title: '停车收费说明',
            dataIndex: 'parkCostInfo',
            valueType: 'text',
            search: false,
            width: 100
        },
        {
            title: '占用收费说明',
            dataIndex: 'occupyCostInfo',
            valueType: 'text',
            search: false,
            width: 120
        },
        {
            title: '价格模板类型',
            dataIndex: 'priceTemplateSegmentList',
            valueType: 'text',
            search: false,
            width: 180,
            render: (data, _) => {
                return _.priceTemplateSegmentList ? _.priceTemplateSegmentList.map((item, index) => {
                    return <div key={index}>
                        <div>
                            {item.typeName}
                        </div>
                        <div>
                            {item.startTime}-{item.endTime}
                        </div>
                    </div>
                }) : '-'
            },
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'text',
            search: false,
            width: 120
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 120,
            valueType: 'option',
            render: (_, record: any) => [
                <Button
                    type="link"
                    size="small"
                    // hidden={!access.hasPerms('/api/sys/user/update')}
                    onClick={async () => {
                        let { data } = await getInfo({ id: record.id })
                        setModalVisible(true);
                        setCurrentRow(data);
                    }}
                >
                    编辑
                </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) {
                                        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 pricePage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
            <UpdateForm
                onSubmit={async (values) => {
                    let success = false;
                    if (values.id) {
                        success = await priceUpdate({ ...values });
                    } else {
                        success = await priceAdd({ ...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;
