package org.sky.scientific.excel.handle;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.time.LocalDate;

public class AttendanceMergeStrategy implements SheetWriteHandler {

	private final Integer year;
	private final Integer month;

	public AttendanceMergeStrategy(Integer year, Integer month) {
		this.year = year;
		this.month = month;
	}

	@Override
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
		// 不需要实现
	}

	@Override
	public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

		Workbook workbook = writeWorkbookHolder.getWorkbook();
 		Sheet sheet = workbook.getSheetAt(0);

		LocalDate startDate = LocalDate.of(year, month, 1);
		int daysInMonth = startDate.lengthOfMonth();
		int totalColumns = daysInMonth + 7; // 天数 + 7个固定列

		// 1. 合并标题行（第一行合并所有列）
		CellRangeAddress titleMerge = new CellRangeAddress(0, 0, 0, totalColumns - 1);
		sheet.addMergedRegion(titleMerge);

		CellRangeAddress xhMerge = new CellRangeAddress(1, 2, 0, 0);
		sheet.addMergedRegion(xhMerge);

		// 2. 合并纵向表头行（第2行）
		// 姓名合并
		CellRangeAddress nameMerge = new CellRangeAddress(1, 2, 1, 1);
		sheet.addMergedRegion(nameMerge);

		// 工号合并
		CellRangeAddress numberMerge = new CellRangeAddress(1, 2, 2, 2);
		sheet.addMergedRegion(numberMerge);

		// 身份证号码合并
		CellRangeAddress idCardMerge = new CellRangeAddress(1, 2, 3, 3);
		sheet.addMergedRegion(idCardMerge);

		// 科研人员类别合并
		CellRangeAddress typeMerge = new CellRangeAddress(1, 2, 4, 4);
		sheet.addMergedRegion(typeMerge);

		// 本月出勤/小时合并
		CellRangeAddress hoursMerge = new CellRangeAddress(1, 2, totalColumns - 2, totalColumns - 2);
		sheet.addMergedRegion(hoursMerge);

		// 本月出勤/天合并
		CellRangeAddress daysMerge = new CellRangeAddress(1, 2, totalColumns - 1, totalColumns - 1);
		sheet.addMergedRegion(daysMerge);

	}

}
