// 经营品牌管理
import { connect } from "umi";
import type { CurrentUser } from "umi";
import type { ConnectState } from "@/models/connect";
import React, { useEffect, useRef, useState } from "react";
import ProCard from "@ant-design/pro-card";
import { MenuFoldOutlined } from "@ant-design/icons";
import type { FormInstance } from "antd";
import { Avatar, Button, Col, Form, message, Popconfirm, Row, Space, Spin, Tree, TreeSelect } from "antd";
import useRequest from "@ahooksjs/use-request";
import { getServerpartTree } from "@/services/options";
import type { ActionType } from "@ant-design/pro-table";
import ProTable from "@ant-design/pro-table";
import ReactHTMLTableToExcel from "react-html-table-to-excel";
import LeftSelectTree from "@/pages/reports/settlementAccount/component/leftSelectTree";
import PageTitleBox from "@/components/PageTitleBox";
import { getBusniessBrandTree } from "../service";
import { initial } from "lodash";
import { ModalForm, ProFormDigit, ProFormSelect, ProFormText, ProFormTextArea } from "@ant-design/pro-form";
import { getList } from "../BusinessTrade/service";
import { handleAddUpdate, handleDelete } from "./service";


const operatingBrand: React.FC<{ currentUser: CurrentUser }> = (props) => {
    const { currentUser } = props
    const downloadBtnRef = useRef<any>()
    const actionRef = useRef<ActionType>();
    const formRef = useRef<FormInstance>();
    const [reqDetailList, setReqDetailList] = useState<any>(); // 合计项数据源
    const [printOut, setPrintOut] = useState<any>(); // 打印数据的内容
    const [collapsible, setCollapsible] = useState<boolean>(false)
    const [treeView, setTreeView] = useState<any>()
    const [printIndex, setPrintIndex] = useState<number>(new Date().getTime())

    const [currentRow, setCurrentRow] = useState<any>();// 当前选中行数据
    // 树相关的属性和方法
    const [selectedId, setSelectedId] = useState<string>()
    // 导出的加载效果
    const [showLoading, setShowLoading] = useState<boolean>(false)
    // 是否显示打印的表格
    const [showExportTable, setShowExportTable] = useState<boolean>(false)
    // 查询的条件
    const [searchParams, setSearchParams] = useState<any>()
    // 导出数据
    const [exportData, setExportData] = useState<any>()
    const [createModalVisible, handleModalVisible] = useState<boolean>(false); // 新建窗口的弹窗
    const [treeSelectOption, setTreeSelectOption] = useState<any[]>(); // 可选择项

    const columns: any = [
        // {
        //     title: <div style={{ textAlign: 'center' }}>品牌图标</div>,
        //     dataIndex: 'BusinessTrade_ICO',
        //     align: 'right',
        //     hideInSearch: true,
        // },

        {
            title: "图标",
            dataIndex: 'BusinessTrade_ICO',
            width: 120,
            align: 'center',
            hideInSearch: true,
            hideInDescriptions: true,
            render: (_, record) => {
                return record?.BusinessTrade_ICO || record?.Brand_ICO ? <img style={{ width: "90px", height: "90px", borderRadius: '50%' }} src={record?.BusinessTrade_ICO} /> :
                    <Avatar src={_} size={16} shape="square">{record.BusinessTrade_Name ? record.BusinessTrade_Name.substring(0, 1) : record?.Brand_Name ? record.Brand_Name.substring(0, 1) : ''}</Avatar>
            }
        },
        {
            title: "有效状态",
            width: 120,
            align: 'center',
            dataIndex: "BrandState",
            valueType: "select",
            fieldProps: {
                options: [{ label: "有效", value: 1 }, { label: "无效", value: 0 }]
            },
            initialValue: 1,
        },
        {
            title: <div style={{ textAlign: 'center' }}>业态/品牌名称</div>,
            dataIndex: 'BusinessTrade_Name',
            align: 'center',
            hideInSearch: true,
            render: (_, record) => {
                return record?.Brand_Name || record?.BusinessTrade_Name
            }
        },
        {
            title: '操作',
            width: 120,
            dataIndex: 'option',
            valueType: 'option',
            align: 'center',
            hideInDescriptions: true,
            render: (_, record) => record?.Brand_ID ? [
                <a
                    key="edit"
                    onClick={() => {
                        setCurrentRow(record);
                        handleModalVisible(true);
                    }}
                >
                    编辑
                </a>,
                <Popconfirm title="确认删除该品牌?" onConfirm={async () => {

                    const sucesse = await handleDelete({ BrandId: record.Brand_ID })
                    if (sucesse && actionRef.current) {
                        actionRef.current.reload()
                    }
                }}>
                    <a>删除</a>
                </Popconfirm>
            ] : ""
        }
    ]

    const exportColumns: any = [
        {
            title: <div style={{ textAlign: 'center' }}>经营业态</div>,
            dataIndex: 'Business_Format',
            align: 'center',
            hideInSearch: true,
        },
        {
            title: <div style={{ textAlign: 'center' }}>品牌名称</div>,
            dataIndex: 'BusinessTrade_Name',
            align: 'center',
            hideInSearch: true,
        },
    ]

    const exportTable = (e) => {
        e.stopPropagation();                                            // 防止Collapse组件收起
        const main = document.getElementsByClassName(`saleReportHideBox${printIndex}`)[0]
        const thead = main.querySelector('thead').cloneNode(true);  // 深克隆DOM节点
        const tbody = main.querySelector('tbody').cloneNode(true);  // 深克隆DOM节点
        const container = document.querySelector('#hiddenBox');

        const tempTable = document.createElement('table');
        tempTable.appendChild(thead);
        tempTable.appendChild(tbody);

        tempTable.setAttribute('id', 'table-to-xls-operatingBrand');                    // 给table添加id，值与按钮上的table字段对应

        container.appendChild(tempTable);                                // 把创建的节点添加到页面容器中

        setShowLoading(false)

        downloadBtnRef.current.handleDownload();
        setShowExportTable(false)
        tempTable.remove() // 防止重复打印一个内容
    }

    useEffect(async () => {
        const data: any = await getList({ BusinessTradeModelPID: -1, BusinessTradeState: 1 })
        setTreeSelectOption([{ AUTOSTATISTICS_NAME: '默认', AUTOSTATISTICS_ID: -1, children: [...data.data] }])
    }, [])

    const normalizeTreeChildren = (list: any[]): any[] => {
        return list.map((node: any) => {
            // 复制节点，防止修改原始数据
            const newNode = { ...node };

            // 可能存在两种字段：BrandTreeList / children
            const subList = newNode.children || newNode.BrandTreeList;

            if (Array.isArray(subList) && subList.length > 0) {
                // 递归处理子节点
                newNode.children = normalizeTreeChildren(subList);
            } else {
                // ✅ 空数组改为 null
                newNode.children = null;
            }

            // 删除旧字段
            delete newNode.BrandTreeList;

            return newNode;
        });
    };

    return (
        <div ref={(el) => {
            // 打印报表
            if (!reqDetailList || reqDetailList.length === 0) return;
            setPrintOut(el);
        }} >

            {
                showLoading ?
                    <div
                        style={{
                            width: '100%',
                            height: '100%',
                            background: 'rgba(0,0,0,0.1)',
                            position: 'fixed',
                            zIndex: 5,
                            display: 'flex',
                            alignItems: 'center',
                            justifyContent: 'center'
                        }}
                    >
                        <div style={{
                            display: 'flex',
                            alignItems: 'center',
                            justifyContent: 'center',
                            padding: '15px 20px 10px',
                            background: '#fff',
                            borderRadius: '8px',
                            width: '200px'
                        }}>
                            <Spin />
                            <span style={{ marginLeft: '5px' }}>数据导出中...</span>
                        </div>
                    </div> : ''
            }

            <div className={`saleReportHideBox${printIndex}`} style={{ position: 'fixed', zIndex: -1, top: 0, left: 0 }}>
                {
                    showExportTable && exportData && exportData.length > 0 ?
                        <ProTable
                            columns={exportColumns}
                            dataSource={exportData}
                            pagination={false}
                            expandable={{
                                defaultExpandAllRows: true
                            }}
                        /> : ''
                }
            </div>
            <div id='hiddenBox' style={{ position: 'fixed', zIndex: -1, top: 0, left: 0 }} />

            <div style={{ backgroundColor: '#fff', display: 'flex' }}>
                <div style={{
                    width: '100%',
                    paddingTop: 0,
                    paddingBottom: 0,
                    paddingRight: 0
                }}>
                    <ProTable
                        actionRef={actionRef}
                        formRef={formRef}
                        columns={columns}
                        rowKey={(record) => {
                            return `${record?.BusinessTrade_Id}`
                        }}
                        bordered
                        expandable={{
                            expandRowByClick: true
                        }}
                        scroll={{ x: "100%", y: "calc(100vh - 410px)" }}
                        headerTitle={<PageTitleBox props={props} />} // 列表表头
                        search={{ span: 6 }}
                        request={async (params) => {
                            const req: any = {
                                ProvinceCode: currentUser?.ProvinceCode,
                                BrandState: 1
                            }
                            setSearchParams(params)
                            const data = await getBusniessBrandTree(req)
                            if (data && data.length > 0) {
                                let exportData: any = []

                                data.forEach((item: any) => {
                                    exportData.push({
                                        Business_Format: item.BusinessTrade_Name,
                                        BusinessTrade_Name: ""
                                    })
                                    if (item.children && item.children.length > 0) {
                                        item.children.forEach((subItem: any) => {
                                            exportData.push({
                                                Business_Format: item.BusinessTrade_Name,
                                                BusinessTrade_Name: subItem.BusinessTrade_Name,
                                            })
                                        })
                                    }
                                })
                                setExportData(exportData)
                                console.log('datadatadata', data);

                                let res: any = normalizeTreeChildren(data)
                                console.log(res, 'res');

                                return { data: res, success: true }
                            }
                            return { data: [], success: true }
                        }}
                        toolbar={{
                            actions: [
                                <span style={{ visibility: 'hidden' }}>
                                    <ReactHTMLTableToExcel
                                        buttonText={'导出excel'}
                                        ref={downloadBtnRef}
                                        table="table-to-xls-operatingBrand"
                                        filename={`经营品牌列表`}
                                        sheet="sheet1"
                                    />
                                </span>,
                                <Button
                                    key="new"
                                    type="primary"
                                    onClick={(e) => {
                                        if (exportData && exportData.length > 0) {
                                            setShowLoading(true)
                                            setTimeout(() => {
                                                setShowExportTable(true)
                                                setTimeout(() => {
                                                    exportTable(e)
                                                }, 100)
                                            }, 100)
                                        } else {
                                            message.error('暂无数据可导出！')
                                        }
                                    }}
                                >
                                    导出excel
                                </Button>,
                                <Button
                                    key="new"
                                    type="primary"
                                    onClick={(e) => {
                                        handleModalVisible(true)
                                    }}
                                >
                                    新增品牌
                                </Button>
                            ]
                        }}
                    />
                </div>
            </div>


            { /* 更新 信息弹出框 */}
            <ModalForm
                layout={'horizontal'}
                wrapperCol={{ span: 16 }}
                labelCol={{ span: 6 }}
                width={600}
                title={currentRow ? '更新经营业态' : '新建经营业态'}
                visible={createModalVisible}
                formRef={formRef}
                onVisibleChange={(value) => {
                    handleModalVisible(value)
                    if (!value) {
                        formRef.current?.resetFields();
                        setCurrentRow(undefined);
                    } else {
                        formRef.current?.setFieldsValue(currentRow || { AUTOSTATISTICS_TYPE: 2000 })

                    }
                }}
                onFinish={async (values) => {
                    let newValue: any = { ...values }

                    if (currentRow) { // 编辑数据
                        newValue = { ...currentRow, ...newValue }
                    }
                    newValue.AUTOSTATISTICS_STATE = values.AUTOSTATISTICS_STATE ? 1 : 0
                    newValue.AUTOSTATISTICS_TYPE = currentRow?.AUTOSTATISTICS_TYPE || 2000
                    const success = await handleAddUpdate(newValue);

                    if (success) {
                        if (actionRef.current) {
                            actionRef.current.reload();
                        }
                        return true

                    }
                    return false
                }}
            >
                <Row>
                    <Col span={24}>
                        <Form.Item
                            name="BRAND_INDUSTRY"
                            label="上级业态"
                            rules={[
                                {
                                    required: true,
                                    message: '请选择上级业态',
                                },
                            ]}
                        >
                            <TreeSelect
                                placeholder="请选择上级业态"
                                dropdownStyle={{ maxHeight: 300, overflow: 'auto' }}
                                treeDefaultExpandedKeys={currentRow ? [currentRow?.BRAND_INDUSTRY] : [-1]}
                                // treeDataSimpleMode
                                showArrow={false}
                                treeData={treeSelectOption}
                                fieldNames={{
                                    label: 'AUTOSTATISTICS_NAME',
                                    value: 'AUTOSTATISTICS_ID'
                                }}
                            />
                        </Form.Item>
                        <ProFormText
                            name="BRAND_NAME"
                            label="品牌名称"
                            placeholder="请输入品牌名称"
                            rules={[
                                {
                                    required: true,
                                    message: '请输入品牌名称',
                                },
                            ]} />

                        <ProFormDigit
                            name="BRAND_INDEX"
                            label="品牌索引"
                            placeholder="请输入品牌索引"
                            min={0}
                            max={9999}
                            rules={[
                                {
                                    required: true,
                                    message: '请输入品牌索引',
                                },
                            ]}
                            fieldProps={{ precision: 0 }}
                        />

                        <ProFormSelect
                            name="BRAND_CATEGORY"
                            label="品牌类型"
                            rules={[
                                {
                                    required: true,
                                    message: '请选择品牌类型',
                                },
                            ]}
                            options={[{ label: '经营品牌', value: 1000 }, { label: '商城品牌', value: 2000 }]}
                        />

                        <ProFormSelect
                            name="BRAND_STATE"
                            label="是否有效"
                            options={[{ label: '有效', value: 1 }, { label: '无效', value: 0 }]}
                            initialValue={1}
                        />
                        <ProFormTextArea
                            name="BRAND_DESC"
                            label="品牌介绍"
                            placeholder="请输入介绍" />
                    </Col>
                </Row>
            </ModalForm>
        </div>
    )
}

export default connect(({ user }: ConnectState) => ({
    currentUser: user.currentUser
}))(operatingBrand);
