package com.madeu.view.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.view.AbstractView;

import com.madeu.util.ConvertStringUtil;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WebVacationManagementView extends AbstractView{
	
	private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
	 
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		// 출력 데이터
		Map<String, Object> map = (Map<String, Object>)model.get("map");
		String filename = (String)map.get("name");
		List<Map<String, Object>> selectList = (List<Map<String, Object>>)map.get("selectListVacationManagement");
		
		int rowCount = 0;   // row
		int colCount = 0;   // col

		//1차로 workbook을 생성
		XSSFWorkbook workbook = new XSSFWorkbook();
		
		setContentType(CONTENT_TYPE_XLSX);
		
		//2차는 sheet생성
		Sheet sheet = workbook.createSheet("연차관리 정보");
		XSSFCellStyle headerStyle = workbook.createCellStyle();
		headerStyle.setAlignment(HorizontalAlignment.CENTER);
		headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		
		Row header = sheet.createRow(rowCount++);
			
		
//		sheet.addMergedRegion(new CellRangeAddress(0,1,0,0));
		createHeaderCell(0, header, headerStyle, "번호");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,1,1));
		createHeaderCell(1, header, headerStyle, "이름(ID)");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,2,2));
		createHeaderCell(2, header, headerStyle, "조직");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,3,3));
		createHeaderCell(3, header, headerStyle, "직무");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,4,4));
		createHeaderCell(4, header, headerStyle, "발행 연차 수");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,5,5));
		createHeaderCell(5, header, headerStyle, "사용");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,6,6));
		createHeaderCell(6, header, headerStyle, "잔여");
		createHeaderCell(7, header, headerStyle, "발행연도");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,7,7));
		createHeaderCell(8, header, headerStyle, "사용기한");
//		sheet.addMergedRegion(new CellRangeAddress(0,1,8,8));
		createHeaderCell(9, header, headerStyle, "연차 발행 메모");

		// 엑셀에 데이터 입력
		for(Map<String, Object> e:selectList){
			Row row = sheet.createRow(rowCount++);
			row.setHeight((short)350);
			row.createCell(0).setCellValue(ConvertStringUtil.ifNullToString(rowCount-1));
			row.createCell(1).setCellValue(ConvertStringUtil.ifNullToString(e.get("memberNameId")));
			row.createCell(2).setCellValue(ConvertStringUtil.ifNullToString(e.get("groupName")));
			row.createCell(3).setCellValue(ConvertStringUtil.ifNullToString(e.get("dutyName")));
			row.createCell(4).setCellValue(ConvertStringUtil.ifNullToString(e.get("days")));
			row.createCell(5).setCellValue(ConvertStringUtil.ifNullToString(e.get("useAttendanceCount")));
			row.createCell(6).setCellValue(ConvertStringUtil.ifNullToString(e.get("unuseAttendanceCount")));
			row.createCell(7).setCellValue(ConvertStringUtil.ifNullToString(e.get("year")));
			row.createCell(8).setCellValue(ConvertStringUtil.ifNullToString(e.get("startEndDate")));
			row.createCell(9).setCellValue(ConvertStringUtil.ifNullToString(e.get("etc")));
		}

		
		//response.setContentType("Application/Msexcel");
		response.setContentType(getContentType());
		Date toDay = new Date();        
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String nowDateTime = dateFormat.format(toDay);
	    log.debug("1. 현재날짜                                : "+ nowDateTime);
		response.setHeader("Content-Disposition", "ATTachment; Filename=".concat(new String(filename.getBytes("euc-kr"),"8859_1")).concat("_").concat(nowDateTime).concat(".xlsx"));
		
		ServletOutputStream out = response.getOutputStream();
        out.flush();
        workbook.write(out);
        out.flush();
        
   }
	
	private void createHeaderCell(int col, Row row, CellStyle cellStyle, String value){
		Cell cell = row.createCell(col);
		cell.setCellStyle(cellStyle);
		cell.setCellValue(value);
	}
}