import React, { useEffect, useState } from 'react'
import { Input, Tooltip, Empty, Pagination, Popover, message } from 'antd'
import { InsertRowBelowOutlined, DatabaseOutlined, FileOutlined, PullRequestOutlined, KeyOutlined } from '@ant-design/icons';
import { useFsRequest, ApiTable } from '$utils';
import { connect } from 'react-redux';
import { downloadImg, markRedKeywords } from '../utils/index'
import KeyModal from '../components/keyModal';
import xlsx from 'xlsx';
const METADTA_TYPE = {
    '库': 'databases',
    '表': 'databases',
    '文件': 'files',
    '接口': 'restapis',
}
const METADTA_TYPE_NAMES = {
    '库': '库表/库(Schema)',
    '表': '库表/表(Table)',
    '文件': '文件(File)',
    '接口': '接口(Api)',
}
import './style.less';
function Retrieval(props) {
    const { user, catalogs, dispatch, actions } = props;
    const [keywords, setKeywords] = useState()
    const [firstInput, setFirstInput] = useState()
    const [page, setPage] = useState(1)
    const [currentData, setCurrentData] = useState(null)
    const [searchDataId, setSearchDataId] = useState(null)
    const formRef = React.createRef();
    // const { data: catalogs = [] } = useFsRequest({
    //     url: ApiTable.getResourceCatalog,
    // });
    useEffect(() => {
        dispatch(actions.metadataManagement.getResourceCatalog())
    }, [])
    //检索元数据
    const { data: result = {} } = useFsRequest({
        url: ApiTable.searchMetadata,
        query: {
            keywords: keywords
        },
        refreshDeps: [keywords],
        ready: !!keywords
    });
    //用户申请资源列表
    const { data: approveList = {} } = useFsRequest({
        url: ApiTable.approveList,
        query: {
            applyById: user?.id
        },
        refreshDeps: [user?.id],
        ready: !!(user?.id)
    });
    //检索元数据
    const { data: tableData = {} } = useFsRequest({
        url: 'meta/table/data',
        query: {
            id: searchDataId
        },
        refreshDeps: [searchDataId],
        ready: !!searchDataId,
    });
    useEffect(() => {
        if (tableData?.rslt && tableData?.metaDataChildren) handleTableExport()
    }, [tableData])
    const handleTableExport = () => {
        const metaData = result?.rows?.find(s => s.id == searchDataId)
        if (!metaData) return;
        let excelTitle = tableData?.metaDataChildren.map(s => {
            return { k: s, v: s }
        });
        let workBook = {
            SheetNames: [], //sheet名称
            Sheets: {}     //根据SheetNames名称顺序依次添加每个sheet数据
        };
        if (tableData?.rslt && tableData?.rslt?.length > 0) {
            let sheetName = '元数据列表';
            let sheetDataMap = new Map();
            let sheetData = [excelTitle];
            let index = 1;
            tableData?.rslt.map(data => {
                const arr = []
                tableData?.metaDataChildren.map(s => {
                    arr.push(
                        { k: s, v: JSON.stringify(data[s]) },
                    );
                });
                sheetData.push(arr)
                index = index + 1;
            })
            sheetDataMap.set(sheetName, sheetData);
            sheetDataMap.forEach((values, key) => {
                // 写入excel
                workBook.Sheets[key] = xlsx.utils.aoa_to_sheet(values);
                workBook.Sheets[key]['!cols'] = [{ wpx: 50 }, { wpx: 150 }, { wpx: 180 }, { wpx: 230 }, { wpx: 230 }, { wpx: 230 }]
            })
            workBook.SheetNames = [sheetName];
            // 将workBook写入文件
            xlsx.writeFile(workBook, `${metaData?.name}-data.xlsx`);
        } else {
            message.info('当前资源暂无数据!')
        }
    }
    const renderIcon = (type) => {
        switch (type) {
            case '库':
                return