import { Row, Col } from 'antd'
import style from '../index.less'
import _ from 'lodash'
// 表格数据...表格columns
export const createTableData = (data) => {
    if (!data || data?.length === 0) return { columns: [], dataSource: [] };
    const columns = [
        { title: '时间', dataIndex: 'time' },
    ]
    let dataSource = []
    const dateRange = data[0].trendLineList;
    if (dateRange && dateRange.length > 0) {
        dateRange.forEach((item, index) => {
            columns.push({
                title: `${item.name} ${index === 0 ? '' : '( 小时 )'}`,
                dataIndex: `data-${index}`,
                render: text => {
                    if (text) {
                        if (index !== 0) {
                            return (parseInt(text, 10) / 3600).toFixed(2)
                        }
                        return text || 0
                    }
                    return 0
                }
            })
        })
        const dataTime = dateRange[0].trendIndexList;
        dataSource = dataTime.map((item, index) => {
            const dataGroup = {};
            dateRange.forEach((child, index2) => {
                dataGroup[`data-${index2}`] = dateRange[index2]?.trendIndexList[index]?.yvalue || 0
            })
            return {
                id: index,
                time: item.xvalueText,
                ...dataGroup
            }
        })
    }
    return {
        columns,
        dataSource
    }
}

// 运行参数仪表数据处理
export const paramsMeterTableData = (data) => {
    let tableData = []
    const columns = [{
        title: '时间',
        dataIndex: 'dateTimeText',
        width: '150px'
    }];
    if (!data || data.length === 0) return [];
    _.forEach(data, item => {
        const arrObj = {};
        const arrValue = {};
        arrValue.dateTimeText = item?.dateTimeText;
        item?.deviceAndMeters?.map(i => {
            i?.deviceAndAttrInfos?.map(child => {
                child?.attrAndValueInfos?.map(children => {
                    arrValue[`${child?.device.id}-${children?.meterAttr?.id}`] = children?.value || ''
                    // console.log(i?.device?.id , children?.value)
                })
            })
        })
        tableData.push(arrValue)
        // console.log( arrValue )
        // console.log( tableData)
    })
    // const dealData = (data) => {
    //     const array1 = []
    //     const timeData = _.uniq(data?.map(item => item?.dateTimeText));
    //     const tempTimeText = [];
    //     const resultArr = [];
    //     data.forEach(item => {
    //         if(!tempTimeText.includes(item.dateTimeText)){
    //             tempTimeText.push(item.dateTimeText);
    //             resultArr.push({

    //             })
    //         }
    //     });

    //     // array1.push(arrValue)
    //     return array1
    // }
    // console.log(tableData)
    const { deviceAndMeters } = data[0];
    if (deviceAndMeters && deviceAndMeters.length > 0) {
        deviceAndMeters.forEach(item => {
            columns.push({
                // dataIndex: item.device.id,
                title: item.device.name,
                children: item?.deviceAndAttrInfos && item?.deviceAndAttrInfos.length > 0 && item?.deviceAndAttrInfos.map(child => {
                    return {
                        title: child?.device?.name || '',
                        // dataIndex: child?.device?.id,
                        children: child?.attrAndValueInfos && child?.attrAndValueInfos?.length > 0 && child?.attrAndValueInfos?.map(children => ({
                            title: `${children?.meterAttr?.name || ''}(${children?.meterAttr?.unitName || ''})`,
                            dataIndex: `${child?.device.id}-${children?.meterAttr?.id}`,
                        })) || []
                    }
                }) || []
            })
        })
    }
    // console.log(tableData , `-----------------------`)
    return {
        columns,
        tableData
    }
}

// 运行参数设备数据处理
export const paramsDeviceTableData = (data) => {
    const tableData = []
    const columns = [{
        title: '仪表类型',
        width: '150px',
        children: [
            {
                title: '时间', dataIndex: 'dateTimeText',
                render: (text, row, index) => {
                    return {
                        children: text,
                        props: {
                            rowSpan: row.rowSpan,
                        },
                    };
                },
            },
            { title: '属性', dataIndex: 'meter' }
        ]
    }];
    if (!data || data.length === 0) return [];
    data.forEach(item => {

        if (item.deviceRBOS && item.deviceRBOS.length > 0) {
            item.deviceRBOS.forEach((i, j) => {
                const arrValue = {};
                arrValue.meter = i.name;
                arrValue.dateTimeText = item?.dateTimeText;
                arrValue.rowSpan = j === 0 ? item?.deviceRBOS?.length : 0;
                i?.ambos?.forEach(child => {
                    if (child?.parameters && child?.parameters?.length > 0) {
                        child?.parameters?.map(items => {
                            arrValue[items?.meterAttr?.name] = items?.value || ''
                        })
                    }
                })
                // arrValue[i.id] = paramsTypes
                tableData.push(arrValue)
            })
        }

    })
    const { deviceRBOS } = data[0];
    if (deviceRBOS && deviceRBOS.length > 0) {
        deviceRBOS.forEach(item => {
            if (item?.ambos && item?.ambos?.length > 0) {
                item?.ambos?.forEach(child => {
                    if(columns?.findIndex(x=>x?.title === child?.typeName) < 0)
                    columns?.push({
                        title: child?.typeName || '',
                        children: child?.parameters?.map(items=>({
                            title: `${items?.meterAttr?.name || ''}(${items?.meterAttr?.unitName || ''})`,
                            dataIndex: items?.meterAttr?.name,
                        }))
                    })
                })
            }
        })
    }
    // console.log(tableData, `-------`, columns)
    return {
        columns,
        tableData
    }
}