import copy
import logging

from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml import CT_P
from pandas.core.indexers import length_of_indexer

# 获取日志记录器
logger = logging.getLogger(__name__)


# 多行多列的数据复制到表格里,由于舆情表格比表1少一列，因此加个判断条件，flag=0,则是从相同表格复制，flag=1,则是从舆情表格复制
def copy_table(
    source_table, target_table, start_row, end_row, start_col, end_col, flag
):
    try:
        logger.info("遍历源表格的指定范围，将数据复制到目标表格的相同位置")
        # 遍历源表格的指定范围，将数据复制到目标表格的相同位置
        for i in range(start_row, end_row):
            for j in range(start_col, end_col):
                # 获取源表格单元格的内容
                source_cell = source_table.cell(i, j)
                # 将内容复制到目标表格的对应单元格
                target_table.cell(i, j+flag).text = source_cell.text
                """
                if flag == 0:
                    target_table.cell(i, j).text = source_cell.text
                if flag == 1:
                    # j+1即表给从后一列开始添加数据
                    target_table.cell(i, j + 1).text = source_cell.text
                """
    except Exception as e:
        logger.exception(f"复制表格数据时发生错误: {e}")
        raise e

def copy_table_no_align(
        source_table, target_table, start_row, end_row, start_col, end_col, target_row,target_col
):
    try:
        logger.info("遍历源表格的指定范围，将数据复制到目标表格的相同位置")
        # 遍历源表格的指定范围，将数据复制到目标表格的相同位置
        for i in range(start_row, end_row):
            for j in range(start_col, end_col):
                # 获取源表格单元格的内容
                source_cell = source_table.cell(i, j)
                # 将内容复制到目标表格的对应单元格
                target_table.cell(target_row+i, target_col+j).text = source_cell.text
    except Exception as e:
        logger.exception(f"复制表格数据时发生错误: {e}")
        raise e


# 将自行统计是数据插入word表格中
# update:2025-07-04 删除多余行
def copy_sta_table(
    target_table,
    data,
    start_row,
    start_col,
    is_dynamics: bool = None,
):
    try:
        logger.info(f"开始将自行统计的数据插入word表格中,{target_table}")
        # update:2025-07-04 删除多余的行数
        if is_dynamics is not None and is_dynamics and len(data)>1:
            for i in range(len(data) - 1):
                source_row = target_table.rows[-1]._element
                new_row_element = copy.deepcopy(source_row)
                target_table._element.append(new_row_element)
                new_row = target_table.rows[-1]
                target_cell = new_row.cells[0]
                while len(target_cell.paragraphs) > 1:
                    p_to_remove = target_cell.paragraphs[-1]._element
                    target_cell._element.remove(p_to_remove)
                if not target_cell.paragraphs:
                    target_cell._element.append(CT_P())
                main_paragraph = target_cell.paragraphs[0]
                for run in main_paragraph.runs:
                    run.text = ""
                main_paragraph.text = ""
                main_paragraph.add_run(str(i + 2))
                main_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
        # 遍历列表，将其插入到表格的指定位置
        for i in range(len(data)):
            for j in range(len(data[i])):
                # 计算目标表格中的行和列索引
                target_row = start_row + i
                target_col = start_col + j
                # 将数据插入到目标表格的对应单元格
                target_table.cell(target_row, target_col).text = str(data[i][j])
    except Exception as e:
        logger.exception(f"自行统计的数据插入word表格中失败: {e}")
        raise e
