const getSpfsjtjData = () => {
    const res = {};
    const root = document.querySelectorAll('.portlet')[1];
    Object.keys(rootDefined.spfsjtj.childMap).forEach((moduleKey, index) => {
        res[moduleKey] = {};
        const moduleDom = root.querySelectorAll('td table')[index];
        const moduleFiledMap = rootDefined.spfsjtj.childMap[moduleKey].childMap;
        Object.keys(moduleFiledMap).forEach((fieldKey, fieldIndex) => {
            const fieldDom = moduleDom.querySelectorAll('tr')[fieldIndex + 1].querySelectorAll('td')[1];
            res[moduleKey][fieldKey] = fieldDom.innerText.trim();
        });
    });
    return res;
}

const getClfwsqytjData = () => {
    const res = {};
    const root = document.querySelectorAll('.portlet')[2];
    Object.keys(rootDefined.clfwsqytj.childMap).forEach((moduleKey, index) => {
        res[moduleKey] = {};
        const moduleDom = root.querySelectorAll('td table')[index];
        const moduleFiledMap = rootDefined.clfwsqytj.childMap[moduleKey].childMap;
        Object.keys(moduleFiledMap).forEach((fieldKey, fieldIndex) => {
            const fieldDom = moduleDom.querySelectorAll('tr')[fieldIndex + 1].querySelectorAll('td')[1];
            res[moduleKey][fieldKey] = fieldDom.innerText.replaceAll('　', '');
        });
    });
    return res;
}

const getClfwdtjData = () => {
    const res = {};
    const root = document.querySelectorAll('.portlet')[3];
    Object.keys(rootDefined.clfwdtj.childMap).forEach((moduleKey) => {
        const tableDomList = root.querySelectorAll('td table');
        const moduleFiledMap = rootDefined.clfwdtj.childMap[moduleKey].childMap;
        const dataList = [];
        if (moduleKey === 'broker') {
            // 纵向表格，数据在第2、3个table中
            const trList = [...tableDomList[1].querySelectorAll('tr')].slice(1).concat([...tableDomList[2].querySelectorAll('tr')].slice(1));
            trList.forEach(trDom => {
                const dataItem = {};
                Object.keys(moduleFiledMap).forEach((fieldKey, fieldIndex) => {
                    const fieldDom = trDom.querySelectorAll('td')[fieldIndex];
                    dataItem[fieldKey] = fieldDom.innerText.trim();
                });
                dataList.push(dataItem);
            });
        } else if (moduleKey === 'area') {
            // 横向表格，数据在第5个table中
            const trList = [...tableDomList[4].querySelectorAll('tr')];
            Object.keys(moduleFiledMap).forEach((fieldKey, fieldIndex) => {
                const tdList = [...trList[fieldIndex].querySelectorAll('td')].slice(1);
                tdList.forEach((tdDom, tdIndex) => {
                    if (!dataList[tdIndex]) {
                        dataList[tdIndex] = {};
                    }
                    dataList[tdIndex][fieldKey] = tdDom.innerText.trim();
                });
            });
        } else if (moduleKey === 'district') {
            // 横向表格，数据在第4个table中，叠加在一起，每组数组占据3行，例如，总共9行数据，则1-3为第一组，4-6为第二组，7-9为第三组，三组加起来为一个完整的数据项
            const trList = [...tableDomList[3].querySelectorAll('tr')];
            const groupSize = Object.keys(moduleFiledMap).length; // 每组数据占据的行数
            for (let trIndex = 0; trIndex < trList.length; trIndex++) {
                const fieldIndex = trIndex % groupSize; // 当前行对应的字段索引
                const tdList = [...trList[trIndex].querySelectorAll('td')].slice(1);
                const startIndex = Math.floor(trIndex / groupSize) * tdList.length; // 当前组的起始行索引
                tdList.forEach((tdDom, tdIndex) => {
                    if (!dataList[startIndex + tdIndex]) {
                        dataList[startIndex + tdIndex] = {};
                    }
                    const fieldKey = Object.keys(moduleFiledMap)[fieldIndex];
                    dataList[startIndex + tdIndex][fieldKey] = tdDom.innerText.trim();
                });

            }
        }
        res[moduleKey] = dataList;
    });
    return res;
}


const extractData = () => {
    return {
        spfsjtj: getSpfsjtjData(),
        clfwsqytj: getClfwsqytjData(),
        clfwdtj: getClfwdtjData(),
    }
}