import { merchantPage, getInfo, merchantUpdate, merchantAdd, deleteBatchByIds, exportExcel } from '@/services/merchant/index';
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 [searchParams, setSearchParams] = useState(null);

    const content = (
        <Button color="default" icon={<DownloadOutlined />} type="link">
            商户导入模板
        </Button>
    );


    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
            width: 30
        },
        // {
        //     title: '用户名',
        //     dataIndex: 'userName',
        //     valueType: 'text',
        //     width: 120
        // },
        // {
        //     title: '手机号',
        //     dataIndex: 'phone',
        //     valueType: 'text',
        //     width: 120
        // },
        // {
        //     title: '电费分成比例',
        //     dataIndex: 'electricityRate',
        //     valueType: 'text',
        //     search: false,
        //     width: 100
        // },
        // {
        //     title: '服务费分成比例',
        //     dataIndex: 'serviceRate',
        //     valueType: 'text',
        //     search: false,
        //     width: 120
        // },
        // {
        //     title: '商户余额',
        //     dataIndex: 'balance',
        //     valueType: 'text',
        //     search: false,
        //     width: 100
        // },
        {
            title: '商户名称',
            dataIndex: 'merchantInfoVO',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <div>
                    <div>
                    {/* 商户名称: */}
                         <span style={{ marginLeft: '10px' }}>{_.merchantInfoVO.merchantName}</span>
                    </div>
                    {/* <div>
                        营业执照:
                        <span style={{ marginLeft: '10px' }}><Image src={_.merchantInfoVO.licenseImage} width={50}></Image></span>
                    </div>
                    <div>
                        社会信用代码:
                        <span style={{ marginLeft: '10px' }}>{_.merchantInfoVO.orgCode}</span>
                    </div>

                    <div>
                        营业执照编码:

                        <span style={{ marginLeft: '10px' }}>{_.merchantInfoVO.licenseNo}</span>
                    </div>
                    <div>
                        商户负责人名称:

                        <span style={{ marginLeft: '10px' }}>{_.merchantInfoVO.headName}</span>
                    </div>
                    <div>
                        联系电话:
                        <span style={{ marginLeft: '10px' }}>{_.merchantInfoVO.contactPhone}</span>
                    </div> */}
                </div>
            },
        },
        {
            title: '营业执照',
            dataIndex: 'balance',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <Image src={_.merchantInfoVO.licenseImage} width={50}></Image>
            },
        },
        {
            title: '社会信用代码',
            dataIndex: 'orgCode',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <div>{_.merchantInfoVO.orgCode}</div>
            },
        },
        {
            title: '营业执照编码',
            dataIndex: 'licenseNo',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <div>{_.merchantInfoVO.licenseNo}</div>
            },
        },
        {
            title: '商户负责人名称',
            dataIndex: 'licenseNo',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <div>{_.merchantInfoVO.headName}</div>
            },
        },
        {
            title: '联系电话',
            dataIndex: 'licenseNo',
            valueType: 'text',
            search: false,
            render: (data, _) => {
                return <div>{_.merchantInfoVO.contactPhone}</div>
            },
        },
        {
            title: '状态',
            dataIndex: 'status',
            valueType: 'text',
            search: true,
            width: 120,
            render: (_: any, record: any) => {
                return _ == 1 ? '正常' : '正常'
            },
            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,
            width: 160
        },
        {
            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 })
                        data.merchantInfoUpdateParams = data.merchantInfoVO
                        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>,
                        // <Popover content={content} title="导入模板">
                        //     <Button type="primary">导入</Button>
                        // </Popover>,
                        // <Button type="primary" onClick={async () => {
                        //     let _res = await exportExcel(searchParams)
                        //     exportData(_res, '商户列表')
                        // }}>导出</Button>,
                    ]}
                    request={async (params, sorter, filter) => {
                        setSearchParams(params)
                        let { data } = await merchantPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
            <UpdateForm
                onSubmit={async (values) => {
                    let success = false;
                    if (values.id) {
                        success = await merchantUpdate({ ...values });
                    } else {
                        success = await merchantAdd({ ...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;
