import { stationsPage, stationsUpdate, stationsAdd, deleteBatchByIds } from '@/services/power/site';

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 { getMerchantSelect, getSiteTypeSelect } from '@/services/merchant/index'
import { history } from '@umijs/max';
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 [optionCd, setOptionCd] = 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)
    }


    useEffect(() => {
        getSiteTypeSelect().then(({ data }) => {
            setOptionCd(data)
        })

    }, []);


    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            valueType: 'text',
            search: false,
            width: 30
        },
        {
            title: '商户信息',
            dataIndex: 'merchantId',
            valueType: 'text',
            width: 150,
            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: 'stationName',
            valueType: 'text',
            width: 120
        },
        {
            title: '站点地址',
            dataIndex: 'stationAddress',
            valueType: 'text',
            search: false,
            width: 150,
            render: (data) => {
                return (
                    <div className="ellipsis" style={{
                        float: 'left',
                        maxWidth: '150px',
                        overflow: 'hidden',
                        whiteSpace: 'nowrap',
                        textOverflow: 'ellipsis',
                    }} title={data}>
                        <Tooltip placement="topLeft" title={data}>{data}</Tooltip>
                    </div>
                )
            },
        },
        {
            title: '站点电话',
            dataIndex: 'stationTel',
            valueType: 'text',
            search: false,
            width: 100
        },
        {
            title: '枪数',
            dataIndex: 'siteType',
            valueType: 'text',
            search: false,
            width: 160,
            render: (_: any, record: any) => {
                return <div>
                    <div>
                        已使用总数:{record.gunCount}
                    </div>
                    <div>
                        枪正常总数:{record.gunUseCount}
                    </div>
                </div>

            },
        },
        {
            title: '价格模板',
            dataIndex: 'templateName',
            valueType: 'text',
            search: false,
            width: 120
        },
        {
            title: '场地类型',
            dataIndex: 'siteType',
            valueType: 'text',
            width: 120,
            render: (_: any, record: any) => {
                let data = optionCd.find((val) => val.id == _)
                return data ? data.label : '_'
            },
            renderFormItem: (
                _,
                { type, defaultRender, formItemProps, fieldProps, ...rest },
                form,
            ) => {
                return <Select
                    {...fieldProps}
                    allowClear
                    placeholder="请选择场地类型"
                    style={{ width: "100%" }}
                    filterOption={false}
                    fieldNames={{
                        label: 'label',
                        value: 'id'
                    }}
                    options={optionCd}
                />
            },
        },

        {
            title: '车位类型',
            dataIndex: 'parkType',
            valueType: 'text',
            search: true,
            width: 120,
            render: (_: any, record: any) => {
                return _ == 1 ? '地上电站' : _ == 2 ? '地下车库' : '-'
            },
            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: 'siteType',
            valueType: 'text',
            search: false,
            width: 160,
            render: (_: any, record: any) => {
                return <div>
                    <div>
                        经度:{record.stationLon}
                    </div>
                    <div>
                        纬度:{record.stationLat}
                    </div>
                </div>

            },
        },
        {
            title: '省市区',
            dataIndex: 'siteType',
            valueType: 'text',
            width: 120,
            search: false,
            render: (data, record) => {
                return <div>{record.provinceName}/{record.cityName}/{record.areaName}</div>
            },
        },
        {
            title: '营业时间描述',
            dataIndex: 'busineHours',
            valueType: 'text',
            width: 120,
            search: false,
        },
        {
            title: '服务描述',
            dataIndex: 'serviceInfo',
            valueType: 'text',
            search: false,
            width: 120,
        },
        {
            title: '状态',
            dataIndex: 'status',
            valueType: 'text',
            search: true,
            width: 120,
            render: (_: any, record: any) => {
                // 状态:0-预备中;1-启用;2-暂停营业;3-下线;
                return _ == 0 ? '预备中' : _ == 1 ? '启用' : _ == 2 ? '暂停营业' : _ == 3 ? '下线' : ''
            },
            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: 0
                        },
                        {
                            name: '启用',
                            id: 1
                        },
                        {
                            name: '暂停营业',
                            id: 2
                        },
                        {
                            name: '下线',
                            id: 3
                        },
                    ]}
                />
            },
        },
        {
            title: '备注',
            dataIndex: 'remarks',
            valueType: 'text',
            search: false,
            width: 120
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            valueType: 'text',
            search: false,
            width: 120
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 120,
            valueType: 'option',
            fixed: 'right',
            render: (_, record: any) => [
                <Button
                    type="link"
                    size="small"
                    // hidden={!access.hasPerms('/api/sys/user/update')}
                    onClick={() => {
                        // setModalVisible(true);
                        // setCurrentRow(record);
                        history.push(`/power/site/${record.id}/update`);
                    }}
                >
                    编辑
                </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);
                                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,
                    }}
                    scroll={{ x: 2200 }}
                    toolBarRender={() => [
                        <Button
                            type="primary"
                            key="add"
                            //   hidden={!access.hasPerms('/api/sys/user/add')}
                            onClick={async () => {
                                history.push('/power/site/0/create');
                                // history.push('goods/0/create');
                                // setCurrentRow(undefined);
                                // setModalVisible(true);
                            }}
                        >
                            <PlusOutlined /> 新增
                        </Button>,
                    ]}
                    request={async (params, sorter, filter) => {

                        let { data } = await stationsPage(params)
                        return {
                            data: data?.records || [],
                            total: data?.total,
                        };
                    }}
                    columns={columns}
                />
            </div>
            <UpdateForm
                onSubmit={async (values) => {
                    let success = false;
                    if (values.id) {
                        success = await stationsUpdate({ ...values });
                    } else {
                        success = await stationsAdd({ ...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;
